提问人:Super Tony 提问时间:8/27/2023 最后编辑:ZeitounatorSuper Tony 更新时间:8/27/2023 访问量:38
如何在 Ansible 中创建数字递增的变量?
How to create variables in Ansible where the number increments?
问:
我正在编写一个 Ansible 任务,它在其中安装一组具有以下 objectClass 以及属性和值的 LDAP 条目。
dn: entry=101,cn=multicast,{{ ipa_base }}
objectClass: multicastAddress
objectClass: top
entry: 101
network: test
ipNumber: 224.0.0.1
我有多个网络,每个网络将分配 62 个 IP,条目和 ipNumber 的每个值都会递增。
Network A:
entry=101 ipNumber: 224.0.0.1
entry=102 ipNumber: 224.0.0.2
Network B:
entry=101 ipNumber: 224.0.1.1
entry=102 ipNumber: 224.0.1.2
目前,我只有一个任务,它静态地创建条目,我想在不必创建每个静态条目的地方有效地编写它。
- name: Create entries
ldap_entry:
dn: entry=101,cn=multicast,{{ ipa_base }}
state: present
objectClass:
- top
- multicastAddress
attributes:
entry: 101
network: test
ipNumber: 224.0.0.1
server_uri: localhost
bind_dn: "cn=Directory Manager"
bind_pw: '{{ dmgr_pass }}'
答:
2赞
Zeitounator
8/27/2023
#1
有几种方法可以实现您的目标,具体取决于您的确切要求。这里有一个让你在带有中间调试的不言自明的剧本中走上正轨的方法。注意:为了示例和易读性,我将输出限制为 2 个网络,每个网络有 2 个 ips。只需更改这些数字即可满足您的需求。
---
- hosts: localhost
gather_facts: false
vars:
number_of_networks: 2
ips_per_network: 2
networks_list: "{{ range(0, number_of_networks) }}"
ips_list: "{{ range(1, ips_per_network + 1) }}"
networks_ips_product: "{{ networks_list | product(ips_list) }}"
ipa_base: "dc=whatever,dc=local"
tasks:
- name: Create a list of networks starting from 0
ansible.builtin.debug:
var: networks_list
- name: Create a list of ips starting from 1
ansible.builtin.debug:
var: ips_list
- name: Combine every network with every ip in a single list
ansible.builtin.debug:
var: networks_ips_product
- name: Loop on that list in a debug tasks which mimics your ldap task
vars:
network_it: "{{ item.0 }}"
ip_it: "{{ item.1 }}"
entry: "10{{ ip_it }}"
ip: "224.0.{{ network_it }}.{{ ip_it }}"
ldap_entry:
dn: entry={{ entry }},cn=multicast,{{ ipa_base }}
state: present
objectClass:
- multicastAddress
- top
attributes:
entry: "{{ entry }}"
network: "Network{{ network_it }}"
ipNumber: "{{ ip }}"
server_uri: localhost
bind_dn: "cn=Directory Manager"
bind_pw: "v3rys3cr3t"
ansible.builtin.debug:
var: ldap_entry
loop: "{{ networks_ips_product }}"
运行上述 playbook 可提供:
PLAY [localhost] ***********************************************************************************************************************************************************************************************************************
TASK [Create a list of networks starting from 0] ***************************************************************************************************************************************************************************************
ok: [localhost] => {
"networks_list": [
0,
1
]
}
TASK [Create a list of ips starting from 1] ********************************************************************************************************************************************************************************************
ok: [localhost] => {
"ips_list": [
1,
2
]
}
TASK [Combine every network with every ip in a single list] ***************************************************************************************************************************************************************************************
ok: [localhost] => {
"networks_ips_product": [
[
0,
1
],
[
0,
2
],
[
1,
1
],
[
1,
2
]
]
}
TASK [Loop on that list in a debug tasks which mimics your ldap task] ******************************************************************************************************************************************************************
ok: [localhost] => (item=[0, 1]) => {
"ansible_loop_var": "item",
"item": [
0,
1
],
"ldap_entry": {
"attributes": {
"entry": "101",
"ipNumber": "224.0.0.1",
"network": "Network0"
},
"bind_dn": "cn=Directory Manager",
"bind_pw": "v3rys3cr3t",
"dn": "entry=101,cn=multicast,dc=whatever,dc=local",
"objectClass": [
"multicastAddress",
"top"
],
"server_uri": "localhost",
"state": "present"
}
}
ok: [localhost] => (item=[0, 2]) => {
"ansible_loop_var": "item",
"item": [
0,
2
],
"ldap_entry": {
"attributes": {
"entry": "102",
"ipNumber": "224.0.0.2",
"network": "Network0"
},
"bind_dn": "cn=Directory Manager",
"bind_pw": "v3rys3cr3t",
"dn": "entry=102,cn=multicast,dc=whatever,dc=local",
"objectClass": [
"multicastAddress",
"top"
],
"server_uri": "localhost",
"state": "present"
}
}
ok: [localhost] => (item=[1, 1]) => {
"ansible_loop_var": "item",
"item": [
1,
1
],
"ldap_entry": {
"attributes": {
"entry": "101",
"ipNumber": "224.0.1.1",
"network": "Network1"
},
"bind_dn": "cn=Directory Manager",
"bind_pw": "v3rys3cr3t",
"dn": "entry=101,cn=multicast,dc=whatever,dc=local",
"objectClass": [
"multicastAddress",
"top"
],
"server_uri": "localhost",
"state": "present"
}
}
ok: [localhost] => (item=[1, 2]) => {
"ansible_loop_var": "item",
"item": [
1,
2
],
"ldap_entry": {
"attributes": {
"entry": "102",
"ipNumber": "224.0.1.2",
"network": "Network1"
},
"bind_dn": "cn=Directory Manager",
"bind_pw": "v3rys3cr3t",
"dn": "entry=102,cn=multicast,dc=whatever,dc=local",
"objectClass": [
"multicastAddress",
"top"
],
"server_uri": "localhost",
"state": "present"
}
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
评论