Issue with Ansible Variables and Job Templates

Problem:

I got an Ansible role with the following:

  1. defaults/main.yml:
---
# defaults file for test
my_message: "Good morning Vietnam!"
  1. tasks/main.yml:
---
# tasks file for test
- name: Print message
  debug:
    var: my_message

I import this role in Foreman, but I don’t want to assign it to any host or host group as I don’t want it to be executed with the rest of the roles (the role above is just an example but my goal is to apply updates including monitoring downtime and server restart), so I create a job template with the following content:

---
- hosts: all
  pre_tasks:
    - name: Display all parameters known for the Foreman host
      debug:
        var: foreman
  roles:
    - test

I set it as an “Ansible Playbook” job category and run it on one of my hosts by scheduling a remote job. So far, it’s perfect and I got the ouput:

[...]
TASK [test : Print message] ****************************************************
ok: [client.domain.com] => {
    "my_message": "Good morning Vietnam!"
}
[...]

Now, I want to change my default variable, so I import it (Configure > Variables), edit it and set the “Default Value” to “This is not a test, this is rock and roll!” and run my role again. But still got the same output:

[...]
TASK [test : Print message] ****************************************************
ok: [client.domain.com] => {
    "my_message": "Good morning Vietnam!"
}
[...]

Expected outcome:

[...]
TASK [test : Print message] ****************************************************
ok: [client.domain.com] => {
    "my_message": "This is not a test, this is rock and roll!"
}
[...]

Foreman and Proxy versions:

Smart Proxies 1.24.2

Foreman and Proxy plugin versions:

Foreman-tasks 0.17.5
Foreman_ansible 4.0.4
Foreman_remote_execution 2.0.8

Distribution and version:

Oracle Linux 7.7

Other relevant data:

I found a workaround, by setting the variable my_message as a global, hostgroup or host parameter. This way I got the expected result. It would have been great if I could have it worked directly in the Ansible Variables though. Do you know if it’s even possible? (cause in the documentation it says: Notice variables sent this way will always have the type String. If you want to override variables that should have other types, use method 1 - method 1 being the Ansible Variables). Thank you :slight_smile:

You can’t mix these two approaches as of today. You either import role with variables and assign them or you use arbitrary playbooks, but then you set variables through either parameter (you figured) or job tempate inputs.

A valid RFE would be allow defining template inputs linkes to ansible variables, we do the same for puppet smart class parameters.

1 Like

Thanks a lot :slight_smile: I’ll then continue with the parameters and use filters in my roles to make sure that I use the correct type of variable!