获取正在执行的确切 Ansible 命令
Get exact Ansible command that is being executed
是否可以确切知道以下 Ansible 代码将在远程服务器上执行什么命令
- name: Ensure apache is running
service: name=httpd state=started
become: true
我相信用户应该有适当的 sudo 权限。但是,我不断收到 sudo: a password is required
.
更新 #1
根据提供的评论,这是我的完整 Ansible 命令:
ANSIBLE_KEEP_REMOTE_FILES=1 sudo -u userA ansible-playbook ssl_playbook.yml -i inventories/staging --extra-vars "target=my_server_set" --private-key=/path/to/ssh.key --u userB -vvv
- userB 是已配置指定 SSH 密钥的远程用户
- userB 的 sudo 权限有限。不幸的是我不能改变这个,我不是服务器管理员。
- userB 目前已经配置为通过 SSH/Key 访问一堆服务器,它似乎是 Ansible 的主要候选者。我目前能够通过 SSH(Apache、Tomcat、Jenkins 等)手动管理我所有的中间件,并希望使用 Ansible 使其自动化。
您第一个问题 ("Is it possible to know exactly what command the following Ansible code will execute on the remote server?") 的答案通常是 "only by inspecting the source for the corresponding module"。给定的模块可以 运行 多个 命令来完成它的操作。
您看到的错误消息(“sudo: a password is required.
”)并不表示远程用户没有适当的 sudo
权限。它仅表明远程用户未配置为无密码 sudo
。您的两个选择是:
为 Ansible 提供密码:
ansible-playbook -K secretpassword ...
修改远程主机上的sudoers
配置以允许无密码sudo
:
remoteuser ALL=(ALL) NOPASSWD:ALL
涉及一组有限命令的 Sudo 配置可能无法正常工作,因为 Ansible 运行使用 sudo
创建一个 脚本。例如,如果我 运行 ansible-playbook -vvv
对抗以下剧本:
- hosts: localhost
gather_facts: false
tasks:
- ping:
become: true
我会看到:
<localhost> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/lars/.ansible/cp/8a5a4c6a60 -tt localhost '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-rebjujbhceobxvfuylirykxzgdonillt; /usr/bin/python /home/lars/.ansible/tmp/ansible-tmp-1505503292.11-47458712165303/ping.py; rm -rf "/home/lars/.ansible/tmp/ansible-tmp-1505503292.11-47458712165303/" > /dev/null 2>&1'"'"'"'"'"'"'"'"' && sleep 0'"'"''
也就是说ansible是运行ning:
sudo -H -S -n -u root /bin/sh -c '...embedded script here...'
sudo
见过的唯一命令是 /bin/sh
,这意味着 sudo
限制您只能使用某些命令的配置注定要失败。
如果您无法修复远程 sudo
配置,您可能需要调查 ansible 的 raw 模块。
是否可以确切知道以下 Ansible 代码将在远程服务器上执行什么命令
- name: Ensure apache is running
service: name=httpd state=started
become: true
我相信用户应该有适当的 sudo 权限。但是,我不断收到 sudo: a password is required
.
更新 #1
根据提供的评论,这是我的完整 Ansible 命令:
ANSIBLE_KEEP_REMOTE_FILES=1 sudo -u userA ansible-playbook ssl_playbook.yml -i inventories/staging --extra-vars "target=my_server_set" --private-key=/path/to/ssh.key --u userB -vvv
- userB 是已配置指定 SSH 密钥的远程用户
- userB 的 sudo 权限有限。不幸的是我不能改变这个,我不是服务器管理员。
- userB 目前已经配置为通过 SSH/Key 访问一堆服务器,它似乎是 Ansible 的主要候选者。我目前能够通过 SSH(Apache、Tomcat、Jenkins 等)手动管理我所有的中间件,并希望使用 Ansible 使其自动化。
您第一个问题 ("Is it possible to know exactly what command the following Ansible code will execute on the remote server?") 的答案通常是 "only by inspecting the source for the corresponding module"。给定的模块可以 运行 多个 命令来完成它的操作。
您看到的错误消息(“sudo: a password is required.
”)并不表示远程用户没有适当的 sudo
权限。它仅表明远程用户未配置为无密码 sudo
。您的两个选择是:
为 Ansible 提供密码:
ansible-playbook -K secretpassword ...
修改远程主机上的
sudoers
配置以允许无密码sudo
:remoteuser ALL=(ALL) NOPASSWD:ALL
涉及一组有限命令的 Sudo 配置可能无法正常工作,因为 Ansible 运行使用 sudo
创建一个 脚本。例如,如果我 运行 ansible-playbook -vvv
对抗以下剧本:
- hosts: localhost
gather_facts: false
tasks:
- ping:
become: true
我会看到:
<localhost> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/lars/.ansible/cp/8a5a4c6a60 -tt localhost '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-rebjujbhceobxvfuylirykxzgdonillt; /usr/bin/python /home/lars/.ansible/tmp/ansible-tmp-1505503292.11-47458712165303/ping.py; rm -rf "/home/lars/.ansible/tmp/ansible-tmp-1505503292.11-47458712165303/" > /dev/null 2>&1'"'"'"'"'"'"'"'"' && sleep 0'"'"''
也就是说ansible是运行ning:
sudo -H -S -n -u root /bin/sh -c '...embedded script here...'
sudo
见过的唯一命令是 /bin/sh
,这意味着 sudo
限制您只能使用某些命令的配置注定要失败。
如果您无法修复远程 sudo
配置,您可能需要调查 ansible 的 raw 模块。