Danny
Danny

Reputation: 14124

How can I access variables from within an ansible module?

I'd like to be able to set configuration variables for my ansible modules as standard host/group vars. How can I access them from within the module?

I'm aware I can pass all the values in the actual call, but that seems really tedious.

--
  tasks:
     - name: tell everyone
       foo_announce: msg="tell everyone" token=x091232 uri=https://api.com

Versus having an appropriate set of default configuration variables and referencing those:

--
  tasks:
     - name: tell everyone
       foo_announce: msg="tell everyone"

Upvotes: 1

Views: 969

Answers (1)

keba
keba

Reputation: 2127

easiest to use an include file and reference it - you can specify default variables or just hard-code the defaults that way.

Personally I use variables and pass them as part of the include:

  tasks:
    - include: include-notification.yaml
      vars:
        themessage: "Starting on {{ inventory_hostname }}"

As long as your include file uses a variable called 'themessage' in the above example, it should work fine...

Upvotes: 1

Related Questions