Ansible - telnet失败 - 命令/ shell模块 - rc返回代码1失败:true

时间:2017-01-05 02:07:08

标签: ansible ansible-playbook telnet exit-code ansible-2.x

这个问题的确有一些历史记录:Auto exit Telnet command back to prompt without human intervention ^] quit close exit code 1

顺便说一句,我尝试使用telnet命令测试端口,其中端口可以是OPEN或NOT。我怎样才能在Ansible中做同样的事情?

我尝试了以下但是(端口是打开还是未打开/它无效),Ansible总是在结果或输出字典变量中说'failed': true并返回代码'rc': 1 < / p>

我也试过了command模块和shell模块,但它没有用。

我该如何让Ansible成功运行此telnet命令,以便下一步操作(在剧本的任务中定义)可以正常工作。

我的Playbook代码段

- name: Ensure collector agent's proxy_address and proxy_port is open
  #command: "echo -e \"^]\\nclose\"| telnet {{ proxy_address }} {{ proxy_port }}"
  #shell: "echo -e \"^]\\nclose\" | telnet `hostname` {{ proxy_port }}"
  shell: "telnet `hostname` {{ proxy_port }}"
  register: telnet_result
  ignore_errors: yes
  tags:
   - giga

- debug: msg="telnet output result was = {{ telnet_result }}"
  tags:
   - giga

使用以下命令运行ansible-playbook,给出了以下错误(如果我使用上面列出的shell命令之一)。

PS:文件myplaybook.yml只是调用角色mytask

ansible-playbook -i inventory -l localhost myplaybook.yml --extra-vars "proxy_port=2878" -t giga -vvv

输出:

TASK [mytask : Ensure collector agent's proxy_address and proxy_port is open] ***
task path: /home/vagrant/aks/ansible/roles/mytask/tasks/install_telegraf.yml:3
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: vagrant
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1483580659.49-61726964686488 `" && echo ansible-tmp-1483580659.49-61726964686488="` echo $HOME/.ansible/tmp/ansible-tmp-1483580659.49-61726964686488 `" ) && sleep 0'
<localhost> PUT /tmp/tmpoVYTlY TO /home/vagrant/.ansible/tmp/ansible-tmp-1483580659.49-61726964686488/command
<localhost> EXEC /bin/sh -c 'chmod u+x /home/vagrant/.ansible/tmp/ansible-tmp-1483580659.49-61726964686488/ /home/vagrant/.ansible/tmp/ansible-tmp-1483580659.49-61726964686488/command && sleep 0'
<localhost> EXEC /bin/sh -c 'sudo -H -S -n -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-asjgqzogcakikhnvwamwruivfbutphtz; LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /home/vagrant/.ansible/tmp/ansible-tmp-1483580659.49-61726964686488/command; rm -rf "/home/vagrant/.ansible/tmp/ansible-tmp-1483580659.49-61726964686488/" > /dev/null 2>&1'"'"' && sleep 0'
fatal: [localhost]: FAILED! => {"changed": true, "cmd": "telnet `hostname` 2878", "delta": "0:00:00.006042", "end": "2017-01-05 01:44:19.590362", "failed": true, "invocation": {"module_args": {"_raw_params": "telnet `hostname` 2878", "_uses_shell": true, "chdir": null, "creates": null, "executable": null, "removes": null, "warn": true}, "module_name": "command"}, "rc": 1, "start": "2017-01-05 01:44:19.584320", "stderr": "Connection closed by foreign host.", "stdout": "Trying 127.0.0.1...\r\nConnected to myvagrant.\r\nEscape character is '^]'.", "stdout_lines": ["Trying 127.0.0.1...", "Connected to myvagrant.", "Escape character is '^]'."], "warnings": []}
...ignoring

TASK [mytask : debug] *************************************
task path: /home/vagrant/aks/ansible/roles/mytask/tasks/install_telegraf.yml:12
ok: [localhost] => {
    "msg": "telnet output result was = {u'changed': True, u'end': u'2017-01-05 01:44:19.590362', u'stdout': u\"Trying 127.0.0.1...\\r\\nConnected to myvagrant.\\r\\nEscape character is '^]'.\", u'cmd': u'telnet `hostname` 2878', 'failed': True, u'delta': u'0:00:00.006042', u'stderr': u'Connection closed by foreign host.', u'rc': 1, 'stdout_lines': [u'Trying 127.0.0.1...', u'Connected to myvagrant.', u\"Escape character is '^]'.\"], u'start': u'2017-01-05 01:44:19.584320', u'warnings': []}"
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0   

1 个答案:

答案 0 :(得分:2)

failed_when: <your_condition>,例如:

failed_when: false

此外,您似乎试图模仿wait_for模块。