ansiblechown

Ansible chown operation not permitted for non-root user


I have the following in an Ansible playbook:

- name: Create certificates directory
  file:
    dest: "{{ '~/wireguard/certs' | expanduser }}"
    state: directory
    owner: "{{ ansible_user_id }}"
    group: "{{ ansible_user_id }}"
    mode: 0700
  run_once: true
  delegate_to: localhost

However, when it gets run in the playbook, I get the following error:

fatal: [1.2.3.4 -> localhost]: FAILED! => {
  "changed": false,
  "gid": 1000,
  "group": "alex",
  "mode": "0755",
  "msg": "chown failed: [Errno 1] Operation not permitted: b'/home/alex/wireguard'",
  "owner": "alex",
  "path": "/home/alex/wireguard",
  "size": 4096,
  "state": "directory",
  "uid": 1000
}

Do I need to run this as root or is it something else? If I do need to run it as root, does become work?


Solution

  • I realized that ansible_user_id didn't have the username that I was expecting, so I was trying to change the ownership to a user that didn't exist. I fixed it by setting a new variable to my local user.