ansible / ansible-runner

A tool and python library that helps when interfacing with Ansible directly or as part of another system whether that be through a container image interface, as a standalone tool, or as a Python module that can be imported. The goal is to provide a stable and consistent interface abstraction to Ansible.
Other
957 stars 352 forks source link

ansible_runner.interface.run not respecting the `limit` option #1325

Open daltschu22 opened 11 months ago

daltschu22 commented 11 months ago

Hello!

I am attempting to deploy playbooks using ansible-runner.

I am passing these options into the interface.run() module. {'private_data_dir': '/tmp', 'playbook': '/opt/rde-ansible/site.yaml', 'inventory': '/opt/rde-ansible/inventories/sandbox/hosts', 'extravars': {}, 'verbosity': 4, 'limit': 'daltschuler-rde'}

Everything seems to execute fine, the output says its targeting the correct ip of the host im trying to configure, but then it starts configuring the underlying host that im running ansible-runner on. Not deploying to the remote host im specifying in my inventory.

Here is an example of the output:

task path: /opt/rde-ansible/site.yaml:2
<10.216.136.166> ESTABLISH LOCAL CONNECTION FOR USER: root
<10.216.136.166> EXEC /bin/sh -c 'echo ~root && sleep 0'
<10.216.136.166> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir "` echo /root/.ansible/tmp/ansible-tmp-1697653060.1215596-43087-114775584520537 `" && echo ansible-tmp-1697653060.1215596-43087-114775584520537="` echo /root/.ansible/tmp/ansible-tmp-1697653060.1215596-43087-114775584520537 `" ) && sleep 0'
<daltschuler-rde> Attempting python interpreter discovery
<10.216.136.166> EXEC /bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v '"'"'python3.11'"'"'; command -v '"'"'python3.10'"'"'; command -v '"'"'python3.9'"'"'; command -v '"'"'python3.8'"'"'; command -v '"'"'python3.7'"'"'; command -v '"'"'python3.6'"'"'; command -v '"'"'python3.5'"'"'; command -v '"'"'/usr/bin/python3'"'"'; command -v '"'"'/usr/libexec/platform-python'"'"'; command -v '"'"'python2.7'"'"'; command -v '"'"'/usr/bin/python'"'"'; command -v '"'"'python'"'"'; echo ENDFOUND && sleep 0'
<10.216.136.166> EXEC /bin/sh -c '/bin/python3.11 && sleep 0'
Using module file /usr/local/lib/python3.11/site-packages/ansible/modules/setup.py
<10.216.136.166> PUT /root/.ansible/tmp/ansible-local-43083ort_twuq/tmpesbuxcek TO /root/.ansible/tmp/ansible-tmp-1697653060.1215596-43087-114775584520537/AnsiballZ_setup.py
<10.216.136.166> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1697653060.1215596-43087-114775584520537/ /root/.ansible/tmp/ansible-tmp-1697653060.1215596-43087-114775584520537/AnsiballZ_setup.py && sleep 0'
<10.216.136.166> EXEC /bin/sh -c '/usr/libexec/platform-python /root/.ansible/tmp/ansible-tmp-1697653060.1215596-43087-114775584520537/AnsiballZ_setup.py && sleep 0'
<10.216.136.166> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1697653060.1215596-43087-114775584520537/ > /dev/null 2>&1 && sleep 0'

That 10.216.136.166 is the correct ip for the remote host.

But then it starts modifying the hostname of my deployment server daltschuler_ansible_deploy, not the limited host daltschuler-rde.

changed: [daltschuler-rde] => {
    "ansible_facts": {
        "ansible_domain": "ec2.internal",
        "ansible_fqdn": "IP-X-X-X-X.ec2.internal",
        "ansible_hostname": "daltschuler-rde",
        "ansible_nodename": "daltschuler-rde"
    },
    "changed": true,
    "diff": {
        "after": "hostname = daltschuler-rde\n",
        "before": "hostname = daltschuler_ansible_deploy\n"
    },
    "invocation": {
        "module_args": {
            "name": "daltschuler-rde",
            "use": null
        }
    },
    "name": "daltschuler-rde"
}

All of these same options work just fine when im targeting the host using ansible-playbook like so: ansible-playbook -i inventories/sandbox/hosts site.yaml -l daltschuler-rde

Any help would be appreciated!

sivel commented 11 months ago

Based on that output, you have connection: local or ansible_connection: local configured somewhere. It's properly limiting to the correct host, but ansible has been told to use the local connection plugin, which will just run the tasks locally.

daltschu22 commented 11 months ago

Is it possible ansible-runner is not parsing the inventory correctly then?

Because my inventory does indeed have that setting, but not on the grouping of host that im selecting.

Example:

my-hosts:
  children:
    cloud:
      hosts:
        daltschuler-rde:
          ansible_host: X.X.X.X
          ansible_become: true
          ansible_become_user: root
ansible_deploy_server:
  vars:
    ansible_connection: local
    ansible_python_interpreter: /usr/bin/python3.11
  hosts:
    daltschuler_ansible_deploy: {}

Im omitting some of the hosts, but you can see the 2 sections are distinct. This works just fine with ansible-playbook, it doesnt pick up the var in the second section.

Also my playbook says to use the first section specifically:

- name: Run the common role across all hosts in the inventory
  hosts: my-hosts
  roles:
    - common

Or am I misunderstanding how the inventory file structure works?

Thanks for the swift reply!

sivel commented 11 months ago

It's really hard to say without an actual reproducer than we can run, or look at in it's completeness.

daltschu22 commented 11 months ago

Im reading through this section of the documentation: https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#how-variables-are-merged

If i'm reading this correctly, is it saying that the variables from my group ansible_deploy_server will be merged into the my-hosts group?

Is it possible thats whats going on? Or is this only valid if both groups contain the same variable? The only location that ansible_connection: local is in the ansible_deploy_server grouping, and thats exactly what it looks like in my file. But if thats the case, i'm still confused why it would work fine when running ansible-deploy separately.