Search code examples
variablesansibleansible-tower

Ansible how to assign new value to extra vars value


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


Solution

  • 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