{"id":701,"date":"2022-05-31T01:04:09","date_gmt":"2022-05-31T01:04:09","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=701"},"modified":"2023-08-07T09:09:41","modified_gmt":"2023-08-07T09:09:41","slug":"ansible-vault","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/ansible-vault","title":{"rendered":"How to use ansible vault?"},"content":{"rendered":"<h1>ANSIBLE VAULT<\/h1>\n<p><a href=\"https:\/\/www.checkmateq.com\/node-js-development\">Ansible Vault<\/a> is a feature of Ansible that allows you to secure sensitive data such as passwords by storing them in an encrypted file rather than leaving them as plaintext in playbooks or roles. You may need to configure and use one or more passwords to encrypt and decrypt the content. You can view encrypted files, decrypt encrypted files, create encrypted files, and view\/edit encrypted files using ansible-vault commands. All the ansible vault commands will start with\u00a0<strong>ansible-vault.<\/strong><\/p>\n<p><strong>How to use ansible-vault command to create an encrypted file.<\/strong><\/p>\n<pre>sudo ansible-vault create hello.yml<\/pre>\n<p><strong>For Instance<\/strong>: In above command,\u00a0<strong>create\u00a0<\/strong>is an option and <strong>hello.yml\u00a0<\/strong>is file name.<\/p>\n<p>This command will create file named &#8220;hello.yml&#8221; and data written to this will be encrypted, you have to secure you file with a password and use the same to access the file, observe below snapshot when &#8220;cat&#8221; command is used to see content of &#8220;hello.yml&#8221; output is a cipher text (encrypted text)<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-780 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-12-300x86.jpeg\" alt=\"\" width=\"786\" height=\"225\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-12-300x86.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-12-1024x292.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-12-768x219.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-12-1536x439.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-12-1200x343.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-12.jpeg 1891w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><b>How to use ansible-vault command to see contents of an encrypted file<\/b><\/p>\n<p><img loading=\"lazy\" class=\" wp-image-720 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-2-300x48.jpeg\" alt=\"\" width=\"699\" height=\"112\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-2-300x48.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-2-1024x165.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-2-768x124.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-2-1536x248.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-2-1200x194.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-2.jpeg 1878w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>if you use &#8220;cat&#8221; command or any other editor to see content of an encrypted file you will get an encrypted text.<\/p>\n<pre>sudo ansible-vault view hello.yml<\/pre>\n<p>This command will allow you to see actual data.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-786 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-13-300x36.jpeg\" alt=\"\" width=\"725\" height=\"87\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-13-300x36.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-13-1024x123.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-13-768x92.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-13-1536x184.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-13-1200x144.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-13.jpeg 1641w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>please consider above example to see content of encrypted file, use option &#8220;<strong>view<\/strong>&#8221; to see contents of an encrypted file, you have to authenticate with password to see the content of encrypted files.<\/p>\n<p><b>How to use ansible-vault command to edit an encrypted file<\/b><\/p>\n<p>you cannot use normal editor like vi\/vim\/nano to edit ansible vault files as the content will be in an encrypted format and user should authenticate with a password to edit an encrypted file.<\/p>\n<pre>sudo ansible-vault edit hello.yml<\/pre>\n<p>This command will open the file to edit using default editor set on your machine terminal.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-722 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-4-300x30.jpeg\" alt=\"\" width=\"680\" height=\"68\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-4-300x30.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-4-1024x103.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-4-768x78.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-4-1536x155.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-4-1200x121.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-4.jpeg 1692w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><b>How to use ansible-vault command to decrypt an encrypted file<\/b><\/p>\n<p>option <strong>decrypt<\/strong> will allow you to decrypt an encrypted file, as you saw in the output snapshot of &#8220;view&#8221; there is no sensitive data in &#8220;hello.yml&#8221;, so if you want to decrypt and keep it as an unencrypted file use below command.<\/p>\n<pre>sudo ansible-vault decrypt hello.yml<\/pre>\n<p>as we decrypted &#8220;hello.yml&#8221;, you can see the content of the file using &#8220;cat&#8221; command<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-724 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-5-300x49.jpeg\" alt=\"\" width=\"631\" height=\"103\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-5-300x49.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-5-1024x166.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-5-768x125.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-5-1536x249.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-5-1200x195.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-5.jpeg 1866w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>How to use ansible-vault command to encrypt a file<\/strong><\/p>\n<p>assume you have created an unencrypted file and some sensitive data is written to it, then you can use option encrypt with ansible-vault command to encrypt the file.<\/p>\n<pre>sudo ansible-vault encrypt password.yml<\/pre>\n<p>here &#8220;encrypt&#8221; is option and &#8220;password.yml&#8221; is file name which is being encrypted.<\/p>\n<p>as you can see file &#8220;password.yml&#8221; is not encrypted and its content can be viewed with &#8220;cat&#8221; command, after encryption content shown using &#8220;cat&#8221; is a cipher text and the content of the file can only be viewed with ansible-vault &#8220;view&#8221;.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-726 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-6-300x109.jpeg\" alt=\"\" width=\"908\" height=\"330\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-6-300x109.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-6-1024x372.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-6-768x279.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-6-1536x558.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-6-1200x436.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-6.jpeg 1888w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><b>How to use ansible-vault command to give password of an encrypted file\u00a0<\/b><\/p>\n<p><strong>&#8211;ask-vault-pass<\/strong>:\u00a0 if you want to use an encrypted file in a ansible playbook you should pass the password but it won&#8217;t ask for a password directly you should use this option &#8220;&#8211;ask-vault-pass&#8221; so that it will prompt for a password, follow below scenario to understand it.<\/p>\n<p>Let&#8217;s take an example that you want to clone to your github private repository you need to provide username and password or personal access token.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-733 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-8-300x23.jpeg\" alt=\"\" width=\"1206\" height=\"93\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-8-300x23.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-8-1024x78.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-8-768x58.jpeg 768w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>if you want to clone the repository on slave machines using a ansible playbook you should give the credentials in link itself as shown below as you cannot enter credentials manually.<\/p>\n<pre>git clone https:\/\/aksshaay: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@github.com\/aksshaay\/vault-practice.git<\/pre>\n<p>here &#8220;aksshaay&#8221; is my github username and &#8220;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&#8221; is my PAT token.<\/p>\n<p>In this scenario if you want to share your playbook with another user you may not like to keep your password plain\/unencrypted, so you can create an encrypted file using ansible-vault to store password\/PAT there and pass it as a variable from a external file in playbook, here I am creating a encrypted file named &#8220;vault-password.yml&#8221; and storing my PAT there.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-736 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-9-300x27.jpeg\" alt=\"\" width=\"1024\" height=\"92\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-9-300x27.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-9-1024x92.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-9-768x69.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-9-1536x138.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-9-1200x108.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-9.jpeg 1719w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p><img loading=\"lazy\" class=\"wp-image-737 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-10-300x48.jpeg\" alt=\"\" width=\"913\" height=\"146\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-10-300x48.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-10-1024x165.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-10-768x124.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-10-1536x247.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-10-1200x193.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-10.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>Now I am creating a playbook named &#8220;clone-repo.yml&#8221; on my ansible master or controller machine to clone my private git repository in slave or controlled machines, here I will pass my PAT (personal access token) of github as variable from external file instead of giving it directly in the link.<\/p>\n<p>here in below yml file iam giving &#8220;vault-password.yml&#8221;(I stored my git PAT here in last step) under vars_files and passing my PAT as a variable.<\/p>\n<p><strong>clone-repo.yml<\/strong><\/p>\n<pre>---\r\n- name: ansible playbook to clone a git repo\r\n  hosts: all\r\n  become: true\r\n  vars_files:\r\n   - vault-password.yml\r\n  tasks:\r\n  - name: clone a repo\r\n    git:\r\n      repo: https:\/\/aksshaay:{{ password }}@github.com\/aksshaay\/vault-practice.git\r\n      dest: \/opt\/ansadmin\/test-vault\r\n<\/pre>\n<pre>sudo ansible-playbook -i hosts clone-repo.yml --ask-vault-pass<\/pre>\n<p>so here in the above command iam giving password of &#8220;vault-password.yml&#8221; file using option\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&#8220;&#8211;ask-vault-pass&#8221;<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-742 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-11-300x22.jpeg\" alt=\"\" width=\"655\" height=\"48\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-11-300x22.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-11-1024x76.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-11-768x57.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-11-1536x114.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-11-1200x89.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/an-11.jpeg 1787w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>ansible-vault command to give password as a file<\/strong><\/p>\n<p>this option will be help full to pass the password using a file, in above example of &#8220;clone-repo.yml&#8221; I used a encrypted file named &#8220;vault-password.yml&#8221; to pass a variable from it and option -&#8220;-ask-vault-pass&#8221; is used to give password of &#8220;vault-password.yml&#8221; file. we can also store the password for &#8220;vault-password.yml&#8221; in a file and pass that file using option <strong>&#8211;vault-password-file.<\/strong><\/p>\n<p>Here iam going to store password for &#8220;vault-password.yml&#8221; in a file named &#8220;my-pass.yml&#8221;\u00a0 and pass this using &#8211;vault-password-file.<\/p>\n<pre>sudo ansible-playbook -i hosts clone-repo.yml --vault-password-file my-pass.yml<\/pre>\n<p>Please write to our technical DevOps Director to address <a href=\"https:\/\/www.checkmateq.com\/devops-engineering\">DevOps Support<\/a> or cloud infrastructure engineering related issues. We will be delighted to assist you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ANSIBLE VAULT Ansible Vault is a feature of Ansible that allows you to secure sensitive data such as passwords by storing them in an encrypted file rather than leaving them as plaintext in playbooks or roles. You may need to configure and use one or more passwords to encrypt and decrypt the content. You can &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/ansible-vault\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to use ansible vault?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":770,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[3,2,11,23,24,14],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/701"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=701"}],"version-history":[{"count":55,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/701\/revisions"}],"predecessor-version":[{"id":4335,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/701\/revisions\/4335"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/770"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}