1

Tower: 3.2.3 Ansible 2.4.2

I have a Tower playbook where a value is assigned lets say build_cl: latest. This is defined in the Ansible Tower's survey, which I believe is regarded as extra-vars. I have a task that performs a check, and if condition is right I need to modify the value of build_cl.

So lets say when Tower playbook is kicked off the var is:

build_cl: latest

Then:

- name: "Get latest installed CL on groups['Healthcheck_Host'][0]"
  shell: |
    grep -oP '(?<=\:)(.*?)(?=\-)' {{ latest_deployed_build_dir.stdout }}/buildinfo.txt
  register: latest_deployed_cl

- debug:
    var: latest_deployed_cl

- set_fact:
    build_cl: "{{ latest_deployed_cl.stdout }}"
    cacheable: yes

- debug:
    var: build_cl

I have tested and the debug for the first task here returns lets say 123456.

However I try to use the set_fact module, but the second debug output still gives: latest.

Nothing I try seems to be effecting the original value. Help would be greatly appreciated. Thanks

New2Python
  • 325
  • 1
  • 4
  • 17
  • I have verified that ansible is assigning the new value: { "changed": false, "ansible_facts": { "build_cl": "123456" }, "_ansible_no_log": false } – New2Python Jul 23 '20 at 00:49
  • Are you passing this as an extra var ? If yes, there is absolutely no chance you can modify the value. Extra vars have [the highest precedence priority](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable). – Zeitounator Jul 23 '20 at 08:15

1 Answers1

1

Extra vars (i.e. vars passed on the command line with the -e option), have the highest precedence and cannot be changed during playbook life. set_fact will not throw any error but the value will remain the one passed at launch.

Here is a quick example to illustrate:

---
- name: Immutable extra var demo
  hosts: localhost
  gather_facts: false

  vars:
    test_var: value set in playbook var

  tasks:
    - name: debug var value at playbook start
      debug:
        var: test_var

    - name: change var value
      set_fact:
        test_var: value set in set_fact

    - name: debug var value at playbook end
      debug:
        var: test_var

Without extra var:

$ ansible-playbook test.yml 

PLAY [Immutable extra var demo] ********************************************************************************************************************************************************************************************************

TASK [debug var value at playbook start] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
    "test_var": "value set in playbook var"
}

TASK [change var value] ****************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug var value at playbook end] *************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "test_var": "value set in set_fact"
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

With extra var:

$ ansible-playbook test.yml -e "test_var='value set in extra vars'"

PLAY [Immutable extra var demo] ********************************************************************************************************************************************************************************************************

TASK [debug var value at playbook start] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
    "test_var": "value set in extra vars"
}

TASK [change var value] ****************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug var value at playbook end] *************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "test_var": "value set in extra vars"
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
  • Deng...thanks for the quick reply. Ill have to spend time I didnt want to or have, to modify my logic then :) – New2Python Jul 23 '20 at 17:17