<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Nasir Hussain</title>
    <description>The latest articles on Forem by Nasir Hussain (@nasirhm).</description>
    <link>https://forem.com/nasirhm</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F74676%2Fa6fca010-1c37-4bd6-8a0f-698a4e46d5c5.jpeg</url>
      <title>Forem: Nasir Hussain</title>
      <link>https://forem.com/nasirhm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nasirhm"/>
    <language>en</language>
    <item>
      <title>Ansible Vault</title>
      <dc:creator>Nasir Hussain</dc:creator>
      <pubDate>Fri, 13 Dec 2019 07:23:47 +0000</pubDate>
      <link>https://forem.com/nasirhm/ansible-vault-4kg4</link>
      <guid>https://forem.com/nasirhm/ansible-vault-4kg4</guid>
      <description>&lt;h1&gt;
  
  
  Introduction to Ansible Vault
&lt;/h1&gt;

&lt;h5&gt;
  
  
  It's a feature of Ansible that allows you to hide sensitive data (Credentials) Into Encrypted files rather than Playbooks or Roles. Ansible automatically decrypts vault-encrypted content at runtime when the key is provided.
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Vxqsr11k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1200/1%2AP5CDychkYdcq1qj5wl0ujg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Vxqsr11k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1200/1%2AP5CDychkYdcq1qj5wl0ujg.jpeg" alt="Ansible Immage, Thumbnail"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Ansible Vault?:
&lt;/h3&gt;

&lt;p&gt;Ansible had no mechanism in which users can encrypt data such as a Playbook and Role and if any third-party module for encryption was used, it caused many problems in terms of Encrypting the Data and Decrypting it at times of Execution, this lead to the idea of a Utility which can fix this gap and provide better functionality with Ansible&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Ansible Vault?
&lt;/h3&gt;

&lt;p&gt;Vault is a mechanism that allows encrypted content to be incorporated transparently into Ansible workflows. A utility called ansible-vault secures confidential data by encrypting it on disk. To integrate these secrets with regular Ansible data, both the ansible and ansible-playbook commands.&lt;br&gt;
It uses the &lt;em&gt;AES256&lt;/em&gt; algorithm to provide symmetric encryption keyed to a user-supplied password. This means that the same password is used to encrypt and decrypt content, which is helpful from a usability standpoint.&lt;/p&gt;

&lt;p&gt;Now that you understand a bit about what Vault is, we can start discussing the tools Ansible provides, and how you can use it for your Ansible workflows.&lt;/p&gt;
&lt;h4&gt;
  
  
  Prerequisite :
&lt;/h4&gt;

&lt;p&gt;Ansible on a Non-Root user with sudo Access. If you don't have Ansible installed kindly check this &lt;a href="https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html"&gt;Link&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Let's Dive in
&lt;/h3&gt;
&lt;h4&gt;
  
  
  Setting up the EDITOR
&lt;/h4&gt;

&lt;p&gt;As a Newbie, Most people on Linux are either not Familiar with vi or vim, so they prefer to use nano and some other easy and simple Editor. So to change your environment according to your needs, Here's how you can do it.&lt;/p&gt;

&lt;p&gt;To set the editor for an individual command, prepend the command with the environment variable assignment, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;EDITOR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;nano ansible-vault &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To Keep this change persistent you would have to do the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;nano ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In ~/.bashrc , add the following to the End of File. it will change your default EDITOR to whatever editor is defined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;EDITOR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;nano
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Save and Close the file, &lt;br&gt;
Source the File to change it in Current Session:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt; ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To ensure you have your desired EDITOR configured Please do the Following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$EDITOR&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It would result in &lt;em&gt;nano&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You're set with the Editor Now,&lt;/p&gt;

&lt;p&gt;Now, Let's start with a File generated by ansible-vault.&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating New Encrypted file:
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ansible-vault create vault.yml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Enter Passsword and you're ready to go.&lt;/p&gt;

&lt;p&gt;as you can see by &lt;em&gt;ls&lt;/em&gt; you would see the file as &lt;em&gt;vault.yml&lt;/em&gt; you can add some text to it and then to verify the encryption function, You can do the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;vault.yml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And you would see some encrypted Text.&lt;/p&gt;

&lt;h4&gt;
  
  
  To Encrypt an Existing file :
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ansible-vault encrypt file.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Instead of opening an editing window, ansible-vault will encrypt the contents of the file and write it back to disk, replacing the unencrypted version.&lt;/p&gt;

&lt;p&gt;Type the Password and you're ready to go Again with that file.&lt;/p&gt;

&lt;h4&gt;
  
  
  Viewing Encrypted file :
&lt;/h4&gt;

&lt;p&gt;You can do it easily by the functionality provided by Ansible-Vault :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ansible-vault view vault.yml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It would ask for the Password and by entering the right credential you would be able to see the contents of the files in the terminal.&lt;/p&gt;

&lt;h4&gt;
  
  
  Editing Encrypted file :
&lt;/h4&gt;

&lt;p&gt;It's pretty easy to edit it on Terminal while having your best configurations in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ansible-vault edit vault.yml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Again by writing the right credential you would be able to edit it on your default editor, that's the reason we changed our editor to nano.&lt;/p&gt;

&lt;h4&gt;
  
  
  Decrypting Encrypted file :
&lt;/h4&gt;

&lt;p&gt;To manually Decrypt, Ansible's got your back this time too :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ansible-vault decrypt vault.yml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It's not recommended to decrypt, Decrypt it if you don't want to encrypt it again by the same scheme rather use view and edit functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Your projects should have all of the information required to successfully install and configure complex systems. However, some configuration data is by definition sensitive and should not be publicly exposed. In this guide, we demonstrated some basic functionality used to encrypt and decrypt valuable data which we can further use in our Playbooks and Roles.&lt;/p&gt;

&lt;p&gt;If you want to go to more Depth of Ansible-Vault, I would recommend checking the following links:&lt;br&gt;
&lt;a href="https://docs.ansible.com/ansible/latest/user_guide/vault.html"&gt;Ansible Documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-use-vault-to-protect-sensitive-ansible-data-on-ubuntu-16-04"&gt;Article on Digital Ocean&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ansible</category>
      <category>ansiblevault</category>
      <category>googlecodein</category>
      <category>automation</category>
    </item>
  </channel>
</rss>
