Ansible Tower (AWX) How to add a temporary inventory host at run time

I came across an interesting conundrum recently. I was trying to build a playbook that needed to run on a server that just provisioned. I didn’t think it was a big deal until I started creating my playbook in AWX and found that I had to define the inventory. Given that you don’t have the command line to simply add a “-i <target_hostname> into the ansible command I started digging.

You might be puzzled to wonder how do I define an inventory of something you just provisioned 😛

Well that is what we are going to talk about.

A simple playbook like this one.

---
- hosts: "{ target_host }}"??
remote_user: svc_ansiorch?
tasks:????
- name: mark ansible was here??????
command: touch /tmp/ansible_was_here

with spew out something like this in tower.

 ansible-playbook 2.7.92  config file = /etc/ansible/ansible.cfg3  configured module search path = [u'/var/lib/awx/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']4  ansible python module location = /usr/lib/python2.7/site-packages/ansible5  executable location = /usr/bin/ansible-playbook6  python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]7Using /etc/ansible/ansible.cfg as config file8/tmp/awx_1209_kavj1db0/tmpxdfz0r4q did not meet host_list requirements, check plugin documentation if this is unexpected9Parsed /tmp/awx_1209_kavj1db0/tmpxdfz0r4q inventory source with script plugin1011PLAYBOOK: ansi-here-touch.yml **************************************************121 plays in ansi-here-touch.yml13 [WARNING]: Could not match supplied host pattern, ignoring: ecl-14testas-s42.example.com 151617PLAY [ecl-testas-s42.example.com] ***********************************************13:28:0818skipping: no hosts matched1920PLAY RECAP *********************************************************************13:28:0821

The problem:
Defining an inventory list of a server or servers that have not been created yet to run the playbook.

The solution:
What I found was that there is a way to get around this. The method is basically to create a temporary inventory inside the playbook itself. In this article we will go over some of the steps that can get you over this problem.

The code to build this temporary inventory goes something like this



– hosts: localhost
connection: local
gather_facts: true


tasks:
– name: add hosts from extra-vars to “temp” group
add_host:
hostname: “{{ target_host }}”
groups: temp


– hosts: temp
gather_facts: true
become: yes
become_method: sudo
ignore_errors: yes

tasks:
– name: Whatever you want to do



Ben Tuma

Over 20 years of experience in the Information Technology field. I love technology and seeing how it changes and impacts peoples lives for the better. I have healthy appetite for innovation and problem solving.

I am sharing my knowledge and challenges in hopes to help others as we constantly face ever changes problems in IT and technology.


%d bloggers like this: