大网数据广告活动
服务器运维问答

Can I use a dictionary variable to supply all task parameters?

· 52CE 我爱测

I want to run a task where the parameters are filled using with_items, rather than having to manually write parameter:{{item.key}} . For example, I have this host var: HtpasswdsToSet: - path: /etc/nginx/passwdfile name: janedoe p…

问题原文

原帖链接 · tom_nb_ny · CC BY-SA 3.0

I want to run a task where the parameters are filled using with_items, rather than having to manually write parameter:{{item.key}}. For example, I have this host var:

HtpasswdsToSet:
 - path: /etc/nginx/passwdfile
   name: janedoe
   password: 'abc123'
 - path: /etc/nginx/passwdfile
   name: bob
   password: '123abc'

Note that the dictionary list keys are actual htpasswd task parameters.

In the playbook, instead of doing this:

- name: add htpasswd users
  htpasswd:
    path: {{item.path}}
    name: {{item.name}}
    password: '{{item.password}}'
  with_items: "{{HtpasswdsToSet}}"

Is there some way to simply do this?

- name: add htpasswd users
  htpasswd: "{{HtpasswdsToSet}}"

This would really help me reduce playbook verbosity. Thank you.

已采纳解答

原帖链接 · Konstantin Suvorov · CC BY-SA 3.0

With Ansible 2.2 you can still use args parameter to achieve that.
But it is deprecated for a while already and will display a warning for you.
Some details about deprecation.

Example:

- hosts: localhost
  gather_facts: no
  vars:
    args_list:
      - content: hello world
        dest: /tmp/test1.txt
        mode: 0666
      - content: test test test
        dest: /tmp/test2.txt
        mode: 0444
  tasks:
    - copy:
      args: "{{ item }}"
      with_items: "{{ args_list }}"

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-01-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。