{"id":1093,"date":"2024-02-24T02:48:23","date_gmt":"2024-02-24T02:48:23","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1093"},"modified":"2024-06-24T22:57:47","modified_gmt":"2024-06-24T22:57:47","slug":"ansible-vault","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1093","title":{"rendered":"Ansible Vault"},"content":{"rendered":"<p>Ansible vault is a tool that helps to encrypt, decrypt and manage sensitive data such as passwords, playbooks, files, etc.<\/p>\n<p>If a playbook is encrypted, ansible-navigator will prompt for the password if ones tries to execute it. One of the use case is that it will block junior system admins to run critical playbooks, while may allow non-critical playbooks that are not encrypted.<\/p>\n<p>Below are few of the commands used with ansible vault:<\/p>\n<p>The below command creates a new encrypted file and prompts for the new Vault password. It then opens a file using the default vi editor.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-vault create sampleplaybook.yml<br \/>\nNew Vault password:<br \/>\nConfirm New Vault password:<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-vault view sampleplaybook.yml<br \/>\nVault password:<br \/>\ntest<\/p>\n<p>Below command encrypts an existing file<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-vault encrypt shijuplaybook.yml<br \/>\nNew Vault password:<br \/>\nConfirm New Vault password:<br \/>\nEncryption successful<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-vault view shijuplaybook.yml<br \/>\nVault password:<\/p>\n<p>==== ==== ===<br \/>\n&#8212;<br \/>\n&#8211; name: shiju sample playbook<br \/>\nhosts: testGRP<br \/>\ntasks:<br \/>\n&#8211; name: Add a user shiju100<br \/>\nansible.builtin.user:<br \/>\nname: &#8220;{{ theUser }}&#8221;<br \/>\nstate: present<\/p>\n<p>&#8211; name: Install httpd server<br \/>\nansible.builtin.dnf:<br \/>\nname: httpd<br \/>\nstate: present<\/p>\n<p>==== ==== ===<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-vault edit sampleplaybook.yml<br \/>\nVault password:<br \/>\n<strong>[root@centos9vm ~]#<\/strong>\u00a0ansible-navigator run -m stdout shijuplaybook.yml &#8211;syntax-check &#8211;ask-vault-password &#8211;enable-prompts<br \/>\nVault password:<\/p>\n<p>playbook: \/root\/shijuplaybook.yml<\/p>\n<p><strong>[root@centos9vm ~]#\u00a0<\/strong>ansible-navigator run -m stdout shijuplaybook.yml &#8211;ask-vault-pass &#8211;enable-prompt<br \/>\nVault password:<\/p>\n<p>===== =====<\/p>\n<p><em>PLAY [shiju sample playbook] ********************************************************************************************************************************<\/em><\/p>\n<p><em>TASK [Gathering Facts] **************************************************************************************************************************************<\/em><br \/>\n<em>ok: [192.168.132.130]<\/em><\/p>\n<p><em>TASK [Add a user shiju100] **********************************************************************************************************************************<\/em><br \/>\n<em>ok: [192.168.132.130]<\/em><\/p>\n<p><em>TASK [Install httpd server] *********************************************************************************************************************************<\/em><br \/>\n<em>ok: [192.168.132.130]<\/em><\/p>\n<p><em>PLAY RECAP **************************************************************************************************************************************************<\/em><br \/>\n<em>192.168.132.130 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/em><\/p>\n<p>===== =====<\/p>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\">Changing the encryption password<\/span><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-vault rekey shijuplaybook.yml<br \/>\n<em>Vault password:<\/em><br \/>\n<em>New Vault password:<\/em><br \/>\n<em>Confirm New Vault password:<\/em><br \/>\n<em>Rekey successful<\/em><\/p>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\">Creating a password file to encrypt paybooks<\/span><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> echo &#8220;this is a password file %#$#&#8221; &gt; secret<br \/>\n<strong>[root@centos9vm ~]#<\/strong> ls -ltr | grep secret<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 28 Feb 27 11:20 secret<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-vault decrypt shijuplaybook.yml<br \/>\nVault password:<br \/>\nDecryption successful<br \/>\n<strong>[root@centos9vm ~]#<\/strong> ansible-vault encrypt shijuplaybook.yml &#8211;vault-password-file=secret<br \/>\nEncryption successful<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout shijuplaybook.yml &#8211;syntax-check &#8211;enable-prompts &#8211;vault-password-file=secret<\/p>\n<p>playbook: \/root\/shijuplaybook.yml<\/p>\n<p>The below command creates a new file using the password stored in the above used secret file:<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong>\u00a0ansible-vault create theplaybook.yml &#8211;vault-password-file=secret<br \/>\nEncryption successful<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Ansible vault is a tool that helps to encrypt, decrypt and manage sensitive data such as passwords, playbooks, files, etc. If a playbook is encrypted, <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1093\" title=\"Ansible Vault\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,21,3],"tags":[],"class_list":["post-1093","post","type-post","status-publish","format-standard","hentry","category-ansible","category-devops","category-linux"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1093"}],"version-history":[{"count":8,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1093\/revisions"}],"predecessor-version":[{"id":1513,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1093\/revisions\/1513"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1093"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}