I have an ansible variable defined in a variable file like below:
cat myvarsfile.yml
newpass1: oldpass3
newpass2: oldpass9
newpass3: oldpass5
…
In my host playbook I load the variable file like below:
- name: Load password variable file
include_vars: "{{ playbook_dir }}/myvarsfile.yml"
When the user passes a parameter say newpass2
I should consider it as variable and load its value from the myvarsfile.yml
file.
ansible-playbook -i /var/test.hosts test.yml -e mypass="newpass2"
I tried the below code considering old_pass
is not defined; but it does not work.
- debug:
msg: "The old value for {{ mypass }} is {{ old_pass | default( vars[mypass] ) }}"
Getting the below error:
"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute u'newpass2'
The error appears to have been in ….
question from:https://stackoverflow.com/questions/65940528/ansible-variable-name-is-itself-a-variable-how-to-read-need-syntax