Vô hiệu hóa Ansible CẢNH BÁO Trình thông dịch Python

Bởi vì chúng tôi muốn Ansible có thể quản lý nhiều loại máy, mã lõi ansible hoạt động trên cả Python 2 và Python 3. Những người đóng góp cho Ansible-core và Ansible Collections nên cố gắng viết mã chạy trên các phiên bản Python giống hệt như phần còn lại của Ansible

Buộc phiên bản Python trong khoảng không quảng cáo Ansible

Phương pháp 1

Phiên bản Python nào sẽ được sử dụng trên máy khách có thể được chỉ định cho Ansible. Tham số ansible_python_interpreter có thể xác định đường dẫn/phiên bản Python tùy chỉnh có thể được xác định cho mỗi khách hàng bằng cách sử dụng tham số ansible_python_interpreter

ansible@ansibleserver:~$ grep -h sles15 inventory/*
sles15.local ansible_ssh_host=10.161.208.42 ansible_python_interpreter=/usr/bin/python3

Playbook hiện đang chạy trên máy tính SLES 15 với Python 3 và đã hoàn tất thành công

PLAY RECAP *****************************************************************************************
sles15.local    : ok=14   changed=6    unreachable=0    failed=0    skipped=24   rescued=0    ignored=0

Phương pháp 2

ĐỀ XUẤT ĐỌC

  • Hướng dẫn cho người mới bắt đầu – Python dùng để làm gì?

  • Pandas Drop Column được giải thích với các ví dụ

Trên hệ thống từ xa, yêu cầu Ansible sử dụng phiên bản trình thông dịch Python được chỉ định

Trước tiên, hãy kiểm tra hệ thống để tìm hệ điều hành từ xa bằng cách chạy lệnh sau

$ lsb_release -a

Trên hệ thống từ xa có trình thông dịch Python. Để xác thực loại hiện tại, hãy chạy lệnh sau

$ ls -l /usr/bin/python*

Playbook và khoảng không quảng cáo cho Ansible

python_interpreter_test_inventory. yml là một ví dụ về tệp kiểm kê cho Ansible

---
python_interpreter_test_default:
  hosts:
    debian10_python_default:
      ansible_host: 192.168.50.221
python_interpreter_test_defined:
  vars:
    ansible_python_interpreter: /usr/bin/python3
  hosts:
    debian10_python_group_defined:
      ansible_host: 192.168.50.221
    debian10_python_defined:
      ansible_host: 192.168.50.221
      ansible_python_interpreter: /usr/bin/python2
    debian10_python_auto:
      ansible_host: 192.168.50.221
      ansible_python_interpreter: auto
    debian10_python_auto_legacy:
      ansible_host: 192.168.50.221
      ansible_python_interpreter: auto_legacy

Tạo và kiểm tra trình thông dịch Python

Chúng tôi đang sử dụng Ansible 2. 9. 2 với sự thay đổi đối với bản đồ INTERPRETER PYTHON DISTRO MAP trong lib/ansible/config/base. yml [để hỗ trợ Debian 10] từ mặc định thành python3 trên yêu cầu hợp nhất Debian 10 #63097. ansible 2. 10 sẽ là phiên bản đầu tiên tích hợp bản cập nhật này

ĐỀ XUẤT ĐỌC

  • Hướng dẫn cho người mới bắt đầu – Python dùng để làm gì?

  • Pandas Drop Column được giải thích với các ví dụ

INTERPRETER_PYTHON:
  name: Python interpreter path [or automatic discovery behavior] used for module execution
  default: auto_legacy
  env: [{name: ANSIBLE_PYTHON_INTERPRETER}]
  ini:
  - {key: interpreter_python, section: defaults}
  vars:
  - {name: ansible_python_interpreter}
  version_added: "2.8"
  description:
  - Path to the Python interpreter to be used for module execution on remote targets, or an automatic discovery mode.
    Supported discovery modes are ``auto``, ``auto_silent``, and ``auto_legacy`` [the default]. All discovery modes
    employ a lookup table to use the included system Python [on distributions known to include one], falling back to a
    fixed ordered list of well-known Python interpreter locations if a platform-specific default is not available. The
    fallback behavior will issue a warning that the interpreter should be set explicitly [since interpreters installed
    later may change which one is used]. This warning behavior can be disabled by setting ``auto_silent``. The default
    value of ``auto_legacy`` provides all the same behavior, but for backward-compatibility with older Ansible releases
    that always defaulted to ``/usr/bin/python``, will use that interpreter if present [and issue a warning that the
    default behavior will change to that of ``auto`` in a future Ansible release.
INTERPRETER_PYTHON_DISTRO_MAP:
  name: Mapping of known included platform pythons for various Linux distros
  default:
    centos: &rhelish
      '6': /usr/bin/python
      '8': /usr/libexec/platform-python
    debian:
      '10': /usr/bin/python3
    fedora:
      '23': /usr/bin/python3
    redhat: *rhelish
    rhel: *rhelish
    ubuntu:
      '14': /usr/bin/python
      '16': /usr/bin/python3
  version_added: "2.8"
  # FUTURE: add inventory override once we're sure it can't be abused by a rogue target
  # FUTURE: add a platform layer to the map so we could use it for, e.g., freebsd/macos/etc.?
INTERPRETER_PYTHON_FALLBACK:
  name: Ordered list of Python interpreters to check for in discovery
  default:
  - /usr/bin/python
  - python3.7
  - python3.6
  - python3.5
  - python2.7
  - python2.6
  - /usr/libexec/platform-python
  - /usr/bin/python3
  - python
  # FUTURE: add inventory override once we're sure it can't be abused by a rogue target
  version_added: "2.8"

Lựa chọn mặc định cho trình thông dịch python ansible là di sản tự động kể từ Ansible 2. 8, có nghĩa là nó sẽ ưu tiên /usr/bin/python [nếu nó tồn tại] trên phiên bản Python được tìm thấy. Bạn có thể thay đổi nó thành tự động, đây sẽ là mặc định trong tương lai và sẽ hoạt động theo hướng ngược lại

Sử dụng cài đặt auto_legacy_silent hoặc auto_silent để tắt cảnh báo không dùng nữa và dự phòng. Ngoài ra, thay vào đó, hãy đặt nó thành đường dẫn của một trình thông dịch Python cụ thể. Sử dụng playbook Ansible đã đề cập trước đó để chứng minh điều này

$ ansible-playbook -i python_interpreter_test_inventory.yml python_interpreter_test_playbook.yml

PLAY [python_interpreter_test_default, python_interpreter_test_defined] ***********************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [debian10_python_default]
TASK [debug] **********************************************************************************************************************************************************************
ok: [debian10_python_default] => {
    "ansible_python_interpreter": "VARIABLE IS NOT DEFINED!"
}
TASK [debug] **********************************************************************************************************************************************************************
ok: [debian10_python_default] => {
    "ansible_python_version": "2.7.16"
}
PLAY [python_interpreter_test_default, python_interpreter_test_defined] ***********************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [debian10_python_defined]
TASK [debug] **********************************************************************************************************************************************************************
ok: [debian10_python_defined] => {
    "ansible_python_interpreter": "/usr/bin/python2"
}
TASK [debug] **********************************************************************************************************************************************************************
ok: [debian10_python_defined] => {
    "ansible_python_version": "2.7.16"
}
PLAY [python_interpreter_test_default, python_interpreter_test_defined] ***********************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [debian10_python_auto]
TASK [debug] **********************************************************************************************************************************************************************
ok: [debian10_python_auto] => {
    "ansible_python_interpreter": "auto"
}
TASK [debug] **********************************************************************************************************************************************************************
ok: [debian10_python_auto] => {
    "ansible_python_version": "3.7.3"
}
PLAY [python_interpreter_test_default, python_interpreter_test_defined] ***********************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [debian10_python_auto_legacy]
TASK [debug] **********************************************************************************************************************************************************************
ok: [debian10_python_auto_legacy] => {
    "ansible_python_interpreter": "auto_legacy"
}
TASK [debug] **********************************************************************************************************************************************************************
ok: [debian10_python_auto_legacy] => {
    "ansible_python_version": "2.7.16"
}
PLAY [python_interpreter_test_default, python_interpreter_test_defined] ***********************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [debian10_python_group_defined]
TASK [debug] **********************************************************************************************************************************************************************
ok: [debian10_python_group_defined] => {
    "ansible_python_interpreter": "/usr/bin/python3"
}
TASK [debug] **********************************************************************************************************************************************************************
ok: [debian10_python_group_defined] => {
    "ansible_python_version": "3.7.3"
}
PLAY RECAP ************************************************************************************************************************************************************************
debian10_python_auto          : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
debian10_python_auto_legacy   : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
debian10_python_default       : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
debian10_python_defined       : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
debian10_python_group_defined : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Thats tất cả để có nó

Phần kết luận

Sử dụng một danh sách hoặc tập hợp các danh sách được gọi là khoảng không quảng cáo, Ansible hoạt động đồng thời với nhiều nút hoặc “máy chủ” được quản lý trong cơ sở hạ tầng của bạn. Sau khi xác định khoảng không quảng cáo của mình, bạn sẽ sử dụng các mẫu để chọn máy chủ hoặc nhóm nào Ansible sẽ thực thi đối với

Chủ Đề