Initial Commit

This commit is contained in:
2022-09-11 23:27:12 -04:00
commit 1cdd5726c2
22 changed files with 1417 additions and 0 deletions

33
tasks/add_inv_host.yml Normal file
View File

@ -0,0 +1,33 @@
---
- name: Add hosts from CSV to Tower groups
uri:
method : POST
force_basic_auth : yes
url_username : "{{ __tower_user}}"
url_password : "{{ __tower_pass}}"
url : "{{ __tower_url }}/api/v2/groups/\
{{ r_inv_group_list | selectattr('name', '==', item.tower_group) | map(attribute='id') | flatten | join(',') }}\
/hosts/"
status_code :
- 200
- 201
- 204
headers:
Content-type : application/json
body:
name : "{{ item.inventory_hostname }}"
description : "{{ item.inventory_name }}"
inventory : "{{ r_inv_id.id | default(r_inventory.json.id) }}"
body_format : json
return_content : no
validate_certs : no
ignore_errors : yes
register : r_inventory_host
loop : "{{ r_csv_hosts }}"
loop_control :
label : "{{ item.inventory_hostname }}"
when :
- r_twr_host_list[item.inventory_hostname] is not defined
...

26
tasks/create_inv.yml Normal file
View File

@ -0,0 +1,26 @@
---
- name: Create inventory in Tower
uri:
method : POST
force_basic_auth : yes
url_username : "{{ __tower_user}}"
url_password : "{{ __tower_pass}}"
url : "{{ __tower_url }}/api/v2/inventories/"
status_code :
- 200
- 201
headers:
Content-type : application/json
body:
name : "{{ __inventory }}"
organization : "{{ __organization }}"
body_format : json
return_content : no
validate_certs : no
ignore_errors : yes
register : r_inventory
when :
- __inventory not in r_inventory_list
...

View File

@ -0,0 +1,46 @@
---
- name: Convert CSV groups into a list
set_fact:
r_inv_group_consolidate: |
[
{% for csv_host in r_csv_hosts %}
"{{ csv_host.tower_group }}",
{% endfor %}
]
- name: Create inventory group in Tower
uri:
method : POST
force_basic_auth : yes
url_username : "{{ __tower_user}}"
url_password : "{{ __tower_pass}}"
url : "{{ __tower_url }}/api/v2/groups/"
status_code :
- 200
- 201
headers:
Content-type : application/json
body:
name : "{{ lv_tower_group }}"
inventory : "{{ r_inv_id.id | default(r_inventory.json.id) }}"
body_format : json
return_content : no
validate_certs : no
ignore_errors : yes
register : r_inv_group
loop : "{{ r_inv_group_consolidate | unique }}"
loop_control :
loop_var : lv_tower_group
label : "{{ lv_tower_group }}"
when :
- "r_inv_group_list | selectattr('name', '==', lv_tower_group) | map(attribute='id') | flatten | length < 1"
- name: Re-fetch inventory group info from Tower
import_tasks : tasks/fetch_inv_group_list.yml
when :
- r_inv_group.results | rejectattr('skipped', 'defined')
...

32
tasks/debug_inv_host.yml Normal file
View File

@ -0,0 +1,32 @@
---
- name: Debug Information
block:
- name: Debug r_twr_host_list
debug:
var: r_twr_host_list
- name: Debug add host to tower if not exist
debug:
msg: "[NOT_EXIST] adding {{ item.inventory_hostname }} to tower :: group: {{ item.tower_group }}"
loop: "{{ r_csv_hosts }}"
loop_control:
label: "{{ item.inventory_hostname }}"
when:
- r_twr_host_list[item.inventory_hostname] is not defined
- name: Debug add host to tower, but only if host is not in correct groups
debug:
msg: "[UPDATE] adding {{ item.inventory_hostname }} to tower group {{ item.tower_group }}"
loop: "{{ r_csv_hosts }}"
loop_control:
label: "{{ item.inventory_hostname }}"
when:
- r_twr_host_list[item.inventory_hostname] is defined
- r_twr_host_list[item.inventory_hostname].groups[item.tower_group] is not defined
when:
- __show_debug_msgs | bool
...

21
tasks/fetch_host_list.yml Normal file
View File

@ -0,0 +1,21 @@
---
- name: Fetch list of inventory hosts from Tower
uri:
force_basic_auth : yes
url_username : "{{ __tower_user}}"
url_password : "{{ __tower_pass}}"
url : "{{ __tower_url }}/api/v2/inventories/\
{{ r_inv_id.id | default(r_inventory.json.id) }}/hosts/\
?page_size={{ __api_results | default('20') }}"
validate_certs : no
return_content : no
body_format : json
register : r_get_inv_host_list
- name: Register inventory host list
set_fact:
r_twr_host_list: "{{ lookup('template', 'templates/host_list_w_group.j2') }}"
...

View File

@ -0,0 +1,20 @@
---
- name: Fetch list of inventory groups from Tower
uri:
force_basic_auth : yes
url_username : "{{ __tower_user}}"
url_password : "{{ __tower_pass}}"
url : "{{ __tower_url }}/api/v2/inventories/\
{{ r_inv_id.id | default(r_inventory.json.id) }}/groups/"
validate_certs : no
return_content : no
body_format : json
register : r_get_inv_group_list
- name: Register inventory group list
set_fact:
r_inv_group_list: "{{ lookup('template', 'templates/inv_group_list.j2') }}"
...

30
tasks/fetch_inv_list.yml Normal file
View File

@ -0,0 +1,30 @@
---
- name: Fetch list of inventories from Tower
uri:
force_basic_auth : yes
url_username : "{{ __tower_user}}"
url_password : "{{ __tower_pass}}"
url : "{{ __tower_url }}/api/v2/inventories/"
validate_certs : no
return_content : no
body_format : json
register : r_get_inv_list
- name: Build inventory list from Tower results
set_fact:
r_inventory_list : "{{ r_inventory_list | default([]) + [item.name] }}"
loop : "{{ r_get_inv_list.json.results }}"
loop_control :
label : "{{ item.name }}"
- name: Register inventory ID if available
set_fact:
r_inv_id : "{{ lookup('template', 'templates/get_inventory_id.j2') }}"
when :
- r_inventory_list is defined
- __inventory in r_inventory_list
...

7
tasks/register_csv.yml Normal file
View File

@ -0,0 +1,7 @@
---
- name: Register contents of CSV
set_fact:
r_csv_hosts: "{{ lookup('template', 'templates/csv_inventory.j2') }}"
...

34
tasks/update_inv_host.yml Normal file
View File

@ -0,0 +1,34 @@
---
- name: Update existing host and group membership
uri:
method : POST
force_basic_auth : yes
url_username : "{{ __tower_user}}"
url_password : "{{ __tower_pass}}"
url : "{{ __tower_url }}/api/v2/groups/\
{{ r_inv_group_list | selectattr('name', '==', item.tower_group) | map(attribute='id') | flatten | join(',') }}\
/hosts/"
status_code :
- 200
- 201
- 204
headers:
Content-type : application/json
body:
name : "{{ item.inventory_hostname }}"
description : "{{ item.inventory_name }}"
inventory : "{{ r_inv_id.id | default(r_inventory.json.id) }}"
body_format : json
return_content : no
validate_certs : no
ignore_errors : yes
register : r_inventory_host
loop : "{{ r_csv_hosts }}"
loop_control :
label : "{{ item.inventory_hostname }}"
when :
- r_twr_host_list[item.inventory_hostname] is defined
- r_twr_host_list[item.inventory_hostname].groups[item.tower_group] is not defined
...