Work around issue with delegate_to not respecting ansible_user

When using delegate_to with an IP address, ansible does use the corresponding
host in the inventory, and so not respect the ansible_user variable of the
delegate host. Here we revert to using the delegate host's inventory hostname,
and force ansible to respect the ansible_host variable of that host by setting
the variable in the task explicitly.
This commit is contained in:
Mark Goddard 2017-08-14 10:27:23 +00:00
parent 39a37a1647
commit 1e7502000e
6 changed files with 106 additions and 76 deletions

View File

@ -5,7 +5,7 @@
hosts: compute hosts: compute
gather_facts: no gather_facts: no
vars: vars:
delegate_host: "{{ groups['controllers'][0] }}" controller_host: "{{ groups['controllers'][0] }}"
tasks: tasks:
- name: Ensure ipmitool is installed - name: Ensure ipmitool is installed
yum: yum:
@ -13,15 +13,19 @@
state: installed state: installed
become: True become: True
run_once: True run_once: True
delegate_to: "{{ item }}" delegate_to: "{{ controller_host }}"
with_items: vars:
- "{{ hostvars[delegate_host].ansible_host }}" # NOTE: Without this, the controller's ansible_host variable will not
# be respected when using delegate_to.
ansible_host: "{{ hostvars[controller_host].ansible_host | default(controller_host) }}"
- name: Ensure compute nodes are powered off - name: Ensure compute nodes are powered off
command: ipmitool -U {{ ipmi_username }} -P {{ ipmi_password }} -H {{ ipmi_address }} -I lanplus chassis power off command: ipmitool -U {{ ipmi_username }} -P {{ ipmi_password }} -H {{ ipmi_address }} -I lanplus chassis power off
delegate_to: "{{ item }}" delegate_to: "{{ controller_host }}"
with_items: vars:
- "{{ hostvars[delegate_host].ansible_host }}" # NOTE: Without this, the controller's ansible_host variable will not
# be respected when using delegate_to.
ansible_host: "{{ hostvars[controller_host].ansible_host | default(controller_host) }}"
- name: Pause to prevent overwhelming BMCs - name: Pause to prevent overwhelming BMCs
pause: pause:
@ -29,9 +33,11 @@
- name: Ensure compute nodes are set to boot via PXE - name: Ensure compute nodes are set to boot via PXE
command: ipmitool -U {{ ipmi_username }} -P {{ ipmi_password }} -H {{ ipmi_address }} -I lanplus chassis bootdev pxe command: ipmitool -U {{ ipmi_username }} -P {{ ipmi_password }} -H {{ ipmi_address }} -I lanplus chassis bootdev pxe
delegate_to: "{{ item }}" delegate_to: "{{ controller_host }}"
with_items: vars:
- "{{ hostvars[delegate_host].ansible_host }}" # NOTE: Without this, the controller's ansible_host variable will not
# be respected when using delegate_to.
ansible_host: "{{ hostvars[controller_host].ansible_host | default(controller_host) }}"
- name: Pause to prevent overwhelming BMCs - name: Pause to prevent overwhelming BMCs
pause: pause:
@ -39,6 +45,8 @@
- name: Ensure compute nodes are powered on - name: Ensure compute nodes are powered on
command: ipmitool -U {{ ipmi_username }} -P {{ ipmi_password }} -H {{ ipmi_address }} -I lanplus chassis power on command: ipmitool -U {{ ipmi_username }} -P {{ ipmi_password }} -H {{ ipmi_address }} -I lanplus chassis power on
delegate_to: "{{ item }}" delegate_to: "{{ controller_host }}"
with_items: vars:
- "{{ hostvars[delegate_host].ansible_host }}" # NOTE: Without this, the controller's ansible_host variable will not
# be respected when using delegate_to.
ansible_host: "{{ hostvars[controller_host].ansible_host | default(controller_host) }}"

View File

@ -45,7 +45,9 @@
# Bifrost host variables for {{ inventory_hostname }} # Bifrost host variables for {{ inventory_hostname }}
{{ bifrost_hostvars | to_nice_yaml }} {{ bifrost_hostvars | to_nice_yaml }}
dest: "/etc/kolla/bifrost/inventory/host_vars/{{ inventory_hostname }}" dest: "/etc/kolla/bifrost/inventory/host_vars/{{ inventory_hostname }}"
delegate_to: "{{ item }}" delegate_to: "{{ seed_host }}"
with_items: vars:
- "{{ hostvars[seed_host].ansible_host }}" # NOTE: Without this, the seed's ansible_host variable will not be
# respected when using delegate_to.
ansible_host: "{{ hostvars[seed_host].ansible_host | default(seed_host) }}"
become: True become: True

View File

@ -53,6 +53,7 @@
vars: vars:
# Set this to False to avoid rebooting the nodes after configuration. # Set this to False to avoid rebooting the nodes after configuration.
drac_reboot: True drac_reboot: True
seed_host: "{{ groups['seed'][0] }}"
pre_tasks: pre_tasks:
- name: Set the overcloud nodes' maintenance mode - name: Set the overcloud nodes' maintenance mode
command: > command: >
@ -69,12 +70,11 @@
--limit {{ inventory_hostname }} --limit {{ inventory_hostname }}
-m command -m command
-a "openstack baremetal node maintenance set {% raw %}{{ inventory_hostname }}{% endraw %} --reason BIOS-RAID"' -a "openstack baremetal node maintenance set {% raw %}{{ inventory_hostname }}{% endraw %} --reason BIOS-RAID"'
register: show_result delegate_to: "{{ seed_host }}"
# We use this convoluted construct to work around Ansible's limitations vars:
# in evaluation of the delegate_to keyword. # NOTE: Without this, the seed's ansible_host variable will not be
delegate_to: "{{ item }}" # respected when using delegate_to.
with_items: ansible_host: "{{ hostvars[seed_host].ansible_host | default(seed_host) }}"
- "{{ hostvars[groups['seed'][0]].ansible_host }}"
when: "{{ bios_or_raid_change | bool }}" when: "{{ bios_or_raid_change | bool }}"
roles: roles:
@ -102,10 +102,9 @@
--limit {{ inventory_hostname }} --limit {{ inventory_hostname }}
-m command -m command
-a "openstack baremetal node maintenance unset {% raw %}{{ inventory_hostname }}{% endraw %}"' -a "openstack baremetal node maintenance unset {% raw %}{{ inventory_hostname }}{% endraw %}"'
register: show_result delegate_to: "{{ seed_host }}"
# We use this convoluted construct to work around Ansible's limitations vars:
# in evaluation of the delegate_to keyword. # NOTE: Without this, the seed's ansible_host variable will not be
delegate_to: "{{ item }}" # respected when using delegate_to.
with_items: ansible_host: "{{ hostvars[seed_host].ansible_host | default(seed_host) }}"
- "{{ hostvars[groups['seed'][0]].ansible_host }}"
when: "{{ bios_or_raid_change | bool }}" when: "{{ bios_or_raid_change | bool }}"

View File

@ -26,6 +26,7 @@
# Retries to use when using Ironic API and hitting node locked errors. # Retries to use when using Ironic API and hitting node locked errors.
ironic_retries: 6 ironic_retries: 6
ironic_retry_interval: 5 ironic_retry_interval: 5
seed_host: "{{ groups['seed'][0] }}"
gather_facts: no gather_facts: no
tasks: tasks:
- name: Check the ironic node's initial provision state - name: Check the ironic node's initial provision state
@ -45,15 +46,15 @@
-a "openstack baremetal node show {% raw %}{{ inventory_hostname }}{% endraw %} -f value -c provision_state"' -a "openstack baremetal node show {% raw %}{{ inventory_hostname }}{% endraw %} -f value -c provision_state"'
register: show_result register: show_result
changed_when: False changed_when: False
# We use this convoluted construct to work around Ansible's limitations delegate_to: "{{ seed_host }}"
# in evaluation of the delegate_to keyword. vars:
delegate_to: "{{ item }}" # NOTE: Without this, the seed's ansible_host variable will not be
with_items: # respected when using delegate_to.
- "{{ hostvars[groups['seed'][0]].ansible_host }}" ansible_host: "{{ hostvars[seed_host].ansible_host | default(seed_host) }}"
- name: Set a fact containing the ironic node's initial provision state - name: Set a fact containing the ironic node's initial provision state
set_fact: set_fact:
initial_provision_state: "{{ show_result.results[0].stdout_lines[1] }}" initial_provision_state: "{{ show_result.stdout_lines[1] }}"
- name: Fail if the ironic node is in an unexpected provision state - name: Fail if the ironic node is in an unexpected provision state
fail: fail:
@ -81,9 +82,11 @@
retries: "{{ ironic_retries }}" retries: "{{ ironic_retries }}"
delay: "{{ ironic_retry_interval }}" delay: "{{ ironic_retry_interval }}"
when: "{{ initial_provision_state != 'available' }}" when: "{{ initial_provision_state != 'available' }}"
delegate_to: "{{ item }}" delegate_to: "{{ seed_host }}"
with_items: vars:
- "{{ hostvars[groups['seed'][0]].ansible_host }}" # NOTE: Without this, the seed's ansible_host variable will not be
# respected when using delegate_to.
ansible_host: "{{ hostvars[seed_host].ansible_host | default(seed_host) }}"
- name: Wait for the ironic node to become available - name: Wait for the ironic node to become available
command: > command: >
@ -109,13 +112,15 @@
- "{{ wait_available | bool }}" - "{{ wait_available | bool }}"
- "{{ initial_provision_state != 'available' }}" - "{{ initial_provision_state != 'available' }}"
changed_when: False changed_when: False
delegate_to: "{{ item }}" delegate_to: "{{ seed_host }}"
with_items: vars:
- "{{ hostvars[groups['seed'][0]].ansible_host }}" # NOTE: Without this, the seed's ansible_host variable will not be
# respected when using delegate_to.
ansible_host: "{{ hostvars[seed_host].ansible_host | default(seed_host) }}"
- name: Set a fact containing the ironic node's final provision state - name: Set a fact containing the ironic node's final provision state
set_fact: set_fact:
final_provision_state: "{{ show_result.results[0].stdout_lines[1] }}" final_provision_state: "{{ show_result.stdout_lines[1] }}"
when: when:
- "{{ wait_available | bool }}" - "{{ wait_available | bool }}"
- "{{ initial_provision_state != 'available' }}" - "{{ initial_provision_state != 'available' }}"

View File

@ -20,6 +20,7 @@
# Retries to use when using Ironic API and hitting node locked errors. # Retries to use when using Ironic API and hitting node locked errors.
ironic_retries: 6 ironic_retries: 6
ironic_retry_interval: 5 ironic_retry_interval: 5
seed_host: "{{ groups['seed'][0] }}"
gather_facts: no gather_facts: no
tasks: tasks:
- name: Check the ironic node's initial provision state - name: Check the ironic node's initial provision state
@ -39,15 +40,15 @@
-a "openstack baremetal node show {% raw %}{{ inventory_hostname }}{% endraw %} -f value -c provision_state"' -a "openstack baremetal node show {% raw %}{{ inventory_hostname }}{% endraw %} -f value -c provision_state"'
register: show_result register: show_result
changed_when: False changed_when: False
# We use this convoluted construct to work around Ansible's limitations delegate_to: "{{ seed_host }}"
# in evaluation of the delegate_to keyword. vars:
delegate_to: "{{ item }}" # NOTE: Without this, the seed's ansible_host variable will not be
with_items: # respected when using delegate_to.
- "{{ hostvars[groups['seed'][0]].ansible_host }}" ansible_host: "{{ hostvars[seed_host].ansible_host | default(seed_host) }}"
- name: Set a fact containing the ironic node's initial provision state - name: Set a fact containing the ironic node's initial provision state
set_fact: set_fact:
initial_provision_state: "{{ show_result.results[0].stdout_lines[1] }}" initial_provision_state: "{{ show_result.stdout_lines[1] }}"
- name: Fail if the ironic node is in an unexpected provision state - name: Fail if the ironic node is in an unexpected provision state
fail: fail:
@ -75,9 +76,11 @@
retries: "{{ ironic_retries }}" retries: "{{ ironic_retries }}"
delay: "{{ ironic_retry_interval }}" delay: "{{ ironic_retry_interval }}"
when: "{{ initial_provision_state != 'manageable' }}" when: "{{ initial_provision_state != 'manageable' }}"
delegate_to: "{{ item }}" delegate_to: "{{ seed_host }}"
with_items: vars:
- "{{ hostvars[groups['seed'][0]].ansible_host }}" # NOTE: Without this, the seed's ansible_host variable will not be
# respected when using delegate_to.
ansible_host: "{{ hostvars[seed_host].ansible_host | default(seed_host) }}"
- name: Ensure the ironic node is inspected - name: Ensure the ironic node is inspected
command: > command: >
@ -96,9 +99,11 @@
until: "{{ provide_result | success or 'is locked by host' in provide_result.stdout }}" until: "{{ provide_result | success or 'is locked by host' in provide_result.stdout }}"
retries: "{{ ironic_retries }}" retries: "{{ ironic_retries }}"
delay: "{{ ironic_retry_interval }}" delay: "{{ ironic_retry_interval }}"
delegate_to: "{{ item }}" delegate_to: "{{ seed_host }}"
with_items: vars:
- "{{ hostvars[groups['seed'][0]].ansible_host }}" # NOTE: Without this, the seed's ansible_host variable will not be
# respected when using delegate_to.
ansible_host: "{{ hostvars[seed_host].ansible_host | default(seed_host) }}"
- name: Wait for the ironic node to be inspected - name: Wait for the ironic node to be inspected
command: > command: >
@ -123,13 +128,15 @@
when: when:
- "{{ wait_inspected | bool }}" - "{{ wait_inspected | bool }}"
changed_when: False changed_when: False
delegate_to: "{{ item }}" delegate_to: "{{ seed_host }}"
with_items: vars:
- "{{ hostvars[groups['seed'][0]].ansible_host }}" # NOTE: Without this, the seed's ansible_host variable will not be
# respected when using delegate_to.
ansible_host: "{{ hostvars[seed_host].ansible_host | default(seed_host) }}"
- name: Set a fact containing the final provision state - name: Set a fact containing the final provision state
set_fact: set_fact:
final_provision_state: "{{ show_result.results[0].stdout_lines[1] }}" final_provision_state: "{{ show_result.stdout_lines[1] }}"
when: when:
- "{{ wait_inspected | bool }}" - "{{ wait_inspected | bool }}"

View File

@ -29,6 +29,7 @@
# Retries to use when using Ironic API and hitting node locked errors. # Retries to use when using Ironic API and hitting node locked errors.
ironic_retries: 6 ironic_retries: 6
ironic_retry_interval: 5 ironic_retry_interval: 5
seed_host: "{{ groups['seed'][0] }}"
gather_facts: no gather_facts: no
tasks: tasks:
- name: Check the ironic node's initial provision state - name: Check the ironic node's initial provision state
@ -48,15 +49,15 @@
-a "openstack baremetal node show {% raw %}{{ inventory_hostname }}{% endraw %} -f value -c provision_state"' -a "openstack baremetal node show {% raw %}{{ inventory_hostname }}{% endraw %} -f value -c provision_state"'
register: show_result register: show_result
changed_when: False changed_when: False
# We use this convoluted construct to work around Ansible's limitations delegate_to: "{{ seed_host }}"
# in evaluation of the delegate_to keyword. vars:
delegate_to: "{{ item }}" # NOTE: Without this, the seed's ansible_host variable will not be
with_items: # respected when using delegate_to.
- "{{ hostvars[groups['seed'][0]].ansible_host }}" ansible_host: "{{ hostvars[seed_host].ansible_host | default(seed_host) }}"
- name: Set a fact containing the ironic node's initial provision state - name: Set a fact containing the ironic node's initial provision state
set_fact: set_fact:
initial_provision_state: "{{ show_result.results[0].stdout_lines[1] }}" initial_provision_state: "{{ show_result.stdout_lines[1] }}"
- name: Fail if the ironic node is in an unexpected provision state - name: Fail if the ironic node is in an unexpected provision state
fail: fail:
@ -84,9 +85,11 @@
retries: "{{ ironic_retries }}" retries: "{{ ironic_retries }}"
delay: "{{ ironic_retry_interval }}" delay: "{{ ironic_retry_interval }}"
when: "{{ initial_provision_state == 'enroll' }}" when: "{{ initial_provision_state == 'enroll' }}"
delegate_to: "{{ item }}" delegate_to: "{{ seed_host }}"
with_items: vars:
- "{{ hostvars[groups['seed'][0]].ansible_host }}" # NOTE: Without this, the seed's ansible_host variable will not be
# respected when using delegate_to.
ansible_host: "{{ hostvars[seed_host].ansible_host | default(seed_host) }}"
- name: Ensure the ironic node is available - name: Ensure the ironic node is available
command: > command: >
@ -106,9 +109,11 @@
retries: "{{ ironic_retries }}" retries: "{{ ironic_retries }}"
delay: "{{ ironic_retry_interval }}" delay: "{{ ironic_retry_interval }}"
when: "{{ initial_provision_state in ['enroll', 'manageable'] }}" when: "{{ initial_provision_state in ['enroll', 'manageable'] }}"
delegate_to: "{{ item }}" delegate_to: "{{ seed_host }}"
with_items: vars:
- "{{ hostvars[groups['seed'][0]].ansible_host }}" # NOTE: Without this, the seed's ansible_host variable will not be
# respected when using delegate_to.
ansible_host: "{{ hostvars[seed_host].ansible_host | default(seed_host) }}"
- name: Set a fact containing the bifrost host list - name: Set a fact containing the bifrost host list
set_fact: set_fact:
@ -129,9 +134,11 @@
-e @/etc/bifrost/dib.yml -e @/etc/bifrost/dib.yml
--limit {{ bifrost_limit | join(':') }}' --limit {{ bifrost_limit | join(':') }}'
when: "{{ bifrost_limit }}" when: "{{ bifrost_limit }}"
delegate_to: "{{ item }}" delegate_to: "{{ seed_host }}"
with_items: vars:
- "{{ hostvars[groups['seed'][0]].ansible_host }}" # NOTE: Without this, the seed's ansible_host variable will not be
# respected when using delegate_to.
ansible_host: "{{ hostvars[seed_host].ansible_host | default(seed_host) }}"
# We execute this only once, allowing the Bifrost Ansible to handle # We execute this only once, allowing the Bifrost Ansible to handle
# multiple nodes. # multiple nodes.
run_once: True run_once: True
@ -160,13 +167,15 @@
- "{{ wait_active | bool }}" - "{{ wait_active | bool }}"
- "{{ initial_provision_state != 'active' }}" - "{{ initial_provision_state != 'active' }}"
changed_when: False changed_when: False
delegate_to: "{{ item }}" delegate_to: "{{ seed_host }}"
with_items: vars:
- "{{ hostvars[groups['seed'][0]].ansible_host }}" # NOTE: Without this, the seed's ansible_host variable will not be
# respected when using delegate_to.
ansible_host: "{{ hostvars[seed_host].ansible_host | default(seed_host) }}"
- name: Set a fact containing the final provision state - name: Set a fact containing the final provision state
set_fact: set_fact:
final_provision_state: "{{ show_result.results[0].stdout_lines[1] }}" final_provision_state: "{{ show_result.stdout_lines[1] }}"
when: when:
- "{{ wait_active | bool }}" - "{{ wait_active | bool }}"
- "{{ initial_provision_state != 'active' }}" - "{{ initial_provision_state != 'active' }}"