使用loop_control 设置loop_var。否则内外循环使用同一个循环变量,不起作用。
查看文档:Defining inner and outer variable names with loop_var
文档解释说,您必须将内部循环放入不同的文件中才能将其包含在 include_tasks 中。
如果您不想这样,您可以将协议添加到每个项目以使用with_subelements。示例:
- hosts: localhost
connection: local
vars:
protocols:
- "SSL 2.0"
- "SSL 3.0"
- "TLS 1.0"
keys:
- type: Server
property: Enabled
value: 0
protocols: "{{ protocols }}"
- type: Server
property: DisabledByDefault
value: 1
protocols: "{{ protocols }}"
- type: Client
property: Enabled
value: 0
protocols: "{{ protocols }}"
- type: Client
property: DisabledByDefault
value: 1
protocols: "{{ protocols }}"
tasks:
- debug: msg="{{item.0.type}} {{item.0.property}} {{item.0.value}} {{item.1}}"
with_subelements:
- "{{ keys }}"
- protocols
这将生成 12 个密钥:
$ ansible-playbook example.yaml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does
not match 'all'
PLAY [localhost] **********************************************************************************************
TASK [Gathering Facts] ****************************************************************************************
ok: [localhost]
TASK [debug] **************************************************************************************************
ok: [localhost] => (item=[{'type': 'Server', 'property': 'Enabled', 'value': 0}, 'SSL 2.0']) => {
"msg": "Server Enabled 0 SSL 2.0"
}
ok: [localhost] => (item=[{'type': 'Server', 'property': 'Enabled', 'value': 0}, 'SSL 3.0']) => {
"msg": "Server Enabled 0 SSL 3.0"
}
ok: [localhost] => (item=[{'type': 'Server', 'property': 'Enabled', 'value': 0}, 'TLS 1.0']) => {
"msg": "Server Enabled 0 TLS 1.0"
}
ok: [localhost] => (item=[{'type': 'Server', 'property': 'DisabledByDefault', 'value': 1}, 'SSL 2.0']) => {
"msg": "Server DisabledByDefault 1 SSL 2.0"
}
ok: [localhost] => (item=[{'type': 'Server', 'property': 'DisabledByDefault', 'value': 1}, 'SSL 3.0']) => {
"msg": "Server DisabledByDefault 1 SSL 3.0"
}
ok: [localhost] => (item=[{'type': 'Server', 'property': 'DisabledByDefault', 'value': 1}, 'TLS 1.0']) => {
"msg": "Server DisabledByDefault 1 TLS 1.0"
}
ok: [localhost] => (item=[{'type': 'Client', 'property': 'Enabled', 'value': 0}, 'SSL 2.0']) => {
"msg": "Client Enabled 0 SSL 2.0"
}
ok: [localhost] => (item=[{'type': 'Client', 'property': 'Enabled', 'value': 0}, 'SSL 3.0']) => {
"msg": "Client Enabled 0 SSL 3.0"
}
ok: [localhost] => (item=[{'type': 'Client', 'property': 'Enabled', 'value': 0}, 'TLS 1.0']) => {
"msg": "Client Enabled 0 TLS 1.0"
}
ok: [localhost] => (item=[{'type': 'Client', 'property': 'DisabledByDefault', 'value': 1}, 'SSL 2.0']) => {
"msg": "Client DisabledByDefault 1 SSL 2.0"
}
ok: [localhost] => (item=[{'type': 'Client', 'property': 'DisabledByDefault', 'value': 1}, 'SSL 3.0']) => {
"msg": "Client DisabledByDefault 1 SSL 3.0"
}
ok: [localhost] => (item=[{'type': 'Client', 'property': 'DisabledByDefault', 'value': 1}, 'TLS 1.0']) => {
"msg": "Client DisabledByDefault 1 TLS 1.0"
}
PLAY RECAP ****************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0