Skip to content

Commit 50ed598

Browse files
mikekrasnMike Krasnianskyidseeley-sky
authored
add zone to dynamic inventory (#57)
* add zone to dynamic inventory * AWS and GCP now refer to a zone as including the region (eu-west-1b or europe-west1-b). Because this is still a little ambiguous, the variable is called regionzone in cluster_hosts_state. Change to use this here. Co-authored-by: Mike Krasnianskyi <[email protected]> Co-authored-by: Dougal Seeley <[email protected]>
1 parent 597fb45 commit 50ed598

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

dynamic_inventory/tasks/aws.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
register: r__ec2_instance_info
1212
delegate_to: localhost
1313

14+
- debug: msg={{r__ec2_instance_info}}
15+
1416
- name: dynamic_inventory/aws | Set dynamic_inventory_flat
1517
set_fact:
1618
dynamic_inventory_flat: |
1719
{%- if cluster_vars.inventory_ip == 'private' -%}
18-
{{ r__ec2_instance_info.instances | json_query('[*].{hosttype: tags.hosttype, hostname: tags.Name, private_ip: private_ip_address, public_ip: public_ip_address, inventory_ip: private_ip_address}') }}
20+
{{ r__ec2_instance_info.instances | json_query('[*].{hosttype: tags.hosttype, hostname: tags.Name, private_ip: private_ip_address, public_ip: public_ip_address, inventory_ip: private_ip_address, regionzone: placement.availability_zone}') }}
1921
{%- else -%}
20-
{{ r__ec2_instance_info.instances | json_query('[*].{hosttype: tags.hosttype, hostname: tags.Name, private_ip: private_ip_address, public_ip: public_ip_address, inventory_ip: public_ip_address}') }}
21-
{%- endif -%}
22+
{{ r__ec2_instance_info.instances | json_query('[*].{hosttype: tags.hosttype, hostname: tags.Name, private_ip: private_ip_address, public_ip: public_ip_address, inventory_ip: public_ip_address regionzone: placement.availability_zone}') }}
23+
{%- endif -%}

dynamic_inventory/tasks/gce.yml

+8-4
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616
delegate_to: localhost
1717
run_once: true
1818

19-
#- debug: msg={{r__gcp_compute_instance_info}}
19+
- debug: msg={{r__gcp_compute_instance_info}}
2020

2121
- name: dynamic_inventory/gcp | Set dynamic_inventory_flat
2222
set_fact:
2323
dynamic_inventory_flat: |
24+
{% set res = _dynamic_inventory_flat__urlregion -%}
25+
{%- for cluster_var in res -%}{%- set _ = cluster_var.update({'regionzone': cluster_var.regionzone | regex_replace('^.*/(.*)$', '\\1') }) -%}{%- endfor -%}
26+
{{ res }}
27+
vars:
28+
_dynamic_inventory_flat__urlregion: |
2429
{%- if cluster_vars.inventory_ip == 'private' -%}
25-
{{ r__gcp_compute_instance_info.results | json_query('[*].resources[].{hosttype: labels.hosttype, hostname: name, private_ip: networkInterfaces[0].networkIP, public_ip: networkInterfaces[0].accessConfigs[0].natIP, inventory_ip: networkInterfaces[0].networkIP}') }}
30+
{{ r__gcp_compute_instance_info.results | json_query('[*].resources[].{hosttype: labels.hosttype, hostname: name, private_ip: networkInterfaces[0].networkIP, public_ip: networkInterfaces[0].accessConfigs[0].natIP, inventory_ip: networkInterfaces[0].networkIP, regionzone: zone}') }}
2631
{%- else -%}
27-
{{ r__gcp_compute_instance_info.results | json_query('[*].resources[].{hosttype: labels.hosttype, hostname: name, private_ip: networkInterfaces[0].networkIP, public_ip: networkInterfaces[0].accessConfigs[0].natIP, inventory_ip: networkInterfaces[0].accessConfigs[0].natIP}') }}
32+
{{ r__gcp_compute_instance_info.results | json_query('[*].resources[].{hosttype: labels.hosttype, hostname: name, private_ip: networkInterfaces[0].networkIP, public_ip: networkInterfaces[0].accessConfigs[0].natIP, inventory_ip: networkInterfaces[0].accessConfigs[0].natIP, regionzone: zone}') }}
2833
{%- endif -%}
29-

dynamic_inventory/tasks/main.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
- name: dynamic_inventory | Add hosts to dynamic inventory
2525
add_host:
2626
name: "{{ item.hostname }}"
27-
groups: ["{{ item.hosttype }}","{{ cluster_name }}","{{ clusterid }}"]
27+
groups: ["{{ item.hosttype }}","{{ cluster_name }}","{{ clusterid }}","{{ item.regionzone }}"]
2828
ansible_host: "{{ item.inventory_ip }}"
2929
hosttype: "{{ item.hosttype }}"
30+
regionzone: "{{ item.regionzone }}"
3031
with_items: "{{ dynamic_inventory_flat }}"
3132

3233
- name: dynamic_inventory | stat the inventory_file path
@@ -41,7 +42,7 @@
4142
{% if groupname not in ["all", "ungrouped"] -%}
4243
[{{ groupname }}]
4344
{% for hostname in groups[groupname] %}
44-
{{ hostname }} ansible_host={{hostvars[hostname].ansible_host}} hosttype={{ hostvars[hostname].hosttype }}
45+
{{ hostname }} ansible_host={{hostvars[hostname].ansible_host}} hosttype={{ hostvars[hostname].hosttype }} regionzone={{ hostvars[hostname].regionzone }}
4546
{% endfor %}
4647
4748
{% endif %}

readiness/tasks/remove_maintenance_mode.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
run_once: true
4343

4444
# Use this because the gce_labels command does not replace existing labels. https://github.com/ansible/ansible/pull/59891
45-
- name: remove_maintenance_mode/gce | Delete maintenance_mode label
45+
- name: remove_maintenance_mode/gce | Set maintenance_mode to false
4646
gcp_compute_instance:
4747
name: "{{item.name}}"
4848
project: "{{cluster_vars.project_id}}"

0 commit comments

Comments
 (0)