Role install nginx on all hosts ignoring a when condition
jjponz opened this issue · comments
Hello:
I has a problem with the rol, I use the rol like dependent of a principal rol called "some_role".
some_role has 2 groups for ansible tasks, group_a and group_b, and only must install and configure nginx in hosts that are includeds in group_a. For this reason, the tasks/main.yml of some_role is like this:
---
- block:
- name: configure some nginx vhost
include_role:
name: geerlingguy.nginx
vars:
nginx_vhosts:
- listen: "80"
server_name: "{{ base_url }}"
extra_parameters: |
hello baby
when: inventory_hostname in groups['group_a']
The playbook that run this rol is:
- hosts: group_a:group_b
remote_user: ansible
become: yes
tasks:
- name: Include role
include_role:
name: some_rol
And the inventory is:
[group_a]
machine_a
[group_b]
machine_a
machine_b
The expected result is that nginx is only installed and configured in machine_a, but the real result is that nginx is installed in both servers (machine_a and machine_b) but only configure in machine_a
I doubt if the problem if in the rol (because I don't see any pretask to install nginx in all hosts), but the result is not the expected.
Do you know why the result is install nginx in all hosts but only configure in the corrects hosts?
This is my output of ansible --version
ansible 2.6.2
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/jjponz/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609]
I put a compe version of this description in github: https://github.com/jjponz/ansible_role_issue
To execute the playbook, the virtual machines that I use are Ubuntu server 16.04.5, and the playbook is under the folder roles/some_role/tests
Regards
The way I understand it, the reason for this behaviour is due to dynamic nature of the the include (you can read more about it here: https://docs.ansible.com/ansible/2.4/playbooks_reuse.html#dynamic-vs-static. Try to use import_role
instead.
This issue has been marked 'stale' due to lack of recent activity. If there is no further activity, the issue will be closed in another 30 days. Thank you for your contribution!
Please read this blog post to see the reasons why I mark issues as stale.
This issue has been closed due to inactivity. If you feel this is in error, please reopen the issue or file a new issue with the relevant details.