Answers: <p>It is a totally expected result. You define the same variables in different places and ansible applies its <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable" rel="nofollow noreferrer">variable precedence</a></p>
  
  <p>First of all, remember that variables passed as extra vars always win: you can never change them in any other place or during the run (i.e. <code>set_fact</code>).</p>
  
  <p>Now let's decompose what happens. The schema is basically the same for all vars where you don't get the expected result. I'll only give the example for <code>backup_ansible_port</code>. You can follow the spaghetti yourself for the others once you got it.</p>
  
  <p>When you hit the debug task to display <code>"{{ backup_ansible_port }}"</code></p>
  
  <ol>
  <li>The first jinja2 template pass finds it should be an expansion of <code>ansible_port</code> as defined in your play vars.</li>
  <li><code>ansible_port</code> is defined with value "11111" directly in your inventory as a host var (see #8 in above precedence doc) <strong>But</strong> it is also defined as an expansion of <code>tmp_port</code> in your play vars (see #12 in above precedence doc). Higher precedence wins, so the second jinja2 template pass finds it should expand <code>tmp_port</code></li>
  <li><code>tmp_port</code> has value "22" in your extra vars which will be the final value returned.</li>
  </ol>
  
Answers: CC BY-SA 4.0
Answers: 2020-01-24T23:41:54.957Z
Answers: 2020-01-24T23:47:29.853Z
Answers: 2020-01-24T23:47:29.853Z
Answers: Zeitounator
Body: <p>Attempting to save a variable's value for backup before modifying it doesn't work.</p>
  
  <p>I have the following inventory file (note the custom ansible_port and user):</p>
  
  <pre><code>all:
      hosts:
              test:
                      ansible_port: 11111
                      ansible_user: user_foo
                      ansible_host: 192.168.56.101 
  </code></pre>
  
  <p>And I have the following playbook:</p>
  
  <pre><code>- name: Test
    hosts: test
    vars:
            backup_ansible_port: "{{ ansible_port }}"
            backup_ansible_user: "{{ ansible_user }}"
            ansible_port: "{{ tmp_port }}"
            ansible_user: root
    tasks:
            - name: foo
              debug:
                      msg: 
                      - "{{ backup_ansible_port }}"
                      - "{{ backup_ansible_user }}"
                      - "{{ ansible_port }}"
                      - "{{ ansible_user }}"
  </code></pre>
  
  <p>Running the following command:</p>
  
  <blockquote>
    <p>ansible-playbook bootstrap_ssh.yml --ask-pass -e 'target=test tmp_port=22'</p>
  </blockquote>
  
  <p>The output is the following:</p>
  
  <pre><code>...
  TASK [foo] *********************************
    ok: [test] =&gt; {
        "msg": [
            "22", 
            "root", 
            "22", 
            "root"
        ]
    }
  ...
  </code></pre>
  
  <blockquote>
    <p>Expected output:</p>
  </blockquote>
  
  <pre><code>...
  TASK [foo] *********************************
    ok: [test] =&gt; {
        "msg": [
            "11111", 
            "user_foo", 
            "22", 
            "root"
        ]
    }
  ...
  </code></pre>
  
  <p>Why is this not the case?</p>
  
Tags: <ansible><ansible-inventory>
Title: Ansible: Backing up variable value doesn't work as expected
UserDisplayName: PaperTsar
ThreadText: # Ansible: Backing up variable value doesn't work as expected
  
  (Asked by: username_0 on 2020-01-24)
  
  Attempting to save a variable's value for backup before modifying it doesn't work.
  
  I have the following inventory file (note the custom ansible\_port and user):
  
  ```
  all:
      hosts:
              test:
                      ansible_port: 11111
                      ansible_user: user_foo
                      ansible_host: 192.168.56.101 
  
  ```
  
  And I have the following playbook:
  
  ```
  - name: Test
    hosts: test
    vars:
            backup_ansible_port: "{{ ansible_port }}"
            backup_ansible_user: "{{ ansible_user }}"
            ansible_port: "{{ tmp_port }}"
            ansible_user: root
    tasks:
            - name: foo
              debug:
                      msg: 
                      - "{{ backup_ansible_port }}"
                      - "{{ backup_ansible_user }}"
                      - "{{ ansible_port }}"
                      - "{{ ansible_user }}"
  
  ```
  
  Running the following command:
  
  > ansible-playbook bootstrap\_ssh.yml --ask-pass -e 'target=test tmp\_port=22'
  
  The output is the following:
  
  ```
  ...
  TASK [foo] *********************************
    ok: [test] => {
        "msg": [
            "22", 
            "root", 
            "22", 
            "root"
        ]
    }
  ...
  
  ```
  
  > Expected output:
  
  ```
  ...
  TASK [foo] *********************************
    ok: [test] => {
        "msg": [
            "11111", 
            "user_foo", 
            "22", 
            "root"
        ]
    }
  ...
  
  ```
  
  Why is this not the case?
  
  **Tags:** ansible, ansible-inventory
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-01-24. Score: 2 | Accepted answer
  
  It is a totally expected result. You define the same variables in different places and ansible applies its [variable precedence](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable)
  
  First of all, remember that variables passed as extra vars always win: you can never change them in any other place or during the run (i.e. `set_fact`).
  
  Now let's decompose what happens. The schema is basically the same for all vars where you don't get the expected result. I'll only give the example for `backup_ansible_port`. You can follow the spaghetti yourself for the others once you got it.
  
  When you hit the debug task to display `"{{ backup_ansible_port }}"`
  
  1. The first jinja2 template pass finds it should be an expansion of `ansible_port` as defined in your play vars.
  2. `ansible_port` is defined with value "11111" directly in your inventory as a host var (see #8 in above precedence doc) **But** it is also defined as an expansion of `tmp_port` in your play vars (see #12 in above precedence doc). Higher precedence wins, so the second jinja2 template pass finds it should expand `tmp_port`
  3. `tmp_port` has value "22" in your extra vars which will be the final value returned.
  
  
  ---
  
  