Ansible 变量模块字段

Ansible 变量模块字段,ansible,Ansible,如何为模块设置自定义密钥? 我想做的是: module: "{{ var }}": ... 上面引发了一个错误,“模块的参数不受支持:{{var}}” 这对我很有用,例如设置iptables: iptables: "{{ item.key }}": lo with_items: - { key: in_interface } - { key: out_interface } 我不熟悉Ansible;环顾四周,我没有找到解决方案。您不能将jinja2模板表达式用作yaml ha

如何为模块设置自定义密钥? 我想做的是:

module:
  "{{ var }}": ...
上面引发了一个错误,“模块的参数不受支持:{{var}}”

这对我很有用,例如设置iptables:

iptables:
  "{{ item.key }}": lo
with_items:
  - { key: in_interface }
  - { key: out_interface }

我不熟悉Ansible;环顾四周,我没有找到解决方案。

您不能将jinja2模板表达式用作yaml hasmap键:正如您所发现的,它不会被解释

我为我的命题制作了可复制的例子(这只是为了举例说明,有更简单更好的方法可以在这些特定情况下获得相同的结果)

命题1:带有可解释警告的黑客 与您尝试过的方法类似的一种黑客方法是在构造的var中声明模块的所有参数,您可以在其中使用jinja2模板

---
- name: Test dynamic parameters
  hosts: localhost
  gather_facts: false

  tasks:
    - name: Set atime and mtime of file sequentially
      file: "{{ {'path': '/tmp/test_dynparm.txt', 'state': 'touch', item: '201901010000.00'} }}"
      loop:
        - access_time
        - modification_time
这将得到以下结果

TASK [Set atime and mtime of file sequentially] ***************************************************************************************************************************************************************************
 [WARNING]: Using a variable for a task's 'args' is unsafe in some situations (see https://docs.ansible.com/ansible/devel/reference_appendices/faq.html#argsplat-unsafe)

changed: [localhost] => (item=access_time)
changed: [localhost] => (item=modification_time)

PLAY RECAP ****************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


正如您所看到的,这是可行的,但是您从ansible得到一个警告,它不可能关闭,因为在var中传递模块参数。此外,在您的情况下,您必须引用所有要传递给模块的静态键和值

建议2:无警告,可能更易于维护 我的首选解决方案是用纯yaml写下静态模块参数,在循环变量中传递可选参数,并使用or过滤器

其中:

PLAY [Test dynamic parameters] ********************************************************************************************************************************************************************************************

TASK [Set atime and mtime of file sequentially] ***************************************************************************************************************************************************************************
changed: [localhost] => (item={'access_time': 201901010000.0})
changed: [localhost] => (item={'modification_time': 201901010000.0})

PLAY RECAP ****************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   



您将如何使用此键,输入/输出接口:?如果您想在模块iptables中使用它,那么就使用变量。您不能这样使用。使用集合事实或变量
PLAY [Test dynamic parameters] ********************************************************************************************************************************************************************************************

TASK [Set atime and mtime of file sequentially] ***************************************************************************************************************************************************************************
changed: [localhost] => (item={'access_time': 201901010000.0})
changed: [localhost] => (item={'modification_time': 201901010000.0})

PLAY RECAP ****************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0