TASK [Gathering Facts] *********************************************************
task path: /opt/playbook/site.yml:1
Using module file /usr/local/lib/python3.10/dist-packages/ansible/modules/setup.py
Pipelining is enabled.
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: slurm
<localhost> EXEC /bin/sh -c 'sudo -H -S -n -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-mvloemssulwwmnnhtatxivyevcbshjsb ; /usr/bin/python3'"'"' && sleep 0'
fatal: [localhost]: FAILED! => {
"ansible_facts": {},
"changed": false,
"failed_modules": {
"ansible.legacy.setup": {
"failed": true,
"module_stderr": "sudo: a password is required\n",
"module_stdout": "",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}
},
"msg": "The following modules failed to execute: ansible.legacy.setup\n"
}
剧本由 slurm 用户在节点启动时执行。但是,它在收集事实时失败了,我不确定问题出在哪里。显然 sudo 有问题。我正在寻找更有效地调试它的方法。
剧本在普通 ubuntu 用户下运行没有问题。
@natan——我认为如果你不尝试如此直接地调用
sudo
可能会更好。此外,您可能不需要成为 root 用户即可执行setup
.
这是我的很多剧本中的标题:
- name: Initial node setup
hosts: localhost
connection: local
gather_facts: yes
tasks:
- name: Configure the machine...
关键事项:
hosts: localhost
——这是您唯一正在使用的机器。connection: local
-- 避免使用 ssh
连接到它已经在执行的机器上。gather_facts: yes
——你可以使用它来获取你的setup
事实,你不需要成为 root。这就剩下后续操作如何成为root的问题了。 传统上,连接参数(包括密码,如果不可避免)已在清单文件中提供。 您可以在您的物品栏或您的剧本中设置
ansible_become_password
,但您永远不应该使用纯文本形式的密码。
然后,对于需要root才能成功的任务,可以使用:
become: yes
我推荐此 Ansible 页面以获取更多信息:Ansible:了解权限升级