text Ansible基于单个整数动态生成ipam +库存名称
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text Ansible基于单个整数动态生成ipam +库存名称相关的知识,希望对你有一定的参考价值。
---
- 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
...
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
以上是关于text Ansible基于单个整数动态生成ipam +库存名称的主要内容,如果未能解决你的问题,请参考以下文章
GCC中Early inliner和IPA inliner有什么区别?
如何在Ansible中使用静态和动态库存
运维自动化-Ansible ( 四 )
Ansible AdHoc & playbook API + 动态生成Inventory +结果关注
原创Centos使用ansible
3,Ansible---动态Inventory