Ansible 2.2 was just released, and all documentation is based on the development branch. At the time of this writing I’m still using Ansible 2.1. I wanted to include a library from 2.2 into one of my playbooks (specifically the AWS NAT Gateway module). However, the docs that mention your own libraries are somewhat sparse. (Developing Modules and Best Practices)

Heading into the IRC channel brought me to the advice given in the Best Practices document. That the ‘ library/ ‘ directory, when in the same directory as the playbook, allows you to use any non-included library. This means no need to set something like ANSIBLE_LIBRARY as mentioned in this Quora post (https://www.quora.com/How-do-you-add-your-own-module-to-the-Ansible-library-path). It certainly is still an option though.

This means if you have playbook.yml and want to use a non-included module, even something as simple as a newer module you can simply plop it straight into library/ , name it what you want to use as a module name and it’ll “just work.” Given a playbook, and directory structure like the following, and Ansible 2.1 you can use the ec2_vpc_nat_gateway 2.2 module easily.

Directory:

playbook.yml library/ ec2_vpc_nat_gateway.py

Playbook:

--- - hosts: localhost connection: local gather_facts: false vars: vpc_public_subnet_id: "my-vpc-public-subnet-id" vpc_region: "my-region" tasks: ec2_vpc_nat_gateway: if_exist_do_not_create: yes state: present subnet_id: "{{ vpc_public_subnet_id }}" wait: yes region: "{{ vpc_region }}"

This would then call library/ec2_vpc_nat_gateway.py when ran.

A benefit to this is it’s very simple to work on fixing bugs or developing on current modules, simply pull an existing module out of site-packages/ansible/modules and plop it into library/ under another name. I recently submitted a pull-request for fixing a bug in ini_file about unexpected behavior, and simply renamed ini_file to library/ini_file_fix.py for testing, then changed all instances of ini_file to ini_file_fix in my playbook, and the “new” module was immediately used.

Pretty handy!