Ansible: Backing up variable value doesn't work as expected
It is a totally expected result. You define the same variables in different places and ansible applies its variable precedence

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 }}"


The first jinja2 template pass finds it should be an expansion of ansible_port as defined in your play vars.
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
tmp_port has value "22" in your extra vars which will be the final value returned.