问题原文
原帖链接 · 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 }}"
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
