-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_inventory.yaml
65 lines (59 loc) · 2.5 KB
/
generate_inventory.yaml
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
# Copyright 2023 CS Group
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
- name: Generate inventory
hosts: setup
gather_facts: false
tasks:
- name: Set some facts
set_fact:
generated_inventory_vars_path: "{{ inventory_dir }}/group_vars/all/generated_inventory_vars.yaml"
- name: Create group_vars/all directory
file:
path: "{{ generated_inventory_vars_path | dirname }}"
state: directory
- name: Read infra default vars
include_vars:
file: "{{ playbook_dir }}/roles/infra-defaults/defaults/main.yaml"
- name: Get inventory_vars content
set_fact:
inventory_vars_content: |
{% if vault is defined and vault.download_inventory_vars %}
{{ lookup('community.hashi_vault.hashi_vault', vault.path + 'generated_inventory_vars', token=vault.token, url=vault.url) | to_nice_yaml(indent=2) | trim }}
{% elif vault.upload_existing %}
{{ lookup('file', generated_inventory_vars_path) }}
{% else %}
{{ hostvars[inventory_hostname] | dict2items | rejectattr('key', 'match', regex) | items2dict | to_nice_yaml(indent=2) | trim }}
{% endif %}
vars:
regex: "ansible_|discovered_|download_from_vault|generated_inventory_vars_path|group_names|groups|inventory_|playbook_|omit"
- name: Make generated_inventory_vars file
copy:
content: "{{ inventory_vars_content }}"
dest: "{{ generated_inventory_vars_path }}"
mode: 0600
when:
- vault is defined
- not vault.upload_existing
- name: Send generated app_vars to vault
uri:
url: "{{ vault.url }}/v1/{{ vault.path }}generated_inventory_vars"
method: POST
headers:
X-Vault-Token: "{{ vault.token }}"
body_format: json
body: '{"data": {{ inventory_vars_content | from_yaml | to_json }} }'
when:
- vault is defined
- not ( vault.download_inventory_vars | default(false) )
- vault.upload_backup | default(false)