1

I'm using Ansible AWX (Tower) and have a template workflow that executes several templates one after the other, based on if the previous execution was successful.

I noticed I can limit to a specific host when running a single template, I'd like to apply this to the a workflow and my guess is I would have to use the survey option to achieve this, however I'm not sure how.

I have tried to see if I can override the "hosts" value and that failed like I expected it to. How can I go about having it ask me at the beginning of the workflow for the hostname/ip and not for every single template inside the workflow?

Stephen
  • 149
  • 3
  • 12

1 Answers1

4

You have the set_stats option.

Let's suppose you have the following inventory:

  • 10.100.1.1
  • 10.100.1.3
  • 10.100.1.6

Your inventory is called MyOfficeInventory. First rule is that you need this inventory across all your Templates to play with the host from the first one.

I want to ping only my 10.100.1.6 machine, so in the Template I choose MyOfficeInventory and limit to 10.100.1.6.

If we do:

---
- name: Ping
  hosts: all
  gather_facts: False
  connection: local

  tasks:

  - name: Ping
    ping:

We get:

TASK [Ping] ********************************************************************
ok: [10.100.10.6]

Cool! So from MyOfficeInventory I have my only host selected pinged. So now, in my workflow I have the next Template with *MyOfficeInventory** selected (This is the rule as said). If I ping, I will ping all of them unless you limit again so let's do the magic:

In your first Template do:

  - name:  add devices with connectivity to the "working_hosts" group
    group_by:
      key: working_hosts

  - name: "Artifact URL of test results to Tower Workflows"
    set_stats:
      data:
        myinventory:  "{{ groups['working_hosts'] }}"
    run_once: True

Be careful, because for your playbook,

groups['all']

means:

"groups['all']": [
    "10.100.10.1",
    "10.100.10.3", 
    "10.100.10.6"
]

And with your new working_hosts group, you get only your current host:

"groups['working_hosts']": [
    "10.100.10.6"
]

So now you have your brand new myinventory inventory.

Use it like this in the rest of your Playbooks assigned to your Templates:


- name: Ping
  hosts: "{{ myinventory }}"
  gather_facts: False

  tasks:

  - name: Ping
    ping:

Your inventory variable will be transferred and you will get:

ok: [10.100.10.6]

One step further. Do you want to select your host from a Survey?

Create one with your hostname input and add keep your first Playbook as:

- name: Ping
  hosts: "{{ mysurveyhost }}"
  gather_facts: False
imjoseangel
  • 3,543
  • 3
  • 22
  • 30
  • Thanks, I wasn't aware I could use a variable to define hosts. I usually run a syntax check before doing a live run and it complained about it being an invalid value, but it does in fact run without errors when defining that variable in a survey. – Stephen Sep 07 '18 at 08:14
  • Yes, indeed, but remember using the same inventory across your Templates and only changing it adding all the hosts you are going to work with. You are welcomed!!! – imjoseangel Sep 07 '18 at 09:59