summaryrefslogtreecommitdiffstats
path: root/roles/static_inventory/tasks/openstack.yml
blob: adf78c9668eca0e33a7cfd6ea14118c622ea84bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
---
- no_log: true
  block:
    - name: fetch all nodes from openstack shade dynamic inventory
      command: shade-inventory --list
      register: registered_nodes_output
      when: refresh_inventory|bool

    - name: set fact for openstack inventory cluster nodes
      set_fact:
        registered_nodes: "{{ (registered_nodes_output.stdout | from_json) | json_query(q) }}"
      vars:
        q: "[] | [?metadata.clusterid=='{{stack_name}}']"
      when:
        - refresh_inventory|bool

    - name: set_fact for openstack inventory nodes
      set_fact:
        registered_bastion_nodes: "{{ (registered_nodes_output.stdout | from_json) | json_query(q) }}"
        registered_nodes_floating: "{{ (registered_nodes_output.stdout | from_json) | json_query(q2) }}"
      vars:
        q: "[] | [?metadata.group=='infra.{{stack_name}}']"
        q2: "[] | [?metadata.clusterid=='{{stack_name}}'] | [?public_v4!='']"
      when:
        - refresh_inventory|bool

    - name: set_fact for openstack inventory nodes with provider network
      set_fact:
        registered_nodes_floating: "{{ (registered_nodes_output.stdout | from_json) | json_query(q) }}"
      vars:
        q: "[] | [?metadata.clusterid=='{{stack_name}}'] | [?public_v4=='']"
      when:
        - refresh_inventory|bool
        - openstack_provider_network_name|default(None)

    - name: Add cluster nodes w/o floating IPs to inventory
      with_items: "{{ registered_nodes|difference(registered_nodes_floating) }}"
      add_host:
        name: '{{ item.name }}'
        ansible_host: >-
          {% if use_bastion|bool -%}
          {{ item.name }}
          {%- else -%}
          {%- set node = registered_nodes | json_query("[?name=='" + item.name + "']") -%}
          {{ node[0].addresses[openstack_private_network|quote][0].addr }}
          {%- endif %}
        ansible_fqdn: '{{ item.name }}'
        ansible_user: '{{ ssh_user }}'
        ansible_private_key_file: '{{ private_ssh_key }}'
        ansible_ssh_extra_args: '-F {{ ssh_config_path }}'
        private_v4: >-
          {% set node = registered_nodes | json_query("[?name=='" + item.name + "']") -%}
          {{ node[0].addresses[openstack_private_network|quote][0].addr }}

    - name: Add cluster nodes with floating IPs to inventory
      with_items: "{{ registered_nodes_floating }}"
      add_host:
        name: '{{ item.name }}'
        ansible_host: >-
          {% if use_bastion|bool -%}
          {{ item.name }}
          {%- elif openstack_provider_network_name|default(None) -%}
          {{ item.private_v4 }}
          {%- else -%}
          {{ item.public_v4 }}
          {%- endif %}
        ansible_fqdn: '{{ item.name }}'
        ansible_user: '{{ ssh_user }}'
        ansible_private_key_file: '{{ private_ssh_key }}'
        ansible_ssh_extra_args: '-F {{ ssh_config_path }}'
        private_v4: >-
          {% set node = registered_nodes | json_query("[?name=='" + item.name + "']") -%}
          {{ node[0].addresses[openstack_private_network|quote][0].addr }}
        public_v4: >-
          {% if openstack_provider_network_name|default(None) -%}
          {{ item.private_v4 }}
          {%- else -%}
          {{ item.public_v4 }}
          {%- endif %}

    # Split registered_nodes into old nodes and new app nodes
    # Add new app nodes to new_nodes host group for upscaling
    - name: Create new_app_nodes variable
      set_fact:
        new_app_nodes: []

    - name: Filter new app nodes out of registered_nodes
      include: filter_out_new_app_nodes.yaml
      with_items: "{{ registered_nodes }}"
      loop_control:
        loop_var: node

    - name: Add new app nodes to the new_nodes section (if a deployment already exists)
      with_items: "{{ new_app_nodes }}"
      add_host:
        name: "{{ item.name }}"
        groups: new_nodes, app

    - name: Add the rest of cluster nodes to their corresponding groups
      with_items: "{{ registered_nodes }}"
      add_host:
        name: '{{ item.name }}'
        groups: '{{ item.metadata.group }}'

    - name: Add bastion node to inventory
      add_host:
        name: bastion
        groups: bastions
        ansible_host: '{{ registered_bastion_nodes[0].public_v4 }}'
        ansible_fqdn: '{{ registered_bastion_nodes[0].name }}'
        ansible_user: '{{ ssh_user }}'
        ansible_private_key_file: '{{ private_ssh_key }}'
        ansible_ssh_extra_args: '-F {{ ssh_config_path }}'
        private_v4: >-
          {% set node = registered_nodes | json_query("[?name=='" + registered_bastion_nodes[0].name + "']") -%}
          {{ node[0].addresses[openstack_private_network|quote][0].addr }}
        public_v4: '{{ registered_bastion_nodes[0].public_v4 }}'
      when:
        - registered_bastion_nodes is defined
        - use_bastion|bool