1

Looking for some guidance:

I have a 3 step workflow - the first 2 templates use set_stats to set some fact data. eg:

- set_stats:
    data:
      xldeploy_workstation_host : "{{ hostvars['localhost']['targethost'] }}"
  when: tower_job_id is defined

and

- set_stats:
    data:
      target_inv: "xld{{ env }}"
      target_env: "{{ env }}/TEMPLATE"
      app_system: "{{ system | lower }}"
  when: tower_job_id is defined

In the 3rd template, it uses the app_system variable like so:

- name: Run {{ app_system }} playbook
  import_playbook: "{{ app_system }}.yml"

However, I get the below error message:

ERROR! 'app_system' is undefined

I can see that set_stats worked by looking at the logs of the previous steps in the workflow:

ok: [1.2.3.4] => {
    "ansible_stats": {
        "aggregate": true, 
        "data": {
            "xldeploy_workstation_host": "*host.fqdn*"
        }, 
        "per_host": false
    }, 
    "changed": false
}

and

ok: [1.2.3.5] => {
    "ansible_stats": {
        "aggregate": true, 
        "data": {
            "app_system": "*app*", 
            "project_name": "*projectname*", 
            "target_env": "dev/TEMPLATE", 
            "target_inv": "xlddev"
        }, 
        "per_host": false
    }, 
    "changed": false
}

Not sure why the last step in the workflow is not picking up the set_stats data - it looks like it should work according to the documentation.

I am using Tower version 3.2.2 and Ansible version 2.4.3.0

kirk
  • 19
  • 1
  • 3

2 Answers2

1

Reading the documentation of set_stats this is just use for keeping some data about the playbook run, hence cannot be use for variables.

If you want to define a variable called {{ app_system }} you need to set the variable value or set a fact.

Baptiste Mille-Mathias
  • 2,144
  • 4
  • 31
  • 37
  • 4
    Hi - Reading the [doco](https://docs.ansible.com/ansible-tower/3.2.2/html/userguide/workflows.html), the example shows running a playbook, using `set_stats` to set a var, and then using that var in another playbook in the workflow - see the step `2. Through the invoke_set_stats playbook, set_stats is then invoked to artifact the URL of the uploaded integration_results.txt into the Ansible variable “integration_results_url”.` That is what I was trying to achieve. – kirk Jul 29 '18 at 22:42
  • I have encountered the same issue. I wonder if their example being limited to local host has any significance? – Nabheet Oct 03 '22 at 12:27
1

Instead of

- set_stats:
    data:
      app_system: "{{ system | lower }}"

try to use a module set_fact as follows:

- set_fact:
    app_system: "{{ system | lower }}"

After that you should be able to get your variable value using usual {{ app_system }}. But this variable is set for the host you're playing right now.

If you want to set a variable in one play (which is playing for some host #1) and want to get its value in another play (which is playing for some other host #2) you should use something like this in the second play:

- set_fact:
    app_system: "{{ hostvars[host_#1][foo] }}"

and then you again can use {{ app_system }} in the second play

Kirill Oficerov
  • 2,152
  • 2
  • 15
  • 11