1

I am integrating Ansible with Python and MySQL DB. A part of my use case is that given a group name to Ansible, send that group name to python which does a db read and returns a list of IPs corresponding to that group name. For a test I want to ping the returned IPs.

Here is my playbook for achieving the same:

name: run a cmd
hosts: localhost
connection: local
tasks:
  name: runs a python script with a parameter
      shell: python /pythonScripts/AnsibleDBRead.py <someGroupName>
      register: py_ret
    - set_fact:
       ip_list: "{{py_ret.stdout}}"
   - debug: var=hostvars['localhost']['ip_list']  # option to set messages here as well but not both together


name: png the hosts returned 
hosts: hostvars['localhost']['ip_list'] #this does not work
#hosts: [ "127.0.0.1", "54.147.177.9"] #this works same value but hardcoded
tasks: 
    - debug: var=hostvars['localhost']['ip_list']  # able to print the value

I am trying to set the values stored in ip_list as the hosts: for second play but without any success. The error I get is no hosts matched. Here is the output whe n run with hardcoded part commented. Ignore the formatting of the script.

PLAY [run a cmd] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [runs a python script with a parameter] ***********************************
changed: [localhost]

TASK [set_fact] ****************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "hostvars['localhost']['ip_list']": [
        "127.0.0.1", 
        "54.147.177.9"
    ]
}

PLAY [png the hosts returned] **************************************************
skipping: no hosts matched

PLAY RECAP *********************************************************************
localhost                  : ok=4    changed=1    unreachable=0    failed=0   

from what I have read I should be able to access the variable declared in one play in another play using hostvars. Any help is appreciated.

1 Answer 1

1

After some trial-and-error testing, I think this should work for you:

- name: ping the hosts returned 
  hosts: "{{ hostvars['localhost']['ip_list'] | join(',') }}"
  tasks: 
    - debug:

And it seems it's a known issue: pass array as "hosts" in playbook #16051 and workaround.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! it worked for me. Can you please explain the significance of "join" as the value returned from python was already comma separated ?
ip_list input was comma a separated string, but it got converted to a list object, so ip_list is a list object, ip_list | join(',') converts it to a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.