我想运行一个任务,使用with_items填充参数,而不必手动写入parameter:{{item.key}} 。 例如,我有这个主机var:
HtpasswdsToSet: - path: /etc/nginx/passwdfile name: janedoe password: 'abc123' - path: /etc/nginx/passwdfile name: bob password: '123abc'
请注意 ,字典列表键是实际的htpasswd任务参数。
在剧本中,而不是这样做:
- name: add htpasswd users htpasswd: path: {{item.path}} name: {{item.name}} password: '{{item.password}}' with_items: "{{HtpasswdsToSet}}"
有没有办法简单地做到这一点?
- name: add htpasswd users htpasswd: "{{HtpasswdsToSet}}"
这真的会帮助我减less剧本的冗长。 谢谢。
有了Ansible 2.2,你仍然可以使用args参数来实现这一点。
但已经过时了一段时间,并会显示警告。
一些关于弃用的细节 。
例:
- 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 }}"