Can I use a dictionary variable to supply all task parameters?
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.
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 }}"
For those who came here from a search engine, i have successfully tested the following on Ansible 2.9.14:
vars:
my_module_defaults:
state: present
data_to_fetch:
- arg1: 42
arg2: foo
- arg1: 43
[...]
tasks:
- name: Very slim task
my_module: "{{ my_module_defaults | combine(item) }}"
with_items: "{{ data_to_fetch }}"