Posted by: terryhowe | May 3, 2016

Ansible 2.0 Modules and Action Plugins

I posted yesterday about Action Plugins for Ansible 2.0 and I’d like to expand on that a little with the associated sample Ansible 2.0 module.  Modules live in the library directory and action plugins in action_plugin.  Here is my sample super simple action plugin:

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 'name' not in args:
            args['name'] = 'Pete'
        result.update(self._execute_module(module_args=args, task_vars=task_vars))

All this action plugin does is default the name to Pete if it is not set. Now the associated module:

#!/usr/bin/env python
module: hello
version_added: "0.1"
short_description: Hello module
    - Module to say hello.
            - Name to greet.
        default: False
- action: hello name='bob'

def main():
    argument_spec = dict(
        name = dict(required=True, type='str'),
    module = AnsibleModule(argument_spec=argument_spec)
    result = { "changed": False, "rc" : 0}
    name = module.params.get('name')
    if name:
        result['msg'] = "Hello %s!" % name
        result['failed'] = True
        result['msg'] = "Empty name is invalid"
    if result.get('failed'):

from ansible.module_utils.basic import *
from ansible.module_utils.hashivault import *

if __name__ == '__main__':

This module returns a message “Hello %(name)s”. If the name is empty, it fails. Here is a sample playbook to run the action plugin and module:

- hosts: localhost
    - hello:
    - hello: name='bob'
    - hello: name=''

When this playbook is run, the results look like:

$ ansible-playbook -v -i dev-admin-nce test.yml 
Using /etc/ansible/ansible.cfg as config file

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [hello] *******************************************************************
ok: [localhost] => {"changed": false, "msg": "Hello Pete!", "rc": 0}

TASK [hello] *******************************************************************
ok: [localhost] => {"changed": false, "msg": "Hello bob!", "rc": 0}

TASK [hello] *******************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Empty name is invalid", "rc": 0}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=1   



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: