Discrepancy between no inventory and the localhost entry in an inventory #56510

@nmasse-itix

Description

SUMMARY

The default "127.0.0.1" host used by ansible when there is no inventory and a localhost ansible_connection=local entry in an explicit inventory do not lead to the same python interpreter being used.

ISSUE TYPE
  • Bug Report
  • COMPONENT NAME

    I don't know

    ANSIBLE VERSION
    ansible 2.7.10
      config file = /etc/ansible/ansible.cfg
      configured module search path = ['/Users/nmasse/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
      ansible python module location = /usr/local/Cellar/ansible/2.7.10/libexec/lib/python3.7/site-packages/ansible
      executable location = /usr/local/bin/ansible
      python version = 3.7.3 (default, Mar 27 2019, 09:23:39) [Clang 10.0.0 (clang-1000.11.45.5)]
    
    CONFIGURATION
    OS / ENVIRONMENT

    MacOS, ansible installed using brew.sh.

    STEPS TO REPRODUCE

    Install Ansible with:

    brew install ansible
    /usr/local/Cellar/ansible/2.7.10/libexec/bin/pip install openshift 

    Playbook and inventory:

    cat <<EOF > playbook.yml
    - hosts: localhost
      gather_facts: no
      pre_tasks:
      - python_requirements_facts:
          dependencies:
            - openshift
      tasks:
      - k8s:
          state: absent
          definition:
            apiVersion: v1
            kind: ConfigMap
            metadata:
              name: test
              namespace: default
            data:
    cat <<EOF > inventory
    localhost ansible_connection=local
    
    ACTUAL RESULTS

    With the default inventory, the playbook succeeds:

    $ ansible-playbook playbook.yml 
     [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
     [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 [python_requirements_facts] ***************************************************************************************************************************************************************************
    ok: [localhost]
    TASK [k8s] *************************************************************************************************************************************************************************************************
    ok: [localhost]
    PLAY RECAP *************************************************************************************************************************************************************************************************
    localhost                  : ok=2    changed=0    unreachable=0    failed=0   
    

    With an explicit inventory, the playbook fails:

    $ ansible-playbook playbook.yml -i inventory 
    PLAY [localhost] *******************************************************************************************************************************************************************************************
    TASK [python_requirements_facts] ***************************************************************************************************************************************************************************
    ok: [localhost]
    TASK [k8s] *************************************************************************************************************************************************************************************************
    fatal: [localhost]: FAILED! => {"changed": false, "msg": "This module requires the OpenShift Python client. Try `pip install openshift`"}
    	to retry, use: --limit @/Users/nmasse/tmp/test2-ansible/playbook.retry
    PLAY RECAP *************************************************************************************************************************************************************************************************
    localhost                  : ok=1    changed=0    unreachable=0    failed=1   
    

    Full output with -vvv for both commands are here:
    https://gist.github.com/nmasse-itix/77575fe8498471a10a59509dbee2fce3

    As you can see:

  • with the default inventory, the tasks are run with the same python interpreter that runs the playbook
  • with the explicit inventory, the tasks are run with the default system python interpreter
  • EXPECTED RESULTS

    I would have expected the same behavior with the default inventory as with an explicit one.

    If I called the ansible-playbook command directly from the command line, I could set the ansible_python_interpreter variable in the inventory and that would be fixed. But those playbooks are run by ansible-runner from the operator-sdk command, so I cannot change the inventory or add switches to the ansible command line.

    PROPOSED CHANGES

    For all hosts that have ansible_connection=local, the python interpreter is set to the interpreter running the playbook, unless ansible_python_interpreter is set explicitely for this host.