Created
July 13, 2018 08:59
-
-
Save versionsix/b55fcf60b6a688214c28e788a67c5e98 to your computer and use it in GitHub Desktop.
Ansible dynamically generate ipam + inventory names based on single integer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- name: Auto create dictionary | |
hosts: [localhost] | |
connection: local | |
vars: | |
ansible_tower: | |
towers_amount: 3 # Make sure this is uneven | |
databases_amount: 1 # Make sure this is uneven | |
prefix: 172.17.202.64/26 | |
tasks: | |
- name: Add towers to atw_hosts dictionary | |
set_fact: | |
atw_hosts: "{{ atw_hosts|default([])+[{'ip': (prefix | ipaddr(item + 16) | ipaddr('address')), 'name': 'tower-' + (item | string), 'type':'tower'}] }}" | |
# key_value3: "{{ key_value3|default({}) | {'x': item} }}" | |
loop: "{{ range(1, ansible_tower.towers_amount+1)|list }}" | |
- name: Add databases to atw_hosts dictionary | |
set_fact: | |
atw_hosts: "{{ atw_hosts|default([])+[{'ip': (prefix | ipaddr(item + 5) | ipaddr('address')), 'name': 'database-' + (item | string), 'type':'database'}] }}" | |
# key_value3: "{{ key_value3|default({}) | {'x': item} }}" | |
loop: "{{ range(1, ansible_tower.databases_amount+1)|list }}" | |
- debug: | |
var: atw_hosts | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PLAY [Auto create dictionary] *************************************************************************************************************************************************************************** | |
TASK [Gathering Facts] ********************************************************************************************************************************************************************************** | |
ok: [localhost] | |
TASK [Add towers to atw_hosts dictionary] *************************************************************************************************************************************************************** | |
ok: [localhost] => (item=1) | |
ok: [localhost] => (item=2) | |
ok: [localhost] => (item=3) | |
TASK [Add databases to atw_hosts dictionary] ************************************************************************************************************************************************************ | |
ok: [localhost] => (item=1) | |
TASK [debug] ******************************************************************************************************************************************************************************************** | |
ok: [localhost] => { | |
"atw_hosts": [ | |
{ | |
"ip": "172.17.202.81", | |
"name": "tower-1", | |
"type": "tower" | |
}, | |
{ | |
"ip": "172.17.202.82", | |
"name": "tower-2", | |
"type": "tower" | |
}, | |
{ | |
"ip": "172.17.202.83", | |
"name": "tower-3", | |
"type": "tower" | |
}, | |
{ | |
"ip": "172.17.202.70", | |
"name": "database-1", | |
"type": "database" | |
} | |
] | |
} | |
PLAY RECAP ********************************************************************************************************************************************************************************************** | |
localhost : ok=4 changed=0 unreachable=0 failed=0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment