DEV Community

pr4th4m
pr4th4m

Posted on

Custom ansible modules

How to write ansible custom modules

Ansible modules are easy way to interact between an existing application and ansible playbook. This blog covers how to write your own ansible module.

Module

  • Create a python file called greet.py with following content
  def main():
      module = AnsibleModule(
          argument_spec = dict(
              message = dict(required=True, type="str"),
          ),
          supports_check_mode=True
      )

      # get module params
      message = module.params.get("message")

      try:
          module.exit_json(changed=True, msg=message)
      except:
          module.fail_json(msg="It's bad not to greet someone")

  from ansible.module_utils.basic import AnsibleModule
  if __name__ == "__main__":
      main()
Enter fullscreen mode Exit fullscreen mode
  • File name greet.py is considered as ansible module.
  • Arguments defined in ansible playbooks will be parsed by AnsibleModule class.
  • Ansible module always returns json, for convenience ansible provides two methods, one for success and other for failure
  # For success
  module.exit_json(changed=True, msg=message)

  # For failure
  module.fail_json(msg="It's bad not to greet someone")
Enter fullscreen mode Exit fullscreen mode

Usage

  • Create an ansible playbook called playbook.yml
  touch playbook.yml
Enter fullscreen mode Exit fullscreen mode
  • Create directory library alongside playbook.yml and copy greet.py to library
  mkdir library
  cp greet.py library
Enter fullscreen mode Exit fullscreen mode
  • Now our directory structure should look like
  library
      |- greet.py
  playbook.yml
Enter fullscreen mode Exit fullscreen mode
  • We can now use greet module with our playbook.yml
  ---
  - hosts: localhost
    tasks:
    - name: Lets start greeting
      greet:
          message: "Good morning"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)