Is it possible to use ansible-vault with ad-hoc commands.
Use case: I have not setup ssh key and I want to use ping module on target machine.
normal way if we have setup sshkey or pass the text password
1: ansible <target_hostname> -m ping [if ssh key configured]
2: ansible <target_hostname> -m ping --extra-vars “ansible_user= ansible_password=”
But I want to do this with ansible-vault.
Is it possible ? any help would be appreciated.
where “secret_file” was created with password of <targer_vm_username>
ansible-vault create secret_file
cat secret_file
Output:
[WARNING]: Error in vault password file loading (default): A vault password must be specified to decrypt data
ERROR! A vault password must be specified to decrypt data
You have to pass vault password to ansible command not vault file itself. Vault file stores your secrets/variables in encrypted format and vault password is used decrypt it. Pass vault password to ansible command.
If your roles or playbooks reference encrypted variables, you need to have give Ansible the password to decrypt them. Prior Ansible 2.4, You can do this in two ways:
1). Using the --ask-vault-pass flag will instruct Ansible to ask for the vault password so it can decrypt the variable files correctly.
2). Using —vault-password-file flag will instruct Ansible to reference vault password from file. Ansible playbook use the password with in the reference file to decrypt vault file.
Since Ansible 2.4, there is way to provide a vault password is to use the --vault-id option as well. This allow vault files or vars that are encrypted with different passwords can be used at the same time. If your roles or playbooks reference encrypted variables, you need to have give Ansible the password to decrypt them. Prior Ansible 2.4, You can do this in two ways:
1). Using the --ask-vault-pass flag will instruct Ansible to ask for the vault password so it can decrypt the variable files correctly.
2). Using —vault-password-file flag will instruct Ansible to reference vault password from file. Ansible playbook use the password with in the reference file to decrypt vault file.
Since Ansible 2.4, there is way to provide a vault password is to use the --vault-id option as well. This allow vault files or vars that are encrypted with different passwords can be used at the same time. That what Andrew was mentioned on his post.
I have already given you example. You can see If you closely read my first replay to this thread. Ansible vault works the same way for both ansible-playbook and ansible command. What ever documented for ansible-playbook also work with ansible ad hoc if you use vault. You have spend some time to learn it. Every thing documented well.
Here is the step by step example:
1). Create a directory group_vars
`
mkdir -p /etc/ansible/group_vars
`
2). Create a variable file with your server user name and password. Please note this is the username and password which your ansible ad hoc command going to use to login to your target machine.
It is working for me now. thanks for the explaining it in detailed.
it is working for for me after creating the group into /etc/ansible/group_var
I have a doubt, can we achieve the same functionality without creating vault file with same group name which we given in inventory file into /etc/ansible/group_var/?
You can create a directory called ‘all’ under your playbook group_vars directory and use ‘all’ in your ad hoc command. This way the variables applied to all the host defined in myhostfile in my example.
tree -L 3 /etc/ansible/group_vars
/etc/ansible/group_vars
`-- all
`-- secrets.yml
ansible -i /etc/ansible/myhostfile all -m file -a “dest=/tmp/hello mode=755 state=directory” -u root --ask-vault-pass