Use group_names to check a host belongs to group

group_names is one of magic variables from Ansible [1] which is a
list of all groups the current host is in, using this syntax will be
more readable and avoid assuming that specific group is already defined
in inventory. Another use case is if we want to run a task only if the
host does not belong to a certain group.

This also makes consitent with some usages in current code [2].

[1]
http://docs.ansible.com/ansible/latest/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts
[2]
https://github.com/openstack/openstack-ansible-os_cinder/blob/master/tasks/main.yml#L70

Change-Id: I7f85e18829cd3c9835f592ceb74f4e6460208ca6
This commit is contained in:
Cuong Nguyen 2017-11-16 15:07:46 +07:00
parent 066c95e656
commit 2bcf76646b
3 changed files with 8 additions and 8 deletions

View File

@ -62,7 +62,7 @@
state: present
notify: Ensure tgt service restarted
when:
- inventory_hostname in groups['cinder_volume']
- "cinder_services['cinder-volume']['group'] in group_names"
- cinder_backend_lvm_inuse | bool
- name: Create nfs shares export file
@ -71,8 +71,8 @@
dest: "{{ item.value.nfs_shares_config }}"
with_dict: "{{ cinder_backends | default({}) }}"
when:
- "cinder_services['cinder-volume']['group'] in group_names"
- item.value.nfs_shares_config is defined
- inventory_hostname in groups['cinder_volume']
tags:
- cinder-nfs

View File

@ -54,7 +54,7 @@
static: no
when:
- "groups['cinder_api'] | length > 0"
- "'cinder_api' in group_names"
- "cinder_services['cinder-api']['group'] in group_names"
- "inventory_hostname == ((groups['cinder_api'] | intersect(ansible_play_hosts)) | list)[0]"
tags:
- cinder-config
@ -67,7 +67,7 @@
static: no
when:
- "groups['cinder_api'] | length > 0"
- "'cinder_api' in group_names"
- "cinder_services['cinder-api']['group'] in group_names"
- "inventory_hostname == ((groups['cinder_api'] | intersect(ansible_play_hosts)) | list)[0]"
tags:
- cinder-config
@ -83,7 +83,7 @@
openstack_service_system_user: "{{ cinder_system_user_name }}"
openstack_service_venv_bin: "{{ cinder_bin }}"
when:
- "'cinder_volume' in group_names"
- "cinder_services['cinder-volume']['group'] in group_names"
- "cinder_backend_rbd_inuse | bool"
tags:
- ceph
@ -95,14 +95,14 @@
static: no
when:
- "groups['cinder_volume'] | length > 0"
- "'cinder_volume' in group_names"
- "cinder_services['cinder-volume']['group'] in group_names"
- "inventory_hostname == ((groups['cinder_volume'] | intersect(ansible_play_hosts)) | list)[0]"
tags:
- cinder-config
- include: cinder_lvm_config.yml
when:
- "'cinder_volume' in group_names"
- "cinder_services['cinder-volume']['group'] in group_names"
- "cinder_backend_lvm_inuse | bool"
tags:
- cinder-config

View File

@ -18,7 +18,7 @@ enable_v1_api = {{ cinder_enable_v1_api }}
enable_v2_api = {{ cinder_enable_v2_api }}
enable_v3_api = {{ cinder_enable_v3_api }}
{% if inventory_hostname in groups['cinder_volume'] %}
{% if cinder_services['cinder-volume']['group'] in group_names %}
{% if cinder_service_backup_program_enabled == true %}
## Cinder Backup
backup_driver = {{ cinder_service_backup_driver }}