1

I have my inventory file configured with group variables. Example:

all:
  children:
    europe:
      vars:
        network_id: 3
        network_name: "europe-eu"
      hosts:
        europe-eu-1254:
          ansible_host: 8.8.8.8
          ansible_ssh_pass: password
          ansible_ssh_user: user
...

I would like to get the group variables in my tasks, but I have no idea how.

Example of the task:

- name: Start latest container
  docker_container:
    name: "server-{{ hostvars[inventory_hostname].vars.network_name }}"
    image: "{{ docker_registry }}:{{ docker_tag }}"
    state: started
    recreate: yes
    network_mode: host
    oom_killer: no
    restart_policy: always
  become: yes
...

I assume, that {{ hostvars[inventory_hostname].vars.network_name }} is not the right way.

2
  • 1
    {{ network_name }} should contain it if the server is in group europe
    – Panki
    Aug 29, 2020 at 20:21
  • If so simple, then your answer I will mark as a solution. But as @Vladimir mentioned vars we should consider.
    – Maximi
    Aug 30, 2020 at 11:38

1 Answer 1

1

Simply reference the variables. For example the playbook

shell> cat playbook.yml
- hosts: all
  tasks:
    - debug:
        var: network_name
    - debug:
        msg: "{{ network_name }}"

gives (abridged)

shell> ansible-playbook playbook.yml

ok: [europe-eu-1254] => {
    "network_name": "europe-eu"
}
ok: [europe-eu-1254] => {
    "msg": "europe-eu"
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .