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

Ansible - telnet fails - command / shell module - rc return code 1 failed: true

这个问题确实有一些来自这个post的历史:

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

我尝试了以下但(无论端口是否打开 open/it 无效),Ansible 总是在结果或输出字典变量和 return 代码中说 'failed': true 'rc': 1

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

我应该怎么做才能成功获得 Ansible 运行 这个 telnet 命令,以便接下来的操作(在剧本的任务中定义)可以工作。

我的剧本片段:

- 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   

failed_when: <your_condition>,例如:

failed_when: false

此外,您似乎想模仿 wait_for 模块。