Ansible命令/Shell模块似乎无法评估和选择参数。

3
我正在运行一个ansible playbook(ansible V1.9.4),它在角色内部运行一个带参数的命令。但当我尝试在任务- name: Run installation of cht agent中执行Anisble-playbook时,它不能正确运行,实际上在执行此任务过程中挂起了。相反,当我手动在ec2主机上运行该命令时,它完美地运行,没有任何问题。我观察到的是,可能有一些参数没有正确地从ansible-play的任务传递到主机,但我可能错了。这是我正在运行的playbook:
---
# 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 }}

我的playbook执行的输出如下:
$ 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 '"'"'。这似乎意味着命令语法没有正确地传递参数。有什么解决方法或建议来解决或提供正确的命令行以在远程主机上执行吗?


Ansible不会直接通过ssh运行命令,而是创建临时文件来运行(这可能会使调试变得更加困难)。当它挂起时,您可以执行类似于ps aux的操作来查找进程,它将向您显示正在运行的内容。我打赌脚本正在等待您的交互输入,出于某种原因。 - Xiong Chiamiov
还有一件事要检查:您是否尝试在代码中硬编码 cht_unique_registration_code 的值?这将告诉您问题是出在命令还是变量定义上。 - Xiong Chiamiov
尝试使用shell模块代替command - 它们在ansible中是不同的东西。 - Konstantin Suvorov
2个回答

1
我已经解决了这个问题。问题不在我的命令或shell的ansible语法上,而是我传递了错误的API密钥,导致'install_cht_perfomsh'无法使用错误的密钥进行处理。
~谢谢帮助

1

你应该使用shell - 在节点上执行命令

shell模块接受命令名称,后跟由空格分隔的参数列表。它几乎与command模块完全相同,但在远程节点上通过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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接