我正在一个角色里面运行一个ansible playbook(ansible V1.9.4),我试图安装并运行带参数的命令,但当我尝试从任务的Anisble-playbook执行时 - name:运行cht agent 的安装,它不能正常运行,但实际上在执行此任务期间会挂起。相反,当我在ec2主机上手动运行命令时,它运行完美,没有任何问题。我观察到的可能是从ansible-play的任务没有正确传递给主机的参数,但我可能错了。
这是我正在运行的剧本,
---
# tasks file for cloudhealth-agent installation
- name: Download the agent
command: wget https://s3.amazonaws.com/remote-collector/agent/v14/install_cht_perfmon.sh -O /tmp/install_cht_perfmon.sh
- name: Run installation of cht agent
command: sh /tmp/install_cht_perfmon.sh 14 {{ cht_unique_registration_code }} aws
args:
creates: /opt/cht_perfmon
sudo: yes
register: install_cmd
- debug: var= {{ install_cmd | to_nice_yaml }}
我的剧本执行输出在下面,
$ ansible-playbook -i inventory/ec2.py cloudhealth-agent.yml -vvv
PLAY [setup cloudhealth agent] ************************************************
GATHERING FACTS ***************************************************************
<ip-172-27-0-189.eu-west-1.compute.internal> ESTABLISH CONNECTION FOR USER: ubuntu
<ip-172-27-0-189.eu-west-1.compute.internal> REMOTE_MODULE setup
<ip-172-27-0-189.eu-west-1.compute.internal> EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=600s -o ForwardAgent=yes -o ConnectTimeout=30 -F ./ssh_config -o ControlPath="/Users/ming/.ansible/cp/%h-%r" -o StrictHostKeyChecking=no -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 ip-172-27-0-189.eu-west-1.compute.internal /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1466754898.59-115321310845059 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1466754898.59-115321310845059 && echo $HOME/.ansible/tmp/ansible-tmp-1466754898.59-115321310845059'
<ip-172-27-0-189.eu-west-1.compute.internal> PUT /var/folders/ws/7l0c9mh95cddncp060gksz3m0000gn/T/tmpCtS3WG TO /home/ubuntu/.ansible/tmp/ansible-tmp-1466754898.59-115321310845059/setup
<ip-172-27-0-189.eu-west-1.compute.internal> EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=600s -o ForwardAgent=yes -o ConnectTimeout=30 -F ./ssh_config -o ControlPath="/Users/ming/.ansible/cp/%h-%r" -o StrictHostKeyChecking=no -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 ip-172-27-0-189.eu-west-1.compute.internal /bin/sh -c 'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /home/ubuntu/.ansible/tmp/ansible-tmp-1466754898.59-115321310845059/setup; rm -rf /home/ubuntu/.ansible/tmp/ansible-tmp-1466754898.59-115321310845059/ >/dev/null 2>&1'
ok: [ip-172-27-0-189.eu-west-1.compute.internal]
TASK: [cloudhealth-agent | Download the agent] ********************************
<ip-172-27-0-189.eu-west-1.compute.internal> ESTABLISH CONNECTION FOR USER: ubuntu
<ip-172-27-0-189.eu-west-1.compute.internal> REMOTE_MODULE command wget https://s3.amazonaws.com/remote-collector/agent/v14/install_cht_perfmon.sh -O /tmp/install_cht_perfmon.sh
<ip-172-27-0-189.eu-west-1.compute.internal> EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=600s -o ForwardAgent=yes -o ConnectTimeout=30 -F ./ssh_config -o ControlPath="/Users/ming/.ansible/cp/%h-%r" -o StrictHostKeyChecking=no -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 ip-172-27-0-189.eu-west-1.compute.internal /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1466754904.34-271098190135647 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1466754904.34-271098190135647 && echo $HOME/.ansible/tmp/ansible-tmp-1466754904.34-271098190135647'
<ip-172-27-0-189.eu-west-1.compute.internal> PUT /var/folders/ws/7l0c9mh95cddncp060gksz3m0000gn/T/tmpbskJDx TO /home/ubuntu/.ansible/tmp/ansible-tmp-1466754904.34-271098190135647/command
<ip-172-27-0-189.eu-west-1.compute.internal> EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=600s -o ForwardAgent=yes -o ConnectTimeout=30 -F ./ssh_config -o ControlPath="/Users/ming/.ansible/cp/%h-%r" -o StrictHostKeyChecking=no -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 ip-172-27-0-189.eu-west-1.compute.internal /bin/sh -c 'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /home/ubuntu/.ansible/tmp/ansible-tmp-1466754904.34-271098190135647/command; rm -rf /home/ubuntu/.ansible/tmp/ansible-tmp-1466754904.34-271098190135647/ >/dev/null 2>&1'
changed: [ip-172-27-0-189.eu-west-1.compute.internal] => {"changed": true, "cmd": ["wget", "https://s3.amazonaws.com/remote-collector/agent/v14/install_cht_perfmon.sh", "-O", "/tmp/install_cht_perfmon.sh"], "delta": "0:00:00.344186", "end": "2016-06-24 07:55:47.631705", "rc": 0, "start": "2016-06-24 07:55:47.287519", "stderr": "--2016-06-24 07:55:47-- https://s3.amazonaws.com/remote-collector/agent/v14/install_cht_perfmon.sh\nResolving s3.amazonaws.com (s3.amazonaws.com)... 54.231.34.80\nConnecting to s3.amazonaws.com (s3.amazonaws.com)|54.231.34.80|:443... connected.\nHTTP request sent, awaiting response... 200 OK\nLength: 10964 (11K) [application/x-sh]\nSaving to: ‘/tmp/install_cht_perfmon.sh’\n\n 0K .......... 100% 77.6M=0s\n\n2016-06-24 07:55:47 (77.6 MB/s) - ‘/tmp/install_cht_perfmon.sh’ saved [10964/10964]", "stdout": "", "warnings": ["Consider using get_url module rather than running wget"]}
TASK: [cloudhealth-agent | Run installation of cht agent] *********************
<ip-172-27-0-189.eu-west-1.compute.internal> ESTABLISH CONNECTION FOR USER: ubuntu
<ip-172-27-0-189.eu-west-1.compute.internal> REMOTE_MODULE command creates=/opt/cht_perfmon sh /tmp/install_cht_perfmon.sh 14 123xxx-123xxx-abcxxx-abcxxx aws
<ip-172-27-0-189.eu-west-1.compute.internal> EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=600s -o ForwardAgent=yes -o ConnectTimeout=30 -F ./ssh_config -o ControlPath="/Users/ming/.ansible/cp/%h-%r" -o StrictHostKeyChecking=no -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 ip-172-27-0-189.eu-west-1.compute.internal /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1466754907.35-97872289930978 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1466754907.35-97872289930978 && echo $HOME/.ansible/tmp/ansible-tmp-1466754907.35-97872289930978'
<ip-172-27-0-189.eu-west-1.compute.internal> PUT /var/folders/ws/7l0c9mh95cddncp060gksz3m0000gn/T/tmppMq1Of TO /home/ubuntu/.ansible/tmp/ansible-tmp-1466754907.35-97872289930978/command
<ip-172-27-0-189.eu-west-1.compute.internal> EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=600s -o ForwardAgent=yes -o ConnectTimeout=30 -F ./ssh_config -o ControlPath="/Users/ming/.ansible/cp/%h-%r" -o StrictHostKeyChecking=no -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 ip-172-27-0-189.eu-west-1.compute.internal /bin/sh -c 'sudo -k && sudo -H -S -p "[sudo via ansible, key=ayckvspsdjyouwajusfxzdiepomhqzil] password: " -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-ayckvspsdjyouwajusfxzdiepomhqzil; LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /home/ubuntu/.ansible/tmp/ansible-tmp-1466754907.35-97872289930978/command; rm -rf /home/ubuntu/.ansible/tmp/ansible-tmp-1466754907.35-97872289930978/ >/dev/null 2>&1'"'"''
^CERROR: interrupted
注意到最后一行显示了一串空引号-u root /bin/sh -c '"'"'
。在我看来,命令语法没有正确传递参数。有关如何解析或提供正确的命令行以在远程主机上执行的任何想法或建议?
答案 0 :(得分:1)
我解决了这个问题。我的ansible语法不是命令或shell,但是我传递了错误的API密钥,导致了&#39; install_cht_perfomsh&#39;无法使用错误的密钥进行处理。
〜感谢您的帮助
答案 1 :(得分:0)
您应该使用shell - Execute commands in nodes:
shell模块接受命令名后跟一个列表 以空格分隔的参数。它几乎就像命令一样 模块,但通过远程上的shell(/ bin / sh)运行命令 节点
- name: Run installation of cht agent
shell: bash -c "/tmp/install_cht_perfmon.sh 14 {{ cht_unique_registration_code }} aws"
args:
creates: /opt/cht_perfmon
sudo: yes
register: install_cmd
或:
- name: Run installation of cht agent
shell: /tmp/install_cht_perfmon.sh 14 {{ cht_unique_registration_code }} aws
args:
creates: /opt/cht_perfmon
sudo: yes
register: install_cmd