1
votes

I have filled an issue on the ansible repository but I would like to confirm that it is really a bug and that I am not doing anything wrong. https://github.com/ansible/ansible/issues/14945

It seems that a variable defined in a group_vars is not reloaded when the host group change in the playbook.

This is my playbook using ansible 2.0.1.0

├── inventory
│   └── dev
│       ├── group_vars
│       │   ├── all
│       │   ├── backend
│       │   └── backoffice
│       └── hosts
├── playbook.yml
└── roles
    └── deploy
        └── tasks
            └── main.yml

The inventory in host :

[shop]
weenect.dev ansible_user=vagrant

[backend]
weenect.dev ansible_user=vagrant

[backoffice]
weenect.dev ansible_user=vagrant

The tasks in the deploy role executed for each host group :

---
- debug: msg="{{ deploy_source_folder }}"

And the value of this variable :

Content of inventory/dev/group_vars/all :

---
deploy_source_folder: "default"

Content of inventory/dev/group_vars/backend :

---
deploy_source_folder: "backend"

Content of inventory/dev/group_vars/backoffice :

---
deploy_source_folder: "backoffice"

When executing my playbook, the expected result would be :

Debug of deploy_source_folder for backend : "backend"
Debug of deploy_source_folder for backoffice : "backoffice"
Debug of deploy_source_folder for shop : "default"

But instead I have :

Debug of deploy_source_folder for backend : "backend"
Debug of deploy_source_folder for backoffice : "backend"
Debug of deploy_source_folder for shop : "backend"
1

1 Answers

0
votes

After some discussion with @abadger from Ansible : https://github.com/ansible/ansible/issues/14945, i understood a lot more Ansible.

Host are first class citizens. It means they are loaded one time in all your playbook. So even if your host appears in multiple inventory groups, they will have only one configuration.

The solution was to alias each host in each inventory group. For example :

[shop]
shop ansible_host=localhost

[backend]
backend ansible_host=localhost

[backoffice]
backoffice ansible_host=localhost

With this, each host will be a different citizen even if they target the same server.

Note : You can change the value of ansible_host to different hostname.