2

I'm looking for ways to reduce the amount of boiler plate config I have to put into some of my ansible tasks.

For instance I have many tasks using the docker_container module, and each one has the same ~10 identical options set. I'd like to have these standard options defined somewhere centrally, and each task simply defines only the unique options it needs.

(The problem researching this is that 99.9% of search results on this subject are about the copy/template module itself).

I guess I could write a custom module in python which extends the docker_container module, but that seems really overkill.

Any ideas on reducing boilerplate config?

1 Answer 1

4

You can use module defaults

- hosts: localhost
  module_defaults:
    ansible.builtin.file:
      owner: root
      group: root
      mode: 0755
  tasks:
    - name: Create file1
      ansible.builtin.file:
        state: touch
        path: /tmp/file1

    - name: Create file2
      ansible.builtin.file:
        state: touch
        path: /tmp/file2

    - name: Create file3
      ansible.builtin.file:
        state: touch
        path: /tmp/file3
2
  • Great, this looks helpful, and didn't turn up in any of my research. Thanks.
    – Coops
    Apr 1, 2022 at 17:51
  • "The module_defaults attribute can be used at the play, block, and task level"..... This certainly helps, but it would be great if this feature could be applied globally. Even placing it a playbook level requires me to duplicate the same settings across many places :-)
    – Coops
    Apr 1, 2022 at 23:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .