
Use an AWX Survey to Select Hosts from an Inventory
For AWX and automation-controller style job templates. Survey input is not an authorization boundary.
A survey can let an operator choose the hosts for an automation job. When the targets already belong to a managed inventory, an inventory limit is generally clearer than constructing new hosts dynamically. Dynamic inventory additions are useful for particular workflows, but they must not turn a text box into access to arbitrary systems.
Prefer an inventory-backed limit
Assign the templateโs approved inventory and credentials, then use a prompted Limit or a controlled survey choice that the playbook maps to an approved inventory group. Confirm the operatorโs template and inventory permissions. Do not accept an unreviewed hostname and assume that matching network credentials make it an authorized target.
Validate multiline input before calling add_host
If a multiline survey is genuinely required, clean blank lines and carriage returns, remove duplicates and restrict values to the approved inventory. This example intentionally rejects any host that is not already present:
---
- name: Validate the requested inventory members
hosts: localhost
gather_facts: false
vars:
requested_hosts: >-
{{ survey_hosts | default('') | string | split('\n') |
map('trim') | reject('equalto', '') | unique | list }}
tasks:
- name: Require a nonempty, authorized host selection
ansible.builtin.assert:
that:
- requested_hosts | length > 0
- requested_hosts | difference(groups['all']) | length == 0
fail_msg: Select only approved hosts from the assigned inventory.
- name: Create a job-local group
ansible.builtin.add_host:
name: "{{ item }}"
groups: selected_targets
loop: "{{ requested_hosts }}"
changed_when: false
- name: Check the selected managed systems
hosts: selected_targets
gather_facts: false
tasks:
- name: Verify Ansible connectivity
ansible.builtin.ping:
Configure survey_hosts as the survey variable. The example uses Ansibleโs split filter; use a compatible ansible-core version in the execution environment. The connectivity example is for Python-capable Unix-like hosts; Windows needs its connection setup and Windows modules. Existing inventory variables and credentials still need to be correct.
Understand the limits of the temporary group
add_host updates the current playbook runโs in-memory inventory. It does not permanently create AWX inventory records. A job-level --limit still applies, so newly grouped hosts cannot bypass an incompatible limit. Test the survey with empty input, duplicates, an allowed host and a rejected host before attaching a role that makes changes.
For workloads discovered outside the assigned inventory, use an approved dynamic inventory source or a separately validated workflow instead of removing the authorization check from this example.
Exactly what I was looking for, thanks very much!!!!