Get a variable from inaccessible inventory from a specific group

I am trying to get specific variables from a specific group, but it seems to lead me to strange results.

An example host file:

[server_machine]
zeus     account=app1

[agent_machine]

machine1   account=agent port_a=1000  port_b=1001 
machine2   account=agent port_a=1200  port_b=1201 
machine3   account=agent port_a=1300  port_b=1301 

What I'm trying to do is run a script in a group server_machinewith the parameters that I get from the group agent_machines.

Thus, the script will run 3 times on server_machinewith all port combinations.

So basically I need a playbook that might look like this:

- hosts: server_machine

- tasks:
    - command: test.py --port_1 {{item}}  --port_2 {{item}}
      with_items:{{group.agent_machine.port_a, group.agent_machine.port_b }}

However, I cannot get it to work.

+4
source share
2 answers

Try the following:

- command: test.py --port_1 {{ hostvars[item]["port_a"] }} --port_2 {{ hostvars[item]["port_b"] }}
  with_items: groups["agent_machine"]
0
source

This is a good use case for 'delegate_to' :

  - hosts: agent_machine
    tasks:
      - command: test.py --port_1 {{port_a}}  --port_2 {{port_b}}
        delegate_to: zeus
0

Source: https://habr.com/ru/post/1616594/


All Articles