From 2bcf76646b3cd6489b81ead502ab02d7672f3420 Mon Sep 17 00:00:00 2001 From: Cuong Nguyen Date: Thu, 16 Nov 2017 15:07:46 +0700 Subject: [PATCH] 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 --- tasks/cinder_post_install.yml | 4 ++-- tasks/main.yml | 10 +++++----- templates/cinder.conf.j2 | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tasks/cinder_post_install.yml b/tasks/cinder_post_install.yml index 048de668..ecbea02d 100644 --- a/tasks/cinder_post_install.yml +++ b/tasks/cinder_post_install.yml @@ -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 diff --git a/tasks/main.yml b/tasks/main.yml index fa81278e..846ebbb2 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -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 diff --git a/templates/cinder.conf.j2 b/templates/cinder.conf.j2 index 88707c22..22b99d30 100644 --- a/templates/cinder.conf.j2 +++ b/templates/cinder.conf.j2 @@ -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 }}