使用包而不是 yum 模块,如 playbook 中 How to unify package installation tasks in ansible? 中的回答。
当我阅读并遵循一本基于 CentOS/RedHat 编写的书时,我遇到了同样的错误。该问题本身可以通过以下方式避免,但如果继续在 Ubuntu 上使用 YUM 会导致更多问题。
环境
DISTRIB_DESCRIPTION="Ubuntu 16.04.1 LTS"
确认症状
ansible@ip-172-31-38-30:~/playbook$ ansible-playbook sample.yaml -v
Using /etc/ansible/ansible.cfg as config file
PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
ok: [ub01]
TASK [Ensure NTP (for time synchronization) is installed.] *********************
fatal: [ub01]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Shared connection to ub01 closed.\r\n", "module_stdout": "\r\nTraceback (most recent call last):\r\n File \"/tmp/ansible_y0ULtJ/ansible_module_yum.py\", line 25, in <module>\r\n import yum\r\nImportError: No module named yum\r\n", "msg": "MODULE FAILURE"}
to retry, use: --limit @/home/ansible/playbook/sample.retry
PLAY RECAP *********************************************************************
ub01 : ok=1 changed=0 unreachable=0 failed=1
安装 yum
ansible@ip-172-31-38-30:~/playbook$ sudo apt install yum
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
yum
0 upgraded, 1 newly installed, 0 to remove and 58 not upgraded.
Need to get 486 kB of archives.
After this operation, 3,265 kB of additional disk space will be used.
Get:1 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial/universe amd64 yum all 3.4.3-3 [486 kB]
Fetched 486 kB in 0s (12.3 MB/s)
Selecting previously unselected package yum.
(Reading database ... 110977 files and directories currently installed.)
Preparing to unpack .../archives/yum_3.4.3-3_all.deb ...
Unpacking yum (3.4.3-3) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up yum (3.4.3-3) ...
验证原来的问题已经消失
ansible@ip-172-31-38-30:~/playbook$ ansible-playbook sample.yaml -v
Using /etc/ansible/ansible.cfg as config file
PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
ok: [ub01]
TASK [Ensure NTP (for time synchronization) is installed.] *********************
fatal: [ub01]: FAILED! => {"changed": false, "failed": true, "msg": "No package matching 'ntp' found available, installed or updated", "rc": 126, "results": ["No package matching 'ntp' found available, installed or updated"]}
to retry, use: --limit @/home/ansible/playbook/sample.retry
PLAY RECAP *********************************************************************
ub01 : ok=1 changed=0 unreachable=0 failed=1
相关问题
python 的位置可能是 Ansible 和 Yum 的原因。
还要注意 /usr/bin/yum 指的是 /usr/bin/python。
#! /usr/bin/python
import sys
try:
import yum
except ImportError:
print >> sys.stderr, """\
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was: