Posted by: terryhowe | May 2, 2016

Setting Ansible Module Defaults Using Action Plugins

In my last blog entry, I gave a simple example of an action plugin that calls a module.  To take that a bit further, here is an action plugin that sets some default value.  This can be handy if you are using a module and want to set some default values for that module that are specific to your application.

from ansible.plugins.action import ActionBase

class ActionModule(ActionBase):

    def run(self, tmp=None, task_vars=None):

        if task_vars is None:
            task_vars = dict()
        result = super(ActionModule, self).run(tmp, task_vars)
        args = self._task.args.copy()
        if 'authtype' not in args:
            args['authtype'] = 'userpass'
        if 'url' not in args:
            args['url'] = self._templar.template('https://vault.{{domain}}:8200')
        if 'username' not in args:
            args['username'] = self._templar.template('{{vault_username}}')
        if 'password' not in args:
            args['password'] = self._templar.template('{{vault_password}}')
        result.update(self._execute_module(module_args=args, task_vars=task_vars))
        self._task.register = args['register']
        return result

There are a couple things going on here. We are setting authtype to to userpass. We are also using the templar to set some arguments based on our variables.

The other thing going on here is we are changing the output after the module is called. Setting self._task.register will put the result back into the specified Ansible variable.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: