Use a dictionary variable to supply all task parameters

ansible

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.

Best Answer

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 }}"