Thanks & Regards
Rajendra Rawat
# ansible-vault view group_vars/myserver.yml
Vault password:
---
ansible_user: root
ansible_ssh_pass: password1
# cat vault_key
myvault@pass
# ansible myserver -m file -a "dest=/tmp/hello mode=755 state=directory" -u root --vault-password-file vault_key
localhost | SUCCESS => {
"changed": false,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/hello",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}
Thanks & Regards
Rajendra Rawat
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7f859c16-ee20-42fc-9ec4-fa57befbff92%40googlegroups.com.
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 gone through the link shared by you but there is no where is it using the vault for ad hoc command. It is using it for playbook.
Request you to please share a example of ping module as I did in my previous email that would be helpful understanding it.
ad-hoc command:
ansible <hostname> -m ping <what_next?>
Please note sshkey is not setup on target host.
Thanks & Regards
Rajendra Rawat
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/39ba802f-c34d-4a70-b5a1-9e21f8e883d3%40googlegroups.com.
1). Create a directory group_vars
# mkdir -p /etc/ansible/group_vars2). 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.
# vim /etc/ansible/group_vars/myservers.yml---
ansible_user: root
ansible_ssh_pass: toorSave the file with above two variables. You have to change your target machine username and password.
3). My sample file looks like below after step 2.
# cat /etc/ansible/group_vars/myservers.yml
---
ansible_user: root
ansible_ssh_pass: toor
4). Encrypt the /etc/ansible/group_vars/myservers.yml file. The ansible-vault command will prompt you for a password twice (a second time to confirm the first). Once that's done, the file will be encrypted! If you edit the file directly, you'll just see encrypted text.
# ansible-vault encrypt /etc/ansible/group_vars/myservers.yml
New Vault password:
Confirm New Vault password:
Encryption successfulNOTE: You have to use this password with ansible ad hoc command. In my case I used ‘test123’ as password.
5). You will need to make inventory files for Ansible. An inventory file lists hosts which you would like to manage and the groups they belong to. I’ve created inventory file called ‘myhostfile’
# vim /etc/ansible/myhostfile
[myserver]
localhostNOTE: Here ‘myserver’ is group name and I have only one host which is my local machine. You need to change localhost to your target machine hostname or IP address. If you closely notice my group name is ‘myserver’ and vault file under group_vars directory also same.
6). Now run your ansible ad hoc command. Here is one ad hoc command which will create a file /tmp/hello on the target machine.
# ansible -i /etc/ansible/myhostfile myservers -m file -a "dest=/tmp/hello mode=755 state=directory" -u root --ask-vault-pass
Vault password:
localhost | SUCCESS => {
"changed": false,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/hello",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}NOTE: I used —ask-vault-pass which will prompt you vault password. In my case it is ‘test123’ which I used to decrypt the file. Please refer step 4.
You can also save the password in file and pass with --vault-password-file
Thanks & Regards
Rajendra Rawat
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ff338a3b-83d4-4318-898a-f0dfaa7a5f0f%40googlegroups.com.
# 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
Vault password:
localhost | SUCCESS => {
"changed": false,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/hello",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}
Thanks & Regards
Rajendra Rawat
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/36f52063-8209-498a-a8e8-73d9ab2128e5%40googlegroups.com.