Ansible 无法使用 sudo 权限执行命令,但手动启动时该命令可以运行

Ansible 无法使用 sudo 权限执行命令,但手动启动时该命令可以运行

我有点困惑:我试图在目标 MacOS 机器上自动执行某些任务,但需要 sudo 的命令只有在手动启动时才有效,而不能通过 Ansible 启动。以下是一个非常简单的任务的示例:

- name: move brew
  become: yes
  command: mkdir /opt/homebrew

我也尝试使用 Ansible 的文件 mmodule,但没有什么区别:

- name: Create /opt/homebrew directory
  become: true
  become_user: user
  file:
    path: /opt/homebrew
    state: directory
    owner: user
    group: Domain Users

在这两种情况下我都会收到有关权限的错误:

FAILED! => {"changed": true, "cmd": ["mkdir", "/opt/homebrew"], "delta": "0:00:00.002731", "end": "2023-06-19 11:07:51.172314", "msg": "non-zero return code", "rc": 1, "start": "2023-06-19 11:07:51.169583", "stderr": "mkdir: /opt/homebrew: File exists", "stderr_lines": ["mkdir: /opt/homebrew: File exists"], "stdout": "", "stdout_lines": []}

FAILED! => {"changed": false, "msg": "There was an issue creating /opt/homebrew2 as requested: [Errno 13] Permission denied: b'/opt/homebrew2'", "path": "/opt/homebrew2", "state": "absent"}

如果我使用命令行中的相同命令创建目录,它就会起作用:

user$ sudo mkdir /opt/homebrew2
Password:
user$

这是用户的条目visudo

user  ALL=NOPASSWD: /usr/bin/xcode-select

我将很感激对此事的任何建议。

相关内容