Ansible playbook 在所有 Ubuntu 服务器上均失败

Posted

技术标签:

【中文标题】Ansible playbook 在所有 Ubuntu 服务器上均失败【英文标题】:Ansible playbooks failing on ALL Ubuntu servers 【发布时间】:2020-05-27 10:47:21 【问题描述】:

我在 CentOS 服务器上运行 Ansible,可以在 Windows 和 RHEL/CentOS Linux 服务器上成功运行 playbook,但令我震惊的是,我无法在任何 Ubuntu 服务器(v14.04、v16.04 或v18.04 - 包括 AWS 托管的实例)!

奇怪的是 Anible hostname -m ping 命令在所有 Ubuntu 主机上都能正常工作...

我尝试将 CentOS 和 OpenSSH 更新到最新版本,但无济于事。我正在努力解决这个问题,所以我希望有人可以帮助我......

我的 Ansible 设置如下:

[root@ansible ansible]# ansible --version
ansible 2.9.3
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

CentOS 版本:

[root@ansible ansible]# rpm -q centos-release
centos-release-7-7.1908.0.el7.centos.x86_64

OpenSSH 版本:

OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017

以详细模式运行 playbook 会产生以下输出:

TASK [Installing package nginx] ***************************************************************************************************************************************
task path: /etc/ansible/sample.yml:7
<192.168.xxx.xxx> ESTABLISH SSH CONNECTION FOR USER: root
<192.168.xxx.xxx> SSH: EXEC sshpass -d8 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="root"' -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/e8342f25c3 192.168.xxx.xxx '/bin/sh -c '"'"'echo ~root && sleep 0'"'"''
<192.168.xxx.xxx> (0, '/root\n', '')
<192.168.xxx.xxx> ESTABLISH SSH CONNECTION FOR USER: root
<192.168.xxx.xxx> SSH: EXEC sshpass -d8 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="root"' -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/e8342f25c3 192.168.xxx.xxx '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1581462806.41-181529489855642 `" && echo ansible-tmp-1581462806.41-181529489855642="` echo /root/.ansible/tmp/ansible-tmp-1581462806.41-181529489855642 `" ) && sleep 0'"'"''
<192.168.xxx.xxx> (0, 'ansible-tmp-1581462806.41-181529489855642=/root/.ansible/tmp/ansible-tmp-1581462806.41-181529489855642\n', '')
Using module file /usr/lib/python2.7/site-packages/ansible/modules/packaging/os/apt.py
<192.168.xxx.xxx> PUT /root/.ansible/tmp/ansible-local-9871PVuptY/tmpt4YxAm TO /root/.ansible/tmp/ansible-tmp-1581462806.41-181529489855642/AnsiballZ_apt.py
<192.168.xxx.xxx> SSH: EXEC sshpass -d8 sftp -o BatchMode=no -b - -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="root"' -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/e8342f25c3 '[192.168.xxx.xxx]'
<192.168.xxx.xxx> (0, 'sftp> put /root/.ansible/tmp/ansible-local-9871PVuptY/tmpt4YxAm /root/.ansible/tmp/ansible-tmp-1581462806.41-181529489855642/AnsiballZ_apt.py\n', '')
<192.168.xxx.xxx> ESTABLISH SSH CONNECTION FOR USER: root
<192.168.xxx.xxx> SSH: EXEC sshpass -d8 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="root"' -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/e8342f25c3 192.168.xxx.xxx '/bin/sh -c '"'"'chmod u+x /root/.ansible/tmp/ansible-tmp-1581462806.41-181529489855642/ /root/.ansible/tmp/ansible-tmp-1581462806.41-181529489855642/AnsiballZ_apt.py && sleep 0'"'"''
<192.168.xxx.xxx> (0, '', '')
<192.168.xxx.xxx> ESTABLISH SSH CONNECTION FOR USER: root
<192.168.xxx.xxx> SSH: EXEC sshpass -d8 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="root"' -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/e8342f25c3 -tt 192.168.xxx.xxx '/bin/sh -c '"'"'/usr/bin/python3 /root/.ansible/tmp/ansible-tmp-1581462806.41-181529489855642/AnsiballZ_apt.py && sleep 0'"'"''
<192.168.xxx.xxx> (1, '\r\n"msg": "value of state must be one of: absent, build-dep, fixed, latest, present, got: installed", "failed": true, "invocation": "module_args": "state": "installed", "update_cache": true, "pkg": "nginx", "package": ["nginx"], "cache_valid_time": 0, "purge": false, "force": false, "dpkg_options": "force-confdef,force-confold", "autoremove": false, "autoclean": false, "only_upgrade": false, "force_apt_get": false, "allow_unauthenticated": false\r\n', 'Shared connection to 192.168.xxx.xxx closed.\r\n')
<192.168.xxx.xxx> Failed to connect to the host via ssh: Shared connection to 192.168.xxx.xxx closed.
<192.168.xxx.xxx> ESTABLISH SSH CONNECTION FOR USER: root
<192.168.xxx.xxx> SSH: EXEC sshpass -d8 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="root"' -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/e8342f25c3 192.168.xxx.xxx '/bin/sh -c '"'"'rm -f -r /root/.ansible/tmp/ansible-tmp-1581462806.41-181529489855642/ > /dev/null 2>&1 && sleep 0'"'"''
<192.168.xxx.xxx> (0, '', '')
fatal: [ubuntu_host]: FAILED! => 

【问题讨论】:

【参考方案1】:

"msg": "状态值必须是以下之一:缺席、构建-dep、已修复、最新、当前、已安装:已安装"、"失败":真、"调用": “module_args”:“state”:“已安装”,“update_cache”:true,“pkg”:“nginx”,“package”:[“nginx”],“cache_valid_time”:0,“purge”:false, “force”:假,“dpkg_options”:“force-confdef,force-confold”,“autoremove”:假,“autoclean”:假,“only_upgrade”:假,“force_apt_get”:假,“allow_unauthenticated”:假

答:答案包含在错误消息中。详情请见apt。

【讨论】:

正如您所建议的,答案在错误消息中。我将“已安装”更改为“现在”,现在一切正常。感谢您的帮助! 这个问题似乎是因为 ansible yum_module does allow state of installed) 而 apt module does not.

以上是关于Ansible playbook 在所有 Ubuntu 服务器上均失败的主要内容,如果未能解决你的问题,请参考以下文章

Ansible playbook复用 - -01

Ansible8:Playbook循环

Ansible-playbook 学习

Ansible8:Playbook循环

执行playbook后提交所有更改

自动化运维工具Ansible实战playbook循环