非 root 用户不允许执行 Ansible chown 操作



我在 Ansible 剧本中有以下内容:

- 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

但是,当它在剧本中运行时,我收到以下错误:

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
}

我需要以 root 身份运行它还是其他东西?如果我确实需要以 root 身份运行它,become有效吗?

Do I need to run this as root or is it something else?

需要根。例如,请参阅更改所有权

"超级用户root具有不受限制的功能,可以更改任何文件的所有权,但普通用户只能更改他们拥有的文件的所有权。

这实际上意味着普通用户只能将他们拥有的文件组更改为他们所属的组。

If I do need to run it as root, does become work?

是的,成为作品。经常使用的插件是sudo。默认值 become_user 为 root。

- file:
  become: yes
  become_method: sudo
  ...

通常,启用远程/登录用户成为 root。但在您的特定情况下,由于delegate_to: localhost,启用正在运行播放的用户。例如在本地主机上进行更改

$ grep alex /etc/sudoers
alex ALL=(ALL) NOPASSWD: ALL

有关其他选项,请参阅插件列表。

我意识到ansible_user_id没有我期望的用户名,所以我试图将所有权更改为不存在的用户。我通过为本地用户设置一个新变量来修复它。

最新更新