0

Actually I have group_vars, I need to merge those vars, can you help me.

group_vars/eu/main.yaml
mgmt_routes:
 Tok:
  -  ip: 172.22.203.253
     netmask: 255.255.255.255 

group_vars/all/main.yaml     
mgmt_routes:
 all:
  -  ip: 172.18.0.70
     netmask: 255.255.255.255 
  -  ip: 172.18.3.50
     netmask: 255.255.255.255 

How can i integrate those variables and How can i call merged variables in playbook

Charan Adabala
  • 181
  • 2
  • 10
  • 1
    Possible duplicate of [In Ansible, how to combine variables from separate files into one array?](https://stackoverflow.com/questions/35554415/in-ansible-how-to-combine-variables-from-separate-files-into-one-array) – Augusto Jun 22 '19 at 15:54
  • It's not a duplicate. The difference is "vars" vs. "group_vars". The use-case of group_vars is completely different. The problem is solved with "hostvars". Accepted solution to the problem with "vars" is "to write your own vars plugin". – Vladimir Botka Jun 22 '19 at 16:33

2 Answers2

1

Let's simplify the data

$ cat group_vars/all/main.yml 
mgmt_routes:
 all: ALL ROUTES
$ cat group_vars/eu/main.yml 
mgmt_routes:
 Tok: TOK ROUTES

With the inventory file

[eu]
test_01

[us]
test_02

The plays below

- hosts: test_01
  tasks:
    - debug:
        var: mgmt_routes

- hosts: test_02
  tasks:
    - debug:
        var: mgmt_routes
    - set_fact:
        all_routes_list: "{{ hostvars|
                             json_query('*.mgmt_routes')|
                             unique }}"
    - set_fact:
        all_routes_dict: "{{ all_routes_dict|
                             default({})|
                             combine(item) }}"
      loop: "{{ all_routes_list }}"
    - debug:
        var: all_routes_dict

give (abridged)

ok: [test_01] => {
    "mgmt_routes": {
        "Tok": "TOK ROUTES"
    }
}

ok: [test_02] => {
    "mgmt_routes": {
        "all": "ALL ROUTES"
    }
}

ok: [test_02] => {
    "all_routes_dict": {
        "Tok": "TOK ROUTES", 
        "all": "ALL ROUTES"
    }
}

Is this what you're looking for?

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
0

Thanks for replying to my post, but actually I have to work on group_vars not on host vars.

Charan Adabala
  • 181
  • 2
  • 10