Skip to content

Instantly share code, notes, and snippets.

@zaki-lknr
Last active August 24, 2020 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaki-lknr/081be9b5df58104801c092bcbab7197f to your computer and use it in GitHub Desktop.
Save zaki-lknr/081be9b5df58104801c092bcbab7197f to your computer and use it in GitHub Desktop.
connection localになる?ならない?interpreter_pythonみてる?

環境

(2.9) [zaki@cloud-dev ansible]$ ansible --version
ansible 2.9.10
  config file = /home/zaki/tmp/ansible/ansible.cfg
  configured module search path = ['/home/zaki/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/zaki/ansible/2.9/lib/python3.6/site-packages/ansible
  executable location = /home/zaki/ansible/2.9/bin/ansible
  python version = 3.6.8 (default, Apr  2 2020, 13:34:55) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
(2.9) [zaki@cloud-dev ansible]$ ansible-config dump --only-changed
DEFAULT_STDOUT_CALLBACK(/home/zaki/tmp/ansible/ansible.cfg) = yaml
(2.9) [zaki@cloud-dev ansible]$ cat /etc/redhat-release 
CentOS Linux release 7.8.2003 (Core)
(2.9) [zaki@cloud-dev ansible]$ which ansible
~/ansible/2.9/bin/ansible

※ Ansibleはpip3でインストールしまいます。

playbook

(2.9) [zaki@cloud-dev ansible]$ cat playbook.yml 
- hosts: localhost
  become: true
  tasks:
    - name: install sl
      yum:
        name: sl
        state: present

実行結果

verboseなし

(2.9) [zaki@cloud-dev ansible]$ ansible-playbook playbook.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] *********************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************
ok: [localhost]

TASK [install sl] ********************************************************************************************************************
fatal: [localhost]: FAILED! => changed=false 
  msg: The Python 2 bindings for rpm are needed for this module. If you require Python 3 support use the `dnf` Ansible module instead.. The Python 2 yum module is needed for this module. If you require Python 3 support use the `dnf` Ansible module instead.

PLAY RECAP ***************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

-vvvあり

(2.9) [zaki@cloud-dev ansible]$ ansible-playbook playbook.yml -vvv
ansible-playbook 2.9.10
  config file = /home/zaki/tmp/ansible/ansible.cfg
  configured module search path = ['/home/zaki/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/zaki/ansible/2.9/lib/python3.6/site-packages/ansible
  executable location = /home/zaki/ansible/2.9/bin/ansible-playbook
  python version = 3.6.8 (default, Apr  2 2020, 13:34:55) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
Using /home/zaki/tmp/ansible/ansible.cfg as config file
host_list declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
script declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
auto declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
yaml declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
ini declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
toml declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAYBOOK: playbook.yml ***************************************************************************************************************
1 plays in playbook.yml

PLAY [localhost] *********************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************
task path: /home/zaki/tmp/ansible/playbook.yml:1
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: zaki
<127.0.0.1> EXEC /bin/sh -c 'echo ~zaki && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/zaki/.ansible/tmp `"&& mkdir /home/zaki/.ansible/tmp/ansible-tmp-1598277955.9973943-72218-259628780768095 && echo ansible-tmp-1598277955.9973943-72218-259628780768095="` echo /home/zaki/.ansible/tmp/ansible-tmp-1598277955.9973943-72218-259628780768095 `" ) && sleep 0'
Using module file /home/zaki/ansible/2.9/lib/python3.6/site-packages/ansible/modules/system/setup.py
<127.0.0.1> PUT /home/zaki/.ansible/tmp/ansible-local-721898r5yvgls/tmprme3x2ny TO /home/zaki/.ansible/tmp/ansible-tmp-1598277955.9973943-72218-259628780768095/AnsiballZ_setup.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/zaki/.ansible/tmp/ansible-tmp-1598277955.9973943-72218-259628780768095/ /home/zaki/.ansible/tmp/ansible-tmp-1598277955.9973943-72218-259628780768095/AnsiballZ_setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'sudo -H -S -n  -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-dibomficdxgyzvvfkvqhipizugjvrfyb ; /home/zaki/ansible/2.9/bin/python /home/zaki/.ansible/tmp/ansible-tmp-1598277955.9973943-72218-259628780768095/AnsiballZ_setup.py'"'"' && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/zaki/.ansible/tmp/ansible-tmp-1598277955.9973943-72218-259628780768095/ > /dev/null 2>&1 && sleep 0'
ok: [localhost]
META: ran handlers

TASK [install sl] ********************************************************************************************************************
task path: /home/zaki/tmp/ansible/playbook.yml:4
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: zaki
<127.0.0.1> EXEC /bin/sh -c 'echo ~zaki && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/zaki/.ansible/tmp `"&& mkdir /home/zaki/.ansible/tmp/ansible-tmp-1598277956.9452167-72316-60467475322013 && echo ansible-tmp-1598277956.9452167-72316-60467475322013="` echo /home/zaki/.ansible/tmp/ansible-tmp-1598277956.9452167-72316-60467475322013 `" ) && sleep 0'
Using module file /home/zaki/ansible/2.9/lib/python3.6/site-packages/ansible/modules/packaging/os/yum.py
<127.0.0.1> PUT /home/zaki/.ansible/tmp/ansible-local-721898r5yvgls/tmp5hjyyubu TO /home/zaki/.ansible/tmp/ansible-tmp-1598277956.9452167-72316-60467475322013/AnsiballZ_yum.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/zaki/.ansible/tmp/ansible-tmp-1598277956.9452167-72316-60467475322013/ /home/zaki/.ansible/tmp/ansible-tmp-1598277956.9452167-72316-60467475322013/AnsiballZ_yum.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'sudo -H -S -n  -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-rwzgopvvicrcorouodrfagsucewltccr ; /home/zaki/ansible/2.9/bin/python /home/zaki/.ansible/tmp/ansible-tmp-1598277956.9452167-72316-60467475322013/AnsiballZ_yum.py'"'"' && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/zaki/.ansible/tmp/ansible-tmp-1598277956.9452167-72316-60467475322013/ > /dev/null 2>&1 && sleep 0'
fatal: [localhost]: FAILED! => changed=false 
  invocation:
    module_args:
      allow_downgrade: false
      autoremove: false
      bugfix: false
      conf_file: null
      disable_excludes: null
      disable_gpg_check: false
      disable_plugin: []
      disablerepo: []
      download_dir: null
      download_only: false
      enable_plugin: []
      enablerepo: []
      exclude: []
      install_repoquery: true
      install_weak_deps: true
      installroot: /
      list: null
      lock_timeout: 30
      name:
      - sl
      releasever: null
      security: false
      skip_broken: false
      state: present
      update_cache: false
      update_only: false
      use_backend: auto
      validate_certs: true
  msg: The Python 2 bindings for rpm are needed for this module. If you require Python 3 support use the `dnf` Ansible module instead.. The Python 2 yum module is needed for this module. If you require Python 3 support use the `dnf` Ansible module instead.

PLAY RECAP ***************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

(2.9) [zaki@cloud-dev ansible]$ 

yumの結果がエラーになっているのは、OS的にPython2ベースのCentOS7ではdnfが使えず、Python3からはyumが使えないため、というのは理解しているつもりです。(ターゲット上でyum on Python2で動かせば動作するというか回避できる内容というつもり)

Ansible dnf python3 is not working with Centos 7 · Issue #67083 · ansible/ansible

ここのこの部分

This is a known limitation of the dnf module. The dnf module depends on the underlying python bindings provided by the OS. The only dnf python bindings provided by the OS are those for the default python, which is /usr/bin/python (python2).

It is not possible or feasible to use a non-default python to manage dnf.

As such, you will have to ensure that you set ansible_python_interpreter: /usr/bin/python for any task which needs to interact with things like dnf or selinux. This is a limitation of the underlying operating system and the packages it provides.

interpreter_pythonをイジってみる

(2.9) [zaki@cloud-dev ansible]$ ansible-config dump --only-changed
DEFAULT_STDOUT_CALLBACK(/home/zaki/tmp/ansible/ansible.cfg) = yaml
INTERPRETER_PYTHON(/home/zaki/tmp/ansible/ansible.cfg) = /usr/bin/perl

playbookはそのまま、インベントリファイル指定なしで以下実行

(2.9) [zaki@cloud-dev ansible]$ ansible-playbook playbook.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] *********************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************
ok: [localhost]

TASK [install sl] ********************************************************************************************************************
fatal: [localhost]: FAILED! => changed=false 
  msg: The Python 2 bindings for rpm are needed for this module. If you require Python 3 support use the `dnf` Ansible module instead.. The Python 2 yum module is needed for this module. If you require Python 3 support use the `dnf` Ansible module instead.

PLAY RECAP ***************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
(2.9) [zaki@cloud-dev ansible]$ ansible-playbook playbook.yml -vvv
ansible-playbook 2.9.10
  config file = /home/zaki/tmp/ansible/ansible.cfg
  configured module search path = ['/home/zaki/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/zaki/ansible/2.9/lib/python3.6/site-packages/ansible
  executable location = /home/zaki/ansible/2.9/bin/ansible-playbook
  python version = 3.6.8 (default, Apr  2 2020, 13:34:55) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
Using /home/zaki/tmp/ansible/ansible.cfg as config file
host_list declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
script declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
auto declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
yaml declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
ini declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
toml declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAYBOOK: playbook.yml ***************************************************************************************************************
1 plays in playbook.yml

PLAY [localhost] *********************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************
task path: /home/zaki/tmp/ansible/playbook.yml:1
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: zaki
<127.0.0.1> EXEC /bin/sh -c 'echo ~zaki && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/zaki/.ansible/tmp `"&& mkdir /home/zaki/.ansible/tmp/ansible-tmp-1598278848.420696-84588-159760650409156 && echo ansible-tmp-1598278848.420696-84588-159760650409156="` echo /home/zaki/.ansible/tmp/ansible-tmp-1598278848.420696-84588-159760650409156 `" ) && sleep 0'
Using module file /home/zaki/ansible/2.9/lib/python3.6/site-packages/ansible/modules/system/setup.py
<127.0.0.1> PUT /home/zaki/.ansible/tmp/ansible-local-84536bjgwel7n/tmp9r5u_3bs TO /home/zaki/.ansible/tmp/ansible-tmp-1598278848.420696-84588-159760650409156/AnsiballZ_setup.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/zaki/.ansible/tmp/ansible-tmp-1598278848.420696-84588-159760650409156/ /home/zaki/.ansible/tmp/ansible-tmp-1598278848.420696-84588-159760650409156/AnsiballZ_setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'sudo -H -S -n  -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-xqtilwzdrmafteirjxhgcbaqtltipnmi ; /home/zaki/ansible/2.9/bin/python /home/zaki/.ansible/tmp/ansible-tmp-1598278848.420696-84588-159760650409156/AnsiballZ_setup.py'"'"' && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/zaki/.ansible/tmp/ansible-tmp-1598278848.420696-84588-159760650409156/ > /dev/null 2>&1 && sleep 0'
ok: [localhost]
META: ran handlers

TASK [install sl] ********************************************************************************************************************
task path: /home/zaki/tmp/ansible/playbook.yml:4
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: zaki
<127.0.0.1> EXEC /bin/sh -c 'echo ~zaki && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/zaki/.ansible/tmp `"&& mkdir /home/zaki/.ansible/tmp/ansible-tmp-1598278849.384935-84690-176094529334910 && echo ansible-tmp-1598278849.384935-84690-176094529334910="` echo /home/zaki/.ansible/tmp/ansible-tmp-1598278849.384935-84690-176094529334910 `" ) && sleep 0'
Using module file /home/zaki/ansible/2.9/lib/python3.6/site-packages/ansible/modules/packaging/os/yum.py
<127.0.0.1> PUT /home/zaki/.ansible/tmp/ansible-local-84536bjgwel7n/tmpffwcvtwl TO /home/zaki/.ansible/tmp/ansible-tmp-1598278849.384935-84690-176094529334910/AnsiballZ_yum.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/zaki/.ansible/tmp/ansible-tmp-1598278849.384935-84690-176094529334910/ /home/zaki/.ansible/tmp/ansible-tmp-1598278849.384935-84690-176094529334910/AnsiballZ_yum.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'sudo -H -S -n  -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-fwvlnbhobfyoawgzqvutdpimgvpzbifb ; /home/zaki/ansible/2.9/bin/python /home/zaki/.ansible/tmp/ansible-tmp-1598278849.384935-84690-176094529334910/AnsiballZ_yum.py'"'"' && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/zaki/.ansible/tmp/ansible-tmp-1598278849.384935-84690-176094529334910/ > /dev/null 2>&1 && sleep 0'
fatal: [localhost]: FAILED! => changed=false 
  invocation:
    module_args:
      allow_downgrade: false
      autoremove: false
      bugfix: false
      conf_file: null
      disable_excludes: null
      disable_gpg_check: false
      disable_plugin: []
      disablerepo: []
      download_dir: null
      download_only: false
      enable_plugin: []
      enablerepo: []
      exclude: []
      install_repoquery: true
      install_weak_deps: true
      installroot: /
      list: null
      lock_timeout: 30
      name:
      - sl
      releasever: null
      security: false
      skip_broken: false
      state: present
      update_cache: false
      update_only: false
      use_backend: auto
      validate_certs: true
  msg: The Python 2 bindings for rpm are needed for this module. If you require Python 3 support use the `dnf` Ansible module instead.. The Python 2 yum module is needed for this module. If you require Python 3 support use the `dnf` Ansible module instead.

PLAY RECAP ***************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

(2.9) [zaki@cloud-dev ansible]$ 

playbookの

- hosts: localhost

指定だとinterpreter_pythonの指定を見ていないような…?

inventory

[hosts]
local ansible_host=localhost ansible_connection=local #ansible_python_interpreter=/usr/bin/python

playbook

- hosts: hosts
  become: true
  tasks:
    - name: install sl
      yum:
        name: sl
        state: present

ansible.cfg

[defaults]
stdout_callback = yaml
#interpreter_python=/usr/bin/python2
interpreter_python=/usr/bin/false

(perlだとぐちゃぐちゃなのでfalseで笑)

実行結果

(2.9) [zaki@cloud-dev ansible]$ ansible-playbook playbook.yml -i inventory.ini 

PLAY [hosts] *************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************
fatal: [local]: FAILED! => changed=false 
  ansible_facts: {}
  failed_modules:
    setup:
      failed: true
      module_stderr: ''
      module_stdout: ''
      msg: |-
        MODULE FAILURE
        See stdout/stderr for the exact error
      rc: 1
  msg: |-
    The following modules failed to execute: setup

PLAY RECAP ***************************************************************************************************************************
local                      : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

これだと/usr/bin/false使っているようです

(2.9) [zaki@cloud-dev ansible]$ ansible-playbook playbook.yml -i inventory.ini -vvv
ansible-playbook 2.9.10
  config file = /home/zaki/tmp/ansible/ansible.cfg
  configured module search path = ['/home/zaki/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/zaki/ansible/2.9/lib/python3.6/site-packages/ansible
  executable location = /home/zaki/ansible/2.9/bin/ansible-playbook
  python version = 3.6.8 (default, Apr  2 2020, 13:34:55) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
Using /home/zaki/tmp/ansible/ansible.cfg as config file
host_list declined parsing /home/zaki/tmp/ansible/inventory.ini as it did not pass its verify_file() method
script declined parsing /home/zaki/tmp/ansible/inventory.ini as it did not pass its verify_file() method
auto declined parsing /home/zaki/tmp/ansible/inventory.ini as it did not pass its verify_file() method
yaml declined parsing /home/zaki/tmp/ansible/inventory.ini as it did not pass its verify_file() method
Parsed /home/zaki/tmp/ansible/inventory.ini inventory source with ini plugin

PLAYBOOK: playbook.yml ***************************************************************************************************************
1 plays in playbook.yml

PLAY [hosts] *************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************
task path: /home/zaki/tmp/ansible/playbook.yml:1
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: zaki
<localhost> EXEC /bin/sh -c 'echo ~zaki && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/zaki/.ansible/tmp `"&& mkdir /home/zaki/.ansible/tmp/ansible-tmp-1598279254.7245605-90204-16937373869534 && echo ansible-tmp-1598279254.7245605-90204-16937373869534="` echo /home/zaki/.ansible/tmp/ansible-tmp-1598279254.7245605-90204-16937373869534 `" ) && sleep 0'
Using module file /home/zaki/ansible/2.9/lib/python3.6/site-packages/ansible/modules/system/setup.py
<localhost> PUT /home/zaki/.ansible/tmp/ansible-local-90195272cli5b/tmp4k0rn1mb TO /home/zaki/.ansible/tmp/ansible-tmp-1598279254.7245605-90204-16937373869534/AnsiballZ_setup.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/zaki/.ansible/tmp/ansible-tmp-1598279254.7245605-90204-16937373869534/ /home/zaki/.ansible/tmp/ansible-tmp-1598279254.7245605-90204-16937373869534/AnsiballZ_setup.py && sleep 0'
<localhost> EXEC /bin/sh -c 'sudo -H -S -n  -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-bxbeodbbqvlgrnxezmmnwahxwzmuypas ; /usr/bin/false /home/zaki/.ansible/tmp/ansible-tmp-1598279254.7245605-90204-16937373869534/AnsiballZ_setup.py'"'"' && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /home/zaki/.ansible/tmp/ansible-tmp-1598279254.7245605-90204-16937373869534/ > /dev/null 2>&1 && sleep 0'
fatal: [local]: FAILED! => changed=false 
  ansible_facts: {}
  failed_modules:
    setup:
      failed: true
      module_stderr: ''
      module_stdout: ''
      msg: |-
        MODULE FAILURE
        See stdout/stderr for the exact error
      rc: 1
  msg: |-
    The following modules failed to execute: setup

PLAY RECAP ***************************************************************************************************************************
local                      : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

(2.9) [zaki@cloud-dev ansible]$ 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment