Score:1

Reduce ansible task boilerplate with some kind of template?

ma flag

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?

Score:3
in flag

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
ma flag
Great, this looks helpful, and didn't turn up in any of my research. Thanks.
ma flag
"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 :-)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.