Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add zone to dynamic inventory #57

Merged
merged 2 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions dynamic_inventory/tasks/aws.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
register: r__ec2_instance_info
delegate_to: localhost

- debug: msg={{r__ec2_instance_info}}

- name: dynamic_inventory/aws | Set dynamic_inventory_flat
set_fact:
dynamic_inventory_flat: |
{%- if cluster_vars.inventory_ip == 'private' -%}
{{ 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}') }}
{{ 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}') }}
{%- else -%}
{{ 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}') }}
{%- endif -%}
{{ 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}') }}
{%- endif -%}
12 changes: 8 additions & 4 deletions dynamic_inventory/tasks/gce.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
delegate_to: localhost
run_once: true

#- debug: msg={{r__gcp_compute_instance_info}}
- debug: msg={{r__gcp_compute_instance_info}}

- name: dynamic_inventory/gcp | Set dynamic_inventory_flat
set_fact:
dynamic_inventory_flat: |
{% set res = _dynamic_inventory_flat__urlregion -%}
{%- for cluster_var in res -%}{%- set _ = cluster_var.update({'regionzone': cluster_var.regionzone | regex_replace('^.*/(.*)$', '\\1') }) -%}{%- endfor -%}
{{ res }}
vars:
_dynamic_inventory_flat__urlregion: |
{%- if cluster_vars.inventory_ip == 'private' -%}
{{ 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}') }}
{{ 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}') }}
{%- else -%}
{{ 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}') }}
{{ 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}') }}
{%- endif -%}

5 changes: 3 additions & 2 deletions dynamic_inventory/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
- name: dynamic_inventory | Add hosts to dynamic inventory
add_host:
name: "{{ item.hostname }}"
groups: ["{{ item.hosttype }}","{{ cluster_name }}","{{ clusterid }}"]
groups: ["{{ item.hosttype }}","{{ cluster_name }}","{{ clusterid }}","{{ item.regionzone }}"]
ansible_host: "{{ item.inventory_ip }}"
hosttype: "{{ item.hosttype }}"
regionzone: "{{ item.regionzone }}"
with_items: "{{ dynamic_inventory_flat }}"

- name: dynamic_inventory | stat the inventory_file path
Expand All @@ -41,7 +42,7 @@
{% if groupname not in ["all", "ungrouped"] -%}
[{{ groupname }}]
{% for hostname in groups[groupname] %}
{{ hostname }} ansible_host={{hostvars[hostname].ansible_host}} hosttype={{ hostvars[hostname].hosttype }}
{{ hostname }} ansible_host={{hostvars[hostname].ansible_host}} hosttype={{ hostvars[hostname].hosttype }} regionzone={{ hostvars[hostname].regionzone }}
{% endfor %}

{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion readiness/tasks/remove_maintenance_mode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
run_once: true

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