Lỗi trình thông dịch Python trong Ansible

Hãy cùng nhau khắc phục lỗi nghiêm trọng của Ansible “Không thể nhập thư viện Python cần thiết (botocore hoặc boto3)” để tìm ra nguyên nhân gốc rễ, cài đặt thư viện cần thiết bằng PIP và chạy thành công mã Ansible For AWS Playbook của chúng tôi

Hôm nay chúng ta sẽ nói về cách khắc phục sự cố Ansible, cụ thể là về thông báo “Không thể nhập thư viện Python được yêu cầu (

$ ansible --version
ansible [core 2.12.2]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/devops/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.8/site-packages/ansible
ansible collection location = /home/devops/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.8.12 (default, Sep 16 2021, 10:46:05) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
jinja version = 2.10.3
libyaml = True
[devops@demo aws]$ python --version
-bash: python: command not found
[devops@demo aws]$ python3 --version
Python 3.6.8
[devops@demo aws]$ python3.8 --version
Python 3.8.12
[devops@demo aws]$ whereis python3.8
python3: /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3 /usr/bin/python3.8 /usr/lib/python3.6 /usr/lib/python3.8 /usr/lib64/python3.6 /usr/lib64/python3.8 /usr/local/lib/python3.8 /usr/include/python3.6m /usr/include/python3.8 /usr/share/man/man1/python3.1.gz
0 hoặc
$ ansible --version
ansible [core 2.12.2]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/devops/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.8/site-packages/ansible
ansible collection location = /home/devops/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.8.12 (default, Sep 16 2021, 10:46:05) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
jinja version = 2.10.3
libyaml = True
[devops@demo aws]$ python --version
-bash: python: command not found
[devops@demo aws]$ python3 --version
Python 3.6.8
[devops@demo aws]$ python3.8 --version
Python 3.8.12
[devops@demo aws]$ whereis python3.8
python3: /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3 /usr/bin/python3.8 /usr/lib/python3.6 /usr/lib/python3.8 /usr/lib64/python3.6 /usr/lib64/python3.8 /usr/local/lib/python3.8 /usr/include/python3.6m /usr/include/python3.8 /usr/share/man/man1/python3.1.gz
1)” và kích hoạt Ansible cho AWS

Thông báo lỗi nghiêm trọng này xảy ra khi chúng tôi đang cố thực thi một số mã đối với Cơ sở hạ tầng AWS EC2 của bạn mà không có thư viện Python cần thiết cho AWS

Những trường hợp này thường liên quan đến cấu hình nút Ansible Controller của bạn và thường không liên quan đến Ansible Playbook

Tôi là Luca Berton và chào mừng đến với tập Ansible Pilot hôm nay

thử nghiệm

Cách tốt nhất để nói về khắc phục sự cố Ansible là nhảy vào một bản demo trực tiếp để cho bạn thấy một cách thực tế "Không thể nhập thư viện Python bắt buộc (

$ ansible --version
ansible [core 2.12.2]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/devops/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.8/site-packages/ansible
ansible collection location = /home/devops/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.8.12 (default, Sep 16 2021, 10:46:05) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
jinja version = 2.10.3
libyaml = True
[devops@demo aws]$ python --version
-bash: python: command not found
[devops@demo aws]$ python3 --version
Python 3.6.8
[devops@demo aws]$ python3.8 --version
Python 3.8.12
[devops@demo aws]$ whereis python3.8
python3: /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3 /usr/bin/python3.8 /usr/lib/python3.6 /usr/lib/python3.8 /usr/lib64/python3.6 /usr/lib64/python3.8 /usr/local/lib/python3.8 /usr/include/python3.6m /usr/include/python3.8 /usr/share/man/man1/python3.1.gz
0 hoặc
$ ansible --version
ansible [core 2.12.2]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/devops/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.8/site-packages/ansible
ansible collection location = /home/devops/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.8.12 (default, Sep 16 2021, 10:46:05) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
jinja version = 2.10.3
libyaml = True
[devops@demo aws]$ python --version
-bash: python: command not found
[devops@demo aws]$ python3 --version
Python 3.6.8
[devops@demo aws]$ python3.8 --version
Python 3.8.12
[devops@demo aws]$ whereis python3.8
python3: /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3 /usr/bin/python3.8 /usr/lib/python3.6 /usr/lib/python3.8 /usr/lib64/python3.6 /usr/lib64/python3.8 /usr/local/lib/python3.8 /usr/include/python3.6m /usr/include/python3.8 /usr/share/man/man1/python3.1.gz
1)" và cách giải quyết vấn đề đó

Trong bản demo này, tôi sẽ tạo lại lỗi và sửa lỗi bằng PIP, Trình quản lý gói Python trên máy demo

thực thi lỗi

$ ansible-playbook ami_search.yml 
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [AMI search] *********************************************************************************TASK [search for AMI] ********************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (botocore or boto3) on demo.example.com's Python /usr/bin/python3.8. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}
PLAY RECAP ****************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

sửa mã

  • phiên bản trăn

Bước đầu tiên là xác định phiên bản Python của bạn (3. 8 trong ví dụ này)

$ ansible --version
ansible [core 2.12.2]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/devops/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.8/site-packages/ansible
ansible collection location = /home/devops/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.8.12 (default, Sep 16 2021, 10:46:05) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
jinja version = 2.10.3
libyaml = True
[devops@demo aws]$ python --version
-bash: python: command not found
[devops@demo aws]$ python3 --version
Python 3.6.8
[devops@demo aws]$ python3.8 --version
Python 3.8.12
[devops@demo aws]$ whereis python3.8
python3: /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3 /usr/bin/python3.8 /usr/lib/python3.6 /usr/lib/python3.8 /usr/lib64/python3.6 /usr/lib64/python3.8 /usr/local/lib/python3.8 /usr/include/python3.6m /usr/include/python3.8 /usr/share/man/man1/python3.1.gz
  • trước

Hãy sử dụng lệnh

$ ansible --version
ansible [core 2.12.2]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/devops/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.8/site-packages/ansible
ansible collection location = /home/devops/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.8.12 (default, Sep 16 2021, 10:46:05) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
jinja version = 2.10.3
libyaml = True
[devops@demo aws]$ python --version
-bash: python: command not found
[devops@demo aws]$ python3 --version
Python 3.6.8
[devops@demo aws]$ python3.8 --version
Python 3.8.12
[devops@demo aws]$ whereis python3.8
python3: /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3 /usr/bin/python3.8 /usr/lib/python3.6 /usr/lib/python3.8 /usr/lib64/python3.6 /usr/lib64/python3.8 /usr/local/lib/python3.8 /usr/include/python3.6m /usr/include/python3.8 /usr/share/man/man1/python3.1.gz
0 vì chúng tôi đang chạy python 3. 8. Trong các bản phân phối Linux khác, bạn có thể cần chỉ định các phiên bản Python khác nhau. Ví dụ Python3. 9 sử dụng
$ ansible --version
ansible [core 2.12.2]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/devops/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.8/site-packages/ansible
ansible collection location = /home/devops/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.8.12 (default, Sep 16 2021, 10:46:05) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
jinja version = 2.10.3
libyaml = True
[devops@demo aws]$ python --version
-bash: python: command not found
[devops@demo aws]$ python3 --version
Python 3.6.8
[devops@demo aws]$ python3.8 --version
Python 3.8.12
[devops@demo aws]$ whereis python3.8
python3: /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3 /usr/bin/python3.8 /usr/lib/python3.6 /usr/lib/python3.8 /usr/lib64/python3.6 /usr/lib64/python3.8 /usr/local/lib/python3.8 /usr/include/python3.6m /usr/include/python3.8 /usr/share/man/man1/python3.1.gz
1 ,
$ ansible --version
ansible [core 2.12.2]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/devops/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.8/site-packages/ansible
ansible collection location = /home/devops/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.8.12 (default, Sep 16 2021, 10:46:05) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
jinja version = 2.10.3
libyaml = True
[devops@demo aws]$ python --version
-bash: python: command not found
[devops@demo aws]$ python3 --version
Python 3.6.8
[devops@demo aws]$ python3.8 --version
Python 3.8.12
[devops@demo aws]$ whereis python3.8
python3: /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3 /usr/bin/python3.8 /usr/lib/python3.6 /usr/lib/python3.8 /usr/lib64/python3.6 /usr/lib64/python3.8 /usr/local/lib/python3.8 /usr/include/python3.6m /usr/include/python3.8 /usr/share/man/man1/python3.1.gz
2 hoặc chỉ
$ ansible --version
ansible [core 2.12.2]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/devops/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.8/site-packages/ansible
ansible collection location = /home/devops/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.8.12 (default, Sep 16 2021, 10:46:05) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
jinja version = 2.10.3
libyaml = True
[devops@demo aws]$ python --version
-bash: python: command not found
[devops@demo aws]$ python3 --version
Python 3.6.8
[devops@demo aws]$ python3.8 --version
Python 3.8.12
[devops@demo aws]$ whereis python3.8
python3: /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3 /usr/bin/python3.8 /usr/lib/python3.6 /usr/lib/python3.8 /usr/lib64/python3.6 /usr/lib64/python3.8 /usr/local/lib/python3.8 /usr/include/python3.6m /usr/include/python3.8 /usr/share/man/man1/python3.1.gz
3

[root@demo aws]# pip3.8 list | grep boto
  • cố định với PIP
[root@demo aws]# pip3.8 install boto3
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3.8 install --user` instead.
Collecting boto3
Downloading https://files.pythonhosted.org/packages/d4/c0/59513bab408fe6dd366f55c5e6ea4128daffe501fa8986749932219bdd59/boto3-1.24.27-py3-none-any.whl (132kB)
|████████████████████████████████| 133kB 616kB/s
Collecting jmespath<2.0.0,>=0.7.1
Downloading https://files.pythonhosted.org/packages/31/b4/b9b800c45527aadd64d5b442f9b932b00648617eb5d63d2c7a6587b7cafc/jmespath-1.0.1-py3-none-any.whl
Collecting botocore<1.28.0,>=1.27.27
Downloading https://files.pythonhosted.org/packages/20/7a/06940ad3f89e19ef065dd2600b14148673efe775da4fa61f39b7c63724a8/botocore-1.27.27-py3-none-any.whl (9.0MB)
|████████████████████████████████| 9.0MB 2.5MB/s
Collecting s3transfer<0.7.0,>=0.6.0
Downloading https://files.pythonhosted.org/packages/5e/c6/af903b5fab3f9b5b1e883f49a770066314c6dcceb589cf938d48c89556c1/s3transfer-0.6.0-py3-none-any.whl (79kB)
|████████████████████████████████| 81kB 3.2MB/s
Collecting python-dateutil<3.0.0,>=2.1
Downloading https://files.pythonhosted.org/packages/36/7a/87837f39d0296e723bb9b62bbb257d0355c7f6128853c78955f57342a56d/python_dateutil-2.8.2-py2.py3-none-any.whl (247kB)
|████████████████████████████████| 256kB 4.9MB/s
Requirement already satisfied: urllib3<1.27,>=1.25.4 in /usr/local/lib/python3.8/site-packages (from botocore<1.28.0,>=1.27.27->boto3) (1.26.9)
Requirement already satisfied: six>=1.5 in /usr/lib/python3.8/site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.28.0,>=1.27.27->boto3) (1.12.0)
Installing collected packages: jmespath, python-dateutil, botocore, s3transfer, boto3
Successfully installed boto3-1.24.27 botocore-1.27.27 jmespath-1.0.1 python-dateutil-2.8.2 s3transfer-0.6.0
  • sau đó
________số 8

sửa lỗi thực thi

$ ansible-playbook ami_search.yml 
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [AMI search] *********************************************************************************TASK [search for existing AMI] ********************************************************************
ok: [localhost]
TASK [debug] **************************************************************************************
ok: [localhost] => {
"ami_found": {
"changed": false,
"failed": false,
"images": [
{
"architecture": "x86_64",
"block_device_mappings": [
{
"device_name": "/dev/sda1",
"ebs": {
"delete_on_termination": true,
"encrypted": false,
"snapshot_id": "snap-03f2e24f30f580353",
"volume_size": 10,
"volume_type": "gp2"
}
}
],
"creation_date": "2020-11-02T11:01:38.000Z",
"deprecation_time": "2022-11-02T11:01:38.000Z",
"description": "Provided by Red Hat, Inc.",
"ena_support": true,
"hypervisor": "xen",
"image_id": "ami-096fda3c22c1c990a",
"image_location": "309956199498/RHEL-8.3.0_HVM-20201031-x86_64-0-Hourly2-GP2",
"image_type": "machine",
"name": "RHEL-8.3.0_HVM-20201031-x86_64-0-Hourly2-GP2",
"owner_id": "309956199498",
"platform_details": "Red Hat Enterprise Linux",
"public": true,
"root_device_name": "/dev/sda1",
"root_device_type": "ebs",
"sriov_net_support": "simple",
"state": "available",
"tags": {},
"usage_operation": "RunInstances:0010",
"virtualization_type": "hvm"
}
]
}
}
PLAY RECAP ****************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

GitHub - lucab85/ansible-pilot. Kho mã kênh YouTube Ansible Pilot

Bạn không thể thực hiện hành động đó vào lúc này. Bạn đã đăng nhập bằng tab hoặc cửa sổ khác. Bạn đã đăng xuất trong một tab khác hoặc…

github. com

Tóm tắt lại

Bây giờ bạn đã biết rõ hơn cách khắc phục sự cố thông báo Ansible “Không thể nhập thư viện Python được yêu cầu (botocore hoặc boto3)”

Tôi hy vọng bạn thích đọc này. Nếu bạn muốn hỗ trợ tôi với tư cách là một nhà văn, hãy cân nhắc đăng ký để trở thành Thành viên trung bình. Chỉ với 5 đô la một tháng và bạn có quyền truy cập không giới hạn vào Phương tiện

Đăng ký kênh YouTube, Medium và Website để không bỏ lỡ các tập tiếp theo của Ansible Pilot

Làm cách nào để đặt trình thông dịch Python cho Ansible?

Để định cấu hình rõ ràng trình thông dịch Python 3, đặt biến khoảng không quảng cáo ansible_python_interpreter ở cấp nhóm hoặc máy chủ thành vị trí của trình thông dịch Python 3 . Đường dẫn trình thông dịch mặc định cũng có thể được đặt trong ansible. cfg.

Ansible sử dụng phiên bản Python nào?

Hiện tại Ansible có thể chạy từ bất kỳ máy nào có Python 2 (phiên bản 2. 7) hoặc Python 3 (phiên bản 3. 5 trở lên) đã cài đặt. Windows không được hỗ trợ cho máy điều khiển. Điều này bao gồm Red Hat, Debian, CentOS, macOS, bất kỳ BSD nào, v.v.

Làm cách nào để cài đặt pip trong Ansible?

Cài đặt Mô-đun Ansible Python pip trong Playbook .
Mở thiết bị đầu cuối trong máy chủ bộ điều khiển Ansible của bạn và chạy các lệnh sau để tạo thư mục có tên ~/ansible_python_pip_module và chuyển sang thư mục đó. .
Tiếp theo, tạo một tệp có tên là pip. .
Chạy lệnh ansible-playbook bên dưới để thực thi pip

Làm cách nào để cài đặt Python bằng playbook Ansible?

Tạo playbook Ansible để cài đặt python . Khi chúng ta cần cài đặt python, điều này phải được chỉ định trong phần pre-task của playbook . Bằng cách chỉ định bất kỳ công việc trước nào trong pre_task, trước tiên nó sẽ thực thi pre_task sau khi kết nối với máy chủ lưu trữ.