Hi
I’m struggling a bit with variables precedences in my play and in a task.
I’m using this play, which works great but I’ve add a last task to reboot my windows server BUT i have to use a special account (so a new ansible_user and ansible_password).
To do that, I was thinking to add a vars statement within my task. => This is currently working if I set them plaintext but every time I set other things like a vault lookup, it fails.
- name: Set Active Directory Settings and Accounts
hosts: "{{ eth1_ip }}"
vars_files:
- vars.yml
roles:
- set-xxxx
- set-yyyy
tasks:
- name: Reboot my server
ansible.windows.win_reboot:
post_reboot_delay: 60
reboot_timeout: 180
msg: Last reboot from Ansible
vars:
ansible_user: "my_service_account"
ansible_password: "its_password"
My vars.yml file is made of this,
ansible_connection: winrm
ansible_port: 5985
##### Vault Settings to retrieve account
windows_secrets_vault: "{{ lookup('community.hashi_vault.vault_kv2_get', 'windows_secrets', engine_mount_point='kv/', auth_method='approle', role_id=approle_id, secret_id=approle_secret_id) }}"
ansible_user: "{{ windows_secrets_vault.secret.template_admin_username }}"
ansible_password: "{{ windows_secrets_vault.secret.template_admin_password }}"
Now if I want to update my vars
statement using lookups to retrieve another ansible_user, like this :
tasks:
- name: Reboot my server
ansible.windows.win_reboot:
post_reboot_delay: 60
reboot_timeout: 180
msg: Last reboot from Ansible
vars:
windows_secrets_vault: "{{ lookup('community.hashi_vault.vault_kv2_get', 'windows_secrets', engine_mount_point='kv/', auth_method='approle', role_id=approle_id, secret_id=approle_secret_id) }}"
ansible_user: "{{ windows_secrets_vault.secret.template_ANOTHER_username }}"
ansible_password: "{{ windows_secrets_vault.secret.template_ANOTHER_password }}"
I got this kind of error
{
"msg": "The field 'remote_user' has an invalid value, which includes an undefined variable. The error was: 'windows_secrets_vault' is undefined. 'windows_secrets_vault' is undefined. 'windows_secrets_vault' is undefined. 'windows_secrets_vault' is undefined",
"_ansible_no_log": false
}
In my understanding of variable precedence and according to this I thought the vars_files is for the whole play (roles + tasks) and task vars are aware of what is defined in the vars.yml file.
So is it possible to base my task vars on vars.yml files (basically, use lookup in my vars statement) ?