{"id":3966,"date":"2023-11-11T16:15:55","date_gmt":"2023-11-11T16:15:55","guid":{"rendered":"https:\/\/geekmungus.co.uk\/?p=3966"},"modified":"2023-11-11T16:15:56","modified_gmt":"2023-11-11T16:15:56","slug":"using-ansible-vault-to-encrypt-a-string","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=3966","title":{"rendered":"Using Ansible Vault to Encrypt a String"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">There are a number of ways to protect secrets you wish to use in your Playbooks. I&#8217;ll show a simple example here of how you can encrypt a string, copy it into your Playbook for use and then run the Playbook, where the string is de-crypted in-line and used without exposing it in plain text within any of the Ansible YAML files. If you are using version control to store your Ansible Playbooks (you are right? \ud83d\ude00 ), then you&#8217;ll not want to be storing the plain text passwords direct into the YAML.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create the Encrypted String<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First we need to create an encrypted string, in this example we&#8217;re using Ansible Vault for this, we&#8217;ll run the following command which will take the string we wish to be encrypted for later use in our Playbook.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ansible-vault encrypt_string --vault-id @prompt WellSecretString!<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll be prompted for the vault password, you&#8217;ll need to enter this to encrypt the string, and you&#8217;ll also need to provide it when you run the Playbook, or the string will not be able to be de-crypted and used. When run you&#8217;ll see an output something like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"745\" height=\"156\" src=\"https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2023\/11\/image.png\" alt=\"\" class=\"wp-image-3967\" srcset=\"https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2023\/11\/image.png 745w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2023\/11\/image-300x63.png 300w\" sizes=\"auto, (max-width: 745px) 100vw, 745px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Add to the Playbook<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Within your Playbook you then need to include the encrypted text (ciphertext) in say a variable, where you can then call upon the variable within a task to use it for something. Here is a very simple example playbook that consists of three files, you add the encrypted text into a variable called &#8220;secretstring&#8221; in the vault-play.yml file. Note the name of the server is because i&#8217;m using it to build a Kubernetes cluster, so just using it for this example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">vault.yml<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>---\r\n\r\n  - hosts: localhost\r\n    become: false\r\n    pre_tasks:\r\n      - name: Check Ansible Version\r\n        assert:\r\n          that:\r\n            - ansible_version.major == 2\r\n            - ( ansible_version.minor == 7 or ansible_version.minor == 9)\r\n          msg: \"Sanger Ansible requires Ansible 2.7 or 2.9\"\r\n\r\n\r\n  - import_playbook: vault-play.yml<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">vault-play.yml<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>---\r\n\r\n  - name: \"Reachability Checks\"\r\n    hosts: k8sservers\r\n\r\n    vars:\r\n      secretstring: !vault |\r\n          $ANSIBLE_VAULT;1.1;AES256\r\n          63353931383132636330333034343831386539333830626236366231303264613930383437373932\r\n          3566333337633465343764656335653163303337306637350a333961616233323932373439306138\r\n          30366161346331663730616261333038623839303963373362336463626636643531323936646166\r\n          3563636230316165360a663162396635303165323034646334396330386264306231646534386136\r\n          39323335373738613133633065646337643231623239383064616636656165646365\r\n\r\n    tasks:\r\n    - name: \"Test Reachability\"\r\n      ping:\r\n    \r\n    - name: \"Print Secret String\"\r\n      ansible.builtin.debug:\r\n        msg: \"{{ secretstring }}\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">hosts<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;k8sservers]\r\nmy-k8s-1.domain.com<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Okay, great we&#8217;re ready to run the Playbook.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Run the Playbook<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now, you can&#8217;t just run the Playbook as you normally would otherwise when Ansible reaches the encrypted string it will barf because it is unable to decrypt the string. So to get round this all we need to do is supply the vault password when we run the Playbook as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ansible-playbook vault.yml -i hosts --ask-vault-pass<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And there you go, as you can see Ansible first asks you for your vault password then runs the Playbook and decrypts the string in-line, okay this isn&#8217;t very helpful, we&#8217;re just printing the secret to the string, but think of what else you could do, such as feeding this secret into a command to perform some privileged operation such as become\/sudo etc.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ansible-playbook vault.yml -i hosts --ask-vault-pass\r\nVault password:\r\n\r\nPLAY &#91;localhost] **********************************************************************************************************************************************\r\n\r\nTASK &#91;Gathering Facts] ****************************************************************************************************************************************\r\nok: &#91;localhost]\r\n\r\nTASK &#91;Check Ansible Version] **********************************************************************************************************************************\r\nok: &#91;localhost] => {\r\n    \"changed\": false,\r\n    \"msg\": \"All assertions passed\"\r\n}\r\n\r\nPLAY &#91;Reachability Checks] ************************************************************************************************************************************\r\n\r\nTASK &#91;Gathering Facts] ****************************************************************************************************************************************\r\nok: &#91;my-k8s-1.domain.com]\r\n\r\nTASK &#91;Test Reachability] **************************************************************************************************************************************\r\nok: &#91;my-k8s-1.domain.com]\r\n\r\nTASK &#91;Print Secret String] ************************************************************************************************************************************\r\nok: &#91;my-k8s-1.domain.com] => {\r\n    \"msg\": \"WellSecretString!\"\r\n}\r\n\r\nPLAY RECAP ****************************************************************************************************************************************************\r\nlocalhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0\r\nmy-k8s-1.domain.com : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Although simple, it is powerful. As mentioned there are other methods to protect secrets in your Playbooks, a downside of this method is that the Vault password is a shared password, i.e. anyone who needs to run the Playbook needs to know it, but if you are a small team or its just you, it can be an excellent way to protect your secrets without too much hassle.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are a number of ways to protect secrets you wish to use in your Playbooks. I&#8217;ll show a simple example here of how you can encrypt a string, copy it into your Playbook for use and then run the Playbook, where the string is de-crypted in-line and used without exposing it in plain text &#8230; <a title=\"Using Ansible Vault to Encrypt a String\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=3966\" aria-label=\"Read more about Using Ansible Vault to Encrypt a String\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49,42,22],"tags":[],"class_list":["post-3966","post","type-post","status-publish","format-standard","hentry","category-ansible","category-devops","category-security"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3966"}],"version-history":[{"count":2,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3966\/revisions"}],"predecessor-version":[{"id":3969,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3966\/revisions\/3969"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}