循环中的 Ansible 循环

Ansible loop within a loop

当涉及到在一项任务中需要多个循环时,我真的很难理解 Ansible 中的循环。 现在我正在编写一个任务,在 Ansible 中创建一些注册表项,并使以下内容有效,

- name: Windows SSL/TLS Configuration
  ansible.windows.win_regedit:
    path: HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 3.0\{{ item.type }}
    name: '{{ item.property }}'
    data: '{{ item.value }}'
    type: dword
    state: present
  loop:
  - type: Server
    property: Enabled
    value: 0
  - type: Server
    property: DisabledByDefault
    value: 1
  - type: Client
    property: Enabled
    value: 0
  - type: Client
    property: DisabledByDefault
    value: 1

但是我想要做的是将“SSL 3.0”替换为“{{ item.protocol }}”之类的东西,并能够将其替换为“SSL 2.0”、“SSL 3.0”和“ TLS 1.0”,然后在每个条目中创建相同的条目。 我希望在同一个任务中完成所有这些任务,而不是必须 运行 完全相同的任务 3 次,路径上只有一个不同。

我想做的事有可能吗?

使用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