AWX/Ansible Tower: How To Use A List Of Hosts Specified In A Templates Survey As The Inventory
Many of the AWX projects I write are designed to be run against specific hosts which may not be contained in an inventory included in AWX. For this scenario I usually write a playbook in such a way that the hosts can be specified in a survey question when the template is executed.
As an example, the first task in the following playbook uses the add_host module to create a new inventory group. The source for the new group comes from the survey_hosts variable. The playbook then continues with a new play using the new group created as the hosts for the play.
---
- name: configure inventory hosts
hosts: localhost
gather_facts: no
vars:
linux_hosts: "{{ survey_hosts.split('\n') }}"
tasks:
- name: make host group from survey hosts list
add_host:
hostname: "{{ item }}"
groupname: new_group
loop: "{{ linux_hosts }}"
- name: switch connecton location of linux openvpn client service
hosts: new_group,!localhost
gather_facts: no
become: true
tasks:
- name: switch location
include_role:
name: switch-location
Tags :
Exactly what I was looking for, thanks very much!!!!