Đôi khi, việc đặt Trình thông dịch Python toàn cầu không phải là lý tưởng vì một số máy chủ và tác vụ có thể yêu cầu các trình thông dịch khác nhau
Chỉ định Trình thông dịch Python ở cấp Nhiệm vụ
- name: Example Task
template:
src: "test.j2"
dest: "/home/"
mode: '0600'
vars:
ansible_python_interpreter: auto
delegate_to: '{{some_remote_host}}'
Điều này sẽ chọn trình thông dịch Python thích hợp nhất. Nếu bạn muốn chỉ định một trình thông dịch Python cụ thể, bạn có thể thay đổi biến ansible_python_interpreter như sau
ansible_python_interpreter: /usr/bin/python3
Chỉ định Trình thông dịch Python ở cấp Máy chủ
vars:
ansible_python_interpreter: /usr/bin/python3
hosts:
default_python_group:
ansible_host: x.x.x.x
python_2_group:
ansible_host: x.x.x.x
ansible_python_interpreter: /usr/bin/python2
python_auto_group:
ansible_host: x.x.x.x
ansible_python_interpreter: auto
Các máy chủ trong default_python_group sẽ sử dụng trình thông dịch Python 3 [/usr/bin/python3]. Các máy chủ trong python_2_group sẽ sử dụng trình thông dịch Python 2 và các máy chủ trong python_auto_group sẽ sử dụng trình thông dịch Python thích hợp nhất cho máy chủ mà playbook chạy trên đó
Nếu bạn cố chạy Ansible với một số Ubuntu 18 mới. 04 trường hợp rất có thể bạn sẽ gặp lỗi như thế này
TASK [Gathering Facts]
fatal: [123.123.123.123]: FAILED! => {"changed": false,
"module_stderr": "/bin/sh: 1: /usr/bin/python: not found\n",
"module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 127
Điều này là do Ubuntu 18. 04 không bao gồm
- name: misc task on ubuntu 18.04 instance
hosts: "*"
tasks:
- debug: var=ansible_host
1 [Python 2] theo mặc định. Bạn có thể tự xác nhận điều này bằng cách - name: misc task on ubuntu 18.04 instance
hosts: "*"
tasks:
- debug: var=ansible_host
2 vào phiên bản và kiểm tra - name: misc task on ubuntu 18.04 instance
hosts: "*"
tasks:
- debug: var=ansible_host
3$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
Shell chỉ ra một cách hữu ích rằng chúng tôi đã cài đặt
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
7 và có thể sử dụng nó thay thế. Lý do Ansible của chúng tôi không thành công ngay cả khi đã cài đặt $ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
7 là vì Ansible vẫn cố gắng sử dụng Python 2 [_______4_______1] theo mặc địnhCác giải pháp có sẵn
Có 3 cách để giải quyết vấn đề này nếu bạn gặp phải trên máy chủ từ xa của mình
- Đặt biến
6 cho tất cả các máy chủ đã cài đặt$ which python $ python --version Command 'python' not found, but can be installed with: sudo apt install python3 sudo apt install python sudo apt install python-minimal You also have python3 installed, you can run 'python3' instead.
7 theo mặc định$ which python $ python --version Command 'python' not found, but can be installed with: sudo apt install python3 sudo apt install python sudo apt install python-minimal You also have python3 installed, you can run 'python3' instead.
- Cài đặt Python 2 bằng mô-đun
8 của Ansible$ which python $ python --version Command 'python' not found, but can be installed with: sudo apt install python3 sudo apt install python sudo apt install python-minimal You also have python3 installed, you can run 'python3' instead.
- Symlink
0 đếnPLAY [misc task on ubuntu 18.04 instance] TASK [Gathering Facts] fatal: [123.123.123.123]: FAILED! => {"changed": false, "module_stderr": "/bin/sh: 1: /usr/bin/python: not found\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 127
1 sử dụng mô-đun- name: misc task on ubuntu 18.04 instance hosts: "*" tasks: - debug: var=ansible_host
8 của Ansible$ which python $ python --version Command 'python' not found, but can be installed with: sudo apt install python3 sudo apt install python sudo apt install python-minimal You also have python3 installed, you can run 'python3' instead.
Tất cả 3 tùy chọn đều có thể được thực hiện trong Ansible mà không cần truy cập vào máy chủ, đây là tin tốt cho chúng tôi [và tự động hóa]. Trong các phần sau, tôi sẽ chỉ ra cách bạn có thể sử dụng từng tùy chọn và ưu/nhược điểm của từng tùy chọn.
Tùy chọn 1 - Đặt $ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
6 cho các máy chủ đã cài đặt $ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
7 theo mặc định
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
Đây là tùy chọn được đưa ra trong tài liệu Ansible chính thức về hỗ trợ Python 3. Các tài liệu đề xuất đặt
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
6 cho các máy chủ từ xa đã cài đặt Python 3. Chúng ta hãy kiểm tra xem?Hãy xem kết quả của việc chạy playbook sau đây với Ubuntu 18 mới. 04 trường hợp
- name: misc task on ubuntu 18.04 instance
hosts: "*"
tasks:
- debug: var=ansible_host
Chúng tôi thậm chí còn không vượt qua được nhiệm vụ
PLAY [misc task on ubuntu 18.04 instance]
TASK [Gathering Facts]
fatal: [123.123.123.123]: FAILED! => {"changed": false,
"module_stderr": "/bin/sh: 1: /usr/bin/python: not found\n",
"module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 127
7PLAY [misc task on ubuntu 18.04 instance]
TASK [Gathering Facts]
fatal: [123.123.123.123]: FAILED! => {"changed": false,
"module_stderr": "/bin/sh: 1: /usr/bin/python: not found\n",
"module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 127
Bây giờ hãy thử lại với
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
6- name: misc task on ubuntu 18.04 instance
hosts: "*"
vars:
ansible_python_interpreter: /usr/bin/python3
tasks:
- debug: var=ansible_host
Lần này không có lỗi
PLAY [misc task on ubuntu 18.04 instance]
TASK [Gathering Facts]
ok: [123.123.123.123]
TASK [debug]
ok: [123.123.123.123] => {
"ansible_host": "123.123.123.123"
}
Chúng ta có thể bao gồm biến này theo một số cách
- Nếu tất cả các máy chủ từ xa của bạn chắc chắn có
7, bạn chỉ cần thêm biến vào tệp$ which python $ python --version Command 'python' not found, but can be installed with: sudo apt install python3 sudo apt install python sudo apt install python-minimal You also have python3 installed, you can run 'python3' instead.
0 của mình- name: misc task on ubuntu 18.04 instance hosts: "*" vars: ansible_python_interpreter: /usr/bin/python3 tasks: - debug: var=ansible_host
# group_vars/all.yml
ansible_python_interpreter: /usr/bin/python3
- Nếu bạn đang sử dụng khoảng không quảng cáo động và một số máy chủ lưu trữ của bạn không cài đặt
7 theo mặc định, bạn có thể thêm các thẻ được đọc bởi tập lệnh khoảng không quảng cáo của bạn [e. g.$ which python $ python --version Command 'python' not found, but can be installed with: sudo apt install python3 sudo apt install python sudo apt install python-minimal You also have python3 installed, you can run 'python3' instead.
2] và sau đó áp dụng- name: misc task on ubuntu 18.04 instance hosts: "*" vars: ansible_python_interpreter: /usr/bin/python3 tasks: - debug: var=ansible_host
3 trong tệp- name: misc task on ubuntu 18.04 instance hosts: "*" vars: ansible_python_interpreter: /usr/bin/python3 tasks: - debug: var=ansible_host
4 dựa trên thẻ- name: misc task on ubuntu 18.04 instance hosts: "*" vars: ansible_python_interpreter: /usr/bin/python3 tasks: - debug: var=ansible_host
# group_vars/tag_OS_ubuntu1804.yml
ansible_python_interpreter: /usr/bin/python3
- Nếu bạn đang sử dụng khoảng không quảng cáo tĩnh và một số máy chủ của bạn không cài đặt
7 theo mặc định, bạn có thể thêm các biến vào các nhóm trong tệp$ which python $ python --version Command 'python' not found, but can be installed with: sudo apt install python3 sudo apt install python sudo apt install python-minimal You also have python3 installed, you can run 'python3' instead.
6 của mình- name: misc task on ubuntu 18.04 instance hosts: "*" vars: ansible_python_interpreter: /usr/bin/python3 tasks: - debug: var=ansible_host
[python2_hosts]
centos7_server
[python3_hosts]
u1804_server
[python3_hosts:vars]
ansible_python_interpreter=/usr/bin/python3
ưu
Ưu điểm lớn nhất của phương pháp này là không yêu cầu thay đổi trên chính máy chủ từ xa, chỉ cần một thay đổi tương đối nhỏ trên máy chủ cục bộ [và bất kỳ yêu cầu gắn thẻ nào]
Nhược điểm
Nhược điểm lớn của phương pháp này là bạn cần biết trước khi chạy playbook rằng máy chủ từ xa chắc chắn đã cài đặt Python 3. Nếu Python 3 không khả dụng trên máy chủ từ xa, cài đặt
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
6 sẽ khiến lượt phát của bạn không thành côngĐặt thẻ hoặc nhóm máy chủ của bạn theo việc chúng có Python 3 hay không có thể là một vấn đề lớn nếu bạn quản lý nhiều cơ sở hạ tầng
Tùy chọn 2 - Cài đặt Python 2 trên máy chủ từ xa bằng mô-đun $ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
8 của Ansible
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
Giải pháp này đòi hỏi phải tạo một lần phát riêng với
- name: misc task on ubuntu 18.04 instance
hosts: "*"
vars:
ansible_python_interpreter: /usr/bin/python3
tasks:
- debug: var=ansible_host
9 và sau đó sử dụng mô-đun $ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
8 của Ansible để chạy các lệnh trên máy chủ từ xa, bỏ qua tất cả các mô-đun Ansible- name: misc task on ubuntu 18.04 instance
hosts: "*"
vars:
ansible_python_interpreter: /usr/bin/python3
tasks:
- debug: var=ansible_host
9 sẽ ngăn Ansible chạy tác vụ PLAY [misc task on ubuntu 18.04 instance]
TASK [Gathering Facts]
fatal: [123.123.123.123]: FAILED! => {"changed": false,
"module_stderr": "/bin/sh: 1: /usr/bin/python: not found\n",
"module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 127
7, tác vụ chạy mô-đun PLAY [misc task on ubuntu 18.04 instance]
TASK [Gathering Facts]
ok: [123.123.123.123]
TASK [debug]
ok: [123.123.123.123] => {
"ansible_host": "123.123.123.123"
}
3 chống lại máy chủViệc sử dụng
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
8 rất không được khuyến khích và nó chỉ thực sự được sử dụng cho những trường hợp đặc biệt như thế này. Lưu ý rằng mô-đun $ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
8 không có "kiểm tra thay đổi" - nó sẽ chạy mỗi lần và không phải là tạm thời trừ khi các lệnh bạn đang chạy với nó là tự độngCác lệnh chúng tôi muốn chạy với
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
8 là những gì chúng tôi sẽ chạy trên máy chủ để cài đặt Python 2sudo apt-get update
sudo apt-get -y install python
Hãy biến chúng thành mệnh lệnh cho
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
8 trong vở kịch mới của chúng ta$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
0Các vở kịch trên sẽ chạy thành công và cung cấp cho bạn đầu ra tương tự như thế này
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
1Lưu ý sự vắng mặt của nhiệm vụ
PLAY [misc task on ubuntu 18.04 instance]
TASK [Gathering Facts]
fatal: [123.123.123.123]: FAILED! => {"changed": false,
"module_stderr": "/bin/sh: 1: /usr/bin/python: not found\n",
"module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 127
7 trong lần chơi đầu tiên. Nhiệm vụ trong lần chơi thứ hai thành công vì hiện đã có Python 2ưu
Chạy vở kịch này ở đầu vở kịch có nghĩa là chúng tôi không phải đặt
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
6 cho bất kỳ máy chủ từ xa nào của chúng tôi. Chúng tôi có thể điều chỉnh lối chơi để có thể chạy nó với tất cả các máy chủ và bỏ qua các lỗi nếu họ không có # group_vars/all.yml
ansible_python_interpreter: /usr/bin/python3
0 bằng cách thêm$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
2Nhược điểm
Chúng ta cần thêm vở kịch bổ sung này vào đầu mỗi vở kịch [hoặc
# group_vars/all.yml
ansible_python_interpreter: /usr/bin/python3
2 nó]. Đây là việc lặp đi lặp lại nhiều lần không cần thiết và thêm một nguồn thất bại tiềm ẩn nếu chúng ta chưa thực hiệnVì không có kiểm tra thay đổi, điều đó cũng có nghĩa là chúng tôi cần chạy toàn bộ lượt chơi mỗi khi chạy sách giải trí, điều này làm tăng thêm thời gian không cần thiết để chạy sách giải trí của chúng tôi
Tùy chọn 3 - Symlink $ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
9 sử dụng mô-đun $ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
8 của Ansible
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
Một tùy chọn khác tương tự như tùy chọn 2 là sử dụng mô-đun
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
8 để “liên kết tượng trưng” $ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
9Với một chút ma thuật shell, chúng ta có thể tạo một lệnh để thực hiện điều này một cách có điều kiện dựa trên việc liệu một trong hai tệp có tồn tại hay không bằng cách sử dụng các điều kiện
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
3Lệnh này nói
Nếu
0 tồn tại vàPLAY [misc task on ubuntu 18.04 instance] TASK [Gathering Facts] fatal: [123.123.123.123]: FAILED! => {"changed": false, "module_stderr": "/bin/sh: 1: /usr/bin/python: not found\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 127
1 không tồn tại thì hãy tạo một liên kết tượng trưng- name: misc task on ubuntu 18.04 instance hosts: "*" tasks: - debug: var=ansible_host
9$ which python $ python --version Command 'python' not found, but can be installed with: sudo apt install python3 sudo apt install python sudo apt install python-minimal You also have python3 installed, you can run 'python3' instead.
Điều này làm cho lệnh đủ linh hoạt để chạy trên tất cả các hệ thống mà không có bất kỳ
# group_vars/all.yml
ansible_python_interpreter: /usr/bin/python3
1 nào. Ví dụ: trên hệ thống CentOS 7, điều kiện sẽ đánh giá thành # group_vars/tag_OS_ubuntu1804.yml
ansible_python_interpreter: /usr/bin/python3
1 vì PLAY [misc task on ubuntu 18.04 instance]
TASK [Gathering Facts]
fatal: [123.123.123.123]: FAILED! => {"changed": false,
"module_stderr": "/bin/sh: 1: /usr/bin/python: not found\n",
"module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 127
0 không tồn tạiỞ dạng Ansible
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
4Bạn sẽ thấy đầu ra sau
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
5ưu
So với tùy chọn 2, phương pháp này nhanh hơn đáng kể và không yêu cầu chúng tôi bỏ qua các lỗi. Ngoài ra, sử dụng câu lệnh có điều kiện có nghĩa là chúng tôi chỉ tạo liên kết tượng trưng nếu
$ which python
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
7 tồn tại và - name: misc task on ubuntu 18.04 instance
hosts: "*"
tasks:
- debug: var=ansible_host
3 thì không, điều này- ngăn ngừa lỗi trong trường hợp
7 không tồn tại$ which python $ python --version Command 'python' not found, but can be installed with: sudo apt install python3 sudo apt install python sudo apt install python-minimal You also have python3 installed, you can run 'python3' instead.
- ngăn chúng tôi ghi đè
3 nếu nó đã được cài đặt- name: misc task on ubuntu 18.04 instance hosts: "*" tasks: - debug: var=ansible_host
Nhược điểm
Nhược điểm thực sự duy nhất của phương pháp này là – giống như tùy chọn 2 – chúng tôi cần thêm hoặc
# group_vars/all.yml
ansible_python_interpreter: /usr/bin/python3
2 vở kịch này ở đầu tất cả các vở kịch của chúng tôi, điều này làm tăng thêm sự trùng lặp không cần thiết và có thể là nguồn gây ra lỗiKết luận và khuyến nghị của tôi
Đây là một cái thực sự khó gọi. Mỗi tùy chọn đều có ưu điểm và nhược điểm và giải pháp tôi đề xuất thực sự phụ thuộc vào tình huống của bạn
Đối với tôi, tùy chọn 1 nói chung là vượt trội hơn vì nó không yêu cầu thay đổi sổ chơi của bạn và không yêu cầu thay đổi máy chủ từ xa theo bất kỳ cách nào
Tôi nghĩ rằng tùy chọn 1 là giải pháp tốt nhất nếu tất cả các máy chủ từ xa của bạn đang chạy một bản phân phối được cài đặt Python 3 theo mặc định [e. g. Ubuntu 16. 04 trở lên]. Đó cũng là giải pháp tốt nhất nếu bạn đã gắn thẻ cơ sở hạ tầng của mình với hệ điều hành và phiên bản hệ điều hành [e. g. thẻ AWS] hoặc nếu bạn đang sử dụng cơ sở hạ tầng tĩnh và bạn đã nhóm các máy chủ của mình theo hệ điều hành
Tôi nghĩ tùy chọn 3 là giải pháp tốt nhất nếu bạn đang xử lý các máy chủ từ xa có thể không cài đặt Python 3 theo mặc định hoặc không có cách nào dễ dàng để áp dụng
- name: misc task on ubuntu 18.04 instance
hosts: "*"
vars:
ansible_python_interpreter: /usr/bin/python3
tasks:
- debug: var=ansible_host
4 dựa trên HĐH. Lý do tùy chọn này thắng tùy chọn 2 là vì nó nhanh hơn đáng kể và có thể chạy với bất kỳ bản phân phối nào mà không cần xử lý lỗi đặc biệt nào