<?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>DEV Community: Jeremy Stretch</title>
    <description>The latest articles on DEV Community by Jeremy Stretch (@j3zz3r).</description>
    <link>https://dev.to/j3zz3r</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%2F2281108%2Fe94ac2eb-3175-40ee-acff-35da90d34b40.png</url>
      <title>DEV Community: Jeremy Stretch</title>
      <link>https://dev.to/j3zz3r</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/j3zz3r"/>
    <language>en</language>
    <item>
      <title>Git Remotes Cheatsheet: Mastering Remote Repository Management</title>
      <dc:creator>Jeremy Stretch</dc:creator>
      <pubDate>Thu, 07 Nov 2024 21:18:15 +0000</pubDate>
      <link>https://dev.to/j3zz3r/git-remote-cheatsheet-168n</link>
      <guid>https://dev.to/j3zz3r/git-remote-cheatsheet-168n</guid>
      <description>&lt;h3&gt;
  
  
  Who is this for?
&lt;/h3&gt;

&lt;p&gt;Developers who need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Work with GitHub, GitLab, or other remote Git servers&lt;/li&gt;
&lt;li&gt;Manage remote repositories&lt;/li&gt;
&lt;li&gt;Collaborate with team members&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is covered?
&lt;/h3&gt;

&lt;p&gt;This cheat sheet provides a comprehensive overview of Git remotes, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Essential remote operations and commands&lt;/li&gt;
&lt;li&gt;Common terminology and concepts&lt;/li&gt;
&lt;li&gt;Repository structures and relationships&lt;/li&gt;
&lt;li&gt;Best practices for remote collaboration&lt;/li&gt;
&lt;li&gt;Common workflows and scenarios&lt;/li&gt;
&lt;li&gt;Troubleshooting tips&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whether you're working on personal projects or collaborating in a team, this guide will help you understand and effectively manage remote repositories.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are Git Remotes?
&lt;/h3&gt;

&lt;p&gt;Git remotes are versions of your repository hosted on the Internet or network. They enable collaboration and code sharing between team members.&lt;/p&gt;

&lt;h3&gt;
  
  
  Remote Commands
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# List all remotes and their URLs&lt;/span&gt;
git remote &lt;span class="nt"&gt;-v&lt;/span&gt;

&lt;span class="c"&gt;# Show remote branch details&lt;/span&gt;
git ls-remote &amp;lt;remote_name&amp;gt; &amp;lt;branch_name&amp;gt;

&lt;span class="c"&gt;# Extract remote URL from Git config&lt;/span&gt;
git config &lt;span class="nt"&gt;--get&lt;/span&gt; remote.origin.url
&lt;span class="c"&gt;# Alternative: grep "url =" .git/config&lt;/span&gt;

&lt;span class="c"&gt;# Update remote URL&lt;/span&gt;
git remote set-url origin &amp;lt;new_url&amp;gt;

&lt;span class="c"&gt;# Add new remote&lt;/span&gt;
git remote add &amp;lt;remote_name&amp;gt; &amp;lt;remote_url&amp;gt;

&lt;span class="c"&gt;# Remove remote&lt;/span&gt;
git remote remove &amp;lt;remote_name&amp;gt;

&lt;span class="c"&gt;# Rename remote&lt;/span&gt;
git remote rename &amp;lt;old_name&amp;gt; &amp;lt;new_name&amp;gt;

&lt;span class="c"&gt;# Prune deleted remote branches&lt;/span&gt;
git remote prune &amp;lt;remote_name&amp;gt;

&lt;span class="c"&gt;# Fetch from remote&lt;/span&gt;
git fetch &amp;lt;remote_name&amp;gt;

&lt;span class="c"&gt;# Fetch specific branch from remote&lt;/span&gt;
git fetch &amp;lt;remote_name&amp;gt; &amp;lt;branch_name&amp;gt;

&lt;span class="c"&gt;# Fetch all remote branches and tags&lt;/span&gt;
git fetch &lt;span class="nt"&gt;--all&lt;/span&gt; &lt;span class="nt"&gt;--tags&lt;/span&gt;

&lt;span class="c"&gt;# Pull from remote (fetch + merge)&lt;/span&gt;
git pull &amp;lt;remote_name&amp;gt; &amp;lt;branch_name&amp;gt;

&lt;span class="c"&gt;# Push to remote&lt;/span&gt;
git push &amp;lt;remote_name&amp;gt; &amp;lt;branch_name&amp;gt;

&lt;span class="c"&gt;# Push to specific remote and branch&lt;/span&gt;
git push &amp;lt;remote_name&amp;gt; &amp;lt;local_branch&amp;gt;:&amp;lt;remote_branch&amp;gt;

&lt;span class="c"&gt;# Force push (use with caution!)&lt;/span&gt;
git push &lt;span class="nt"&gt;--force-with-lease&lt;/span&gt;  &lt;span class="c"&gt;# Safer alternative to --force&lt;/span&gt;
                             &lt;span class="c"&gt;# Acts as a safety mechanism by checking if the remote&lt;/span&gt;
                             &lt;span class="c"&gt;# branch has been modified since your last fetch.&lt;/span&gt;
                             &lt;span class="c"&gt;# This helps prevent accidentally overwriting team&lt;/span&gt;
                             &lt;span class="c"&gt;# members' work, which can happen with a regular&lt;/span&gt;
                             &lt;span class="c"&gt;# --force push.&lt;/span&gt;

&lt;span class="c"&gt;# Check remote tracking branches&lt;/span&gt;
git branch &lt;span class="nt"&gt;-vv&lt;/span&gt;

&lt;span class="c"&gt;# Remove stale remote-tracking branches&lt;/span&gt;
git remote prune origin &lt;span class="nt"&gt;--dry-run&lt;/span&gt;  &lt;span class="c"&gt;# Preview what will be pruned&lt;/span&gt;
git remote prune origin            &lt;span class="c"&gt;# Actually prune&lt;/span&gt;
git fetch &lt;span class="nt"&gt;--prune&lt;/span&gt;                  &lt;span class="c"&gt;# Fetch and prune in one command&lt;/span&gt;

&lt;span class="c"&gt;# Mirror a repository (including all refs)&lt;/span&gt;
git clone &lt;span class="nt"&gt;--mirror&lt;/span&gt; &amp;lt;repository_url&amp;gt;

&lt;span class="c"&gt;# Verify remote connections&lt;/span&gt;
git remote verify &amp;lt;remote_name&amp;gt;

&lt;span class="c"&gt;# Sync fork with upstream&lt;/span&gt;
git fetch upstream
git checkout main
git merge upstream/main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Terminology
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Repository Concepts
repository  - A directory containing your project and its version history
local       - Your copy of the repository on your machine
remote      - A repository hosted on the internet or network (GitHub, GitLab, etc.)
fork        - A copy of someone else's repository under your account
clone       - A local copy of a remote repository

# Common Remote Names
origin      - The default name Git gives to the primary remote repository you cloned from
upstream    - Commonly used name for the original repository you forked from

# Branches and References
main/master - The default primary branch name (main is the new standard, master was historical)
HEAD        - A pointer to the current branch/commit you're working on
tracking branch - A local branch that tracks a remote branch (e.g., main tracks origin/main)

# Data Transfer Operations
fetch       - Download changes from remote without integrating them
pull        - Download changes from remote and integrate them (fetch + merge)
push        - Upload your local changes to a remote repository

# Merging and Integration
merge       - Combine changes from one branch into another
fast-forward - A merge where no new commit is created (target branch just moves forward)
merge commit - A new commit created when combining changes from two branches
conflict    - When changes in two branches modify the same code and cannot auto-merge
rebase      - Alternative to merge: replays your changes on top of target branch
cherry-pick - Apply a specific commit from one branch to another
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Repository Structure Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Your Local Repo (local)
    │
    ├── origin (your fork on GitHub)
    │     └── main branch
    │
    └── upstream (original repository)
          └── main branch

Example:
- upstream: https://github.com/original-author/project
- origin: https://github.com/your-username/project
- local: /home/your-username/project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Remote Setups Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Simple Setup (Direct):
local ↔ origin (primary remote repository)

Fork Setup (Contributing to others):
local ↔ origin (your fork) ↔ upstream (original repository)

Multiple Remotes:
local ↔ origin (primary)
     ↔ staging (testing server)
     ↔ production (live server)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Best Practices
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Always verify the remote URL before pushing sensitive code&lt;/li&gt;
&lt;li&gt;Use SSH over HTTPS when possible for better security&lt;/li&gt;
&lt;li&gt;Set up SSH keys with passphrase for secure authentication&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;--force-with-lease&lt;/code&gt; instead of &lt;code&gt;--force&lt;/code&gt; to prevent overwriting others' work&lt;/li&gt;
&lt;li&gt;Regularly sync with upstream repositories when working with forks&lt;/li&gt;
&lt;li&gt;Set up branch protection rules on remote repositories for critical branches&lt;/li&gt;
&lt;li&gt;Use meaningful names for remotes (e.g., 'upstream' for original repo, 'origin' for fork)&lt;/li&gt;
&lt;li&gt;Configure remote-tracking branches explicitly for better control&lt;/li&gt;
&lt;li&gt;Implement backup strategies using multiple remotes&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;.gitignore&lt;/code&gt; properly to avoid pushing sensitive or unnecessary files&lt;/li&gt;
&lt;li&gt;Review remote changes before merging using &lt;code&gt;git fetch&lt;/code&gt; + &lt;code&gt;git diff&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Document your repository structure and Git workflows in a README.md file&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Key Points
&lt;/h3&gt;

&lt;p&gt;Working with remotes is a fundamental part of Git and collaborative development. Remember these key points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Always synchronise with your remote before starting new work&lt;/li&gt;
&lt;li&gt;Regularly commit and push your changes to avoid large, complex merges&lt;/li&gt;
&lt;li&gt;Use meaningful commit messages to help your colleagues understand your changes&lt;/li&gt;
&lt;li&gt;Keep your local repository organised and regularly prune old branches&lt;/li&gt;
&lt;li&gt;Document your team's Git workflow to maintain consistency&lt;/li&gt;
&lt;li&gt;Consider using Git aliases for frequently used remote commands&lt;/li&gt;
&lt;li&gt;Back up your work regularly by pushing to remote repositories&lt;/li&gt;
&lt;li&gt;When in doubt, create a new branch rather than working directly on main&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  For more detailed information, consult:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Git official documentation: &lt;a href="https://git-scm.com/doc" rel="noopener noreferrer"&gt;https://git-scm.com/doc&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;GitHub Guides: &lt;a href="https://guides.github.com/" rel="noopener noreferrer"&gt;https://guides.github.com/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Your team's internal Git guidelines&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>devops</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Ansible Vault Cheatsheet: Mastering Secure Configuration Management</title>
      <dc:creator>Jeremy Stretch</dc:creator>
      <pubDate>Sat, 26 Oct 2024 15:35:05 +0000</pubDate>
      <link>https://dev.to/j3zz3r/ansible-vault-cheatsheet-mastering-secure-configuration-management-4i3n</link>
      <guid>https://dev.to/j3zz3r/ansible-vault-cheatsheet-mastering-secure-configuration-management-4i3n</guid>
      <description>&lt;h2&gt;
  
  
  What is Ansible Vault?
&lt;/h2&gt;

&lt;p&gt;Ansible Vault is a feature in Ansible that allows you to encrypt and protect sensitive data (such as passwords, API keys, etc.) that needs to be included in your Ansible projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use a Vault Password?
&lt;/h2&gt;

&lt;p&gt;Ansible Vault uses a password to encrypt and decrypt files. This is useful for keeping sensitive information secure, as it allows you to store encrypted content in version control systems without exposing sensitive data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Benefits of Using a Vault Password:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Security&lt;/strong&gt;: Keeps sensitive data encrypted and safe.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Version Control&lt;/strong&gt;: Encrypted files can be safely pushed to repositories like Git.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Ease of Use&lt;/strong&gt;: Once configured, using vault-encrypted files in playbooks is simple.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Example Use Case: Storing Database Credentials Securely
&lt;/h2&gt;

&lt;p&gt;Let's assume we want to store database credentials (username and password) in a secure file for use in an Ansible playbook.&lt;/p&gt;




&lt;h3&gt;
  
  
  1. &lt;strong&gt;Create a New Vault File&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To create a new encrypted vault file that stores database credentials:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ansible-vault create db_credentials.yml&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;This command will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Prompt for a password (which will be used for encryption).&lt;/li&gt;
&lt;li&gt;  Open the default text editor (e.g., Vim) where you can add your sensitive data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Add the following content to &lt;code&gt;db_credentials.yml&lt;/code&gt; (sample database credentials):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db_user: admin
db_password: secure_password123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you save and exit, the file will be encrypted.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. &lt;strong&gt;Edit an Existing Vault File&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To edit the vault file (e.g., if you need to update the database password), use the &lt;code&gt;ansible-vault edit&lt;/code&gt; command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ansible-vault edit db_credentials.yml&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;This will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Prompt for the vault password to decrypt the file.&lt;/li&gt;
&lt;li&gt;  Open the file in your default editor, allowing you to make changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Update the password (or any other value), then save and exit to re-encrypt the file automatically.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. &lt;strong&gt;Encrypting Individual Strings&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Sometimes you may want to encrypt just a single string rather than an entire file. This is useful for storing sensitive data in otherwise unencrypted files.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ansible-vault encrypt_string 'secret_password' --name 'db_password'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will output an encrypted version of 'secret_password' that you can paste into a YAML file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db_password: !vault |
          $ANSIBLE_VAULT;1.1;AES256
          66386439653236336462626566653063336164663966303231363934653561363132333162393533
          3661643066663534383564343537343334633431346664310a316465383138636532343463633236
          37623064636339623565626265353466613262366165396233396465636135353863376136393132
          3938626664623838350a653839636539636465626565316130383833623733326132366265376461
          6233
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  4. &lt;strong&gt;Using Multiple Vault Passwords&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When working with different environments (e.g., development and production), you might want to use different vault passwords for each.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1. Create vault-encrypted files with different IDs:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;ansible-vault create --vault-id dev@prompt secret_dev.yml&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ansible-vault create --vault-id prod@prompt secret_prod.yml&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;2. Use these vault IDs in your playbook:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;ansible-playbook site.yml --vault-id dev@prompt --vault-id prod@prompt&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This allows you to use different passwords for different environments, enhancing security.&lt;/p&gt;




&lt;h3&gt;
  
  
  5. &lt;strong&gt;View the Contents of a Vault File&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To view the encrypted file without editing it, use the &lt;code&gt;ansible-vault view&lt;/code&gt; command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ansible-vault view db_credentials.yml&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;This command will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Prompt for the vault password.&lt;/li&gt;
&lt;li&gt;  Display the decrypted content of the vault file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is useful when you only need to see the values without making changes.&lt;/p&gt;




&lt;h3&gt;
  
  
  6. &lt;strong&gt;Encrypt an Existing File&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If you already have a plain-text file and want to encrypt it using Ansible Vault, you can use:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ansible-vault encrypt plain_file.yml&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;This will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Encrypt the contents of &lt;code&gt;plain_file.yml&lt;/code&gt; and overwrite the file with its encrypted version.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  7. &lt;strong&gt;Decrypt an Encrypted Vault File&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If you need to decrypt a vault file and revert it to plain text, you can use:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ansible-vault decrypt db_credentials.yml&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;This will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Prompt for the vault password.&lt;/li&gt;
&lt;li&gt;  Decrypt the file, leaving it as plain text.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  8. &lt;strong&gt;Running a Playbook with Encrypted Variables&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If your playbook includes variables from a vault-encrypted file (like &lt;code&gt;db_credentials.yml&lt;/code&gt;), you can run the playbook by providing the vault password:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ansible-playbook site.yml --ask-vault-pass&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Alternatively, you can specify a password file for automation purposes:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ansible-playbook site.yml --vault-password-file /path/to/vault_pass.txt&lt;/code&gt; &lt;/p&gt;

&lt;h4&gt;
  
  
  Adding and Using Vault-Encrypted Variables in a Playbook
&lt;/h4&gt;

&lt;p&gt;To use the encrypted variables from &lt;code&gt;db_credentials.yml&lt;/code&gt; in your playbook:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1. &lt;strong&gt;Include the vault file in your playbook:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Add the following line at the beginning of your playbook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;   &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Include database credentials&lt;/span&gt;
     &lt;span class="na"&gt;include_vars&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
       &lt;span class="na"&gt;file&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;db_credentials.yml&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;2. &lt;strong&gt;Use the decrypted variables in your tasks:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once included, you can use the variables like any other Ansible variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;   &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Configure database connection&lt;/span&gt;
     &lt;span class="na"&gt;mysql_user&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
       &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;{{&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;db_user&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;}}"&lt;/span&gt;
       &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;{{&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;db_password&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;}}"&lt;/span&gt;
       &lt;span class="na"&gt;priv&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;*.*:ALL"&lt;/span&gt;
       &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;localhost"&lt;/span&gt;
       &lt;span class="na"&gt;state&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;present&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;3. &lt;strong&gt;Full playbook example:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;   &lt;span class="s"&gt;---&lt;/span&gt;
   &lt;span class="s"&gt;- hosts&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt; &lt;span class="s"&gt;database_servers&lt;/span&gt;
     &lt;span class="s"&gt;vars_files&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;
       &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;db_credentials.yml&lt;/span&gt;

     &lt;span class="na"&gt;tasks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
       &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Configure database connection&lt;/span&gt;
         &lt;span class="na"&gt;mysql_user&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
           &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;{{&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;db_user&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;}}"&lt;/span&gt;
           &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;{{&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;db_password&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;}}"&lt;/span&gt;
           &lt;span class="na"&gt;priv&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;*.*:ALL"&lt;/span&gt;
           &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;localhost"&lt;/span&gt;
           &lt;span class="na"&gt;state&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;present&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we use &lt;code&gt;vars_files&lt;/code&gt; to include the encrypted file directly in the play.&lt;/p&gt;

&lt;p&gt;Remember to run this playbook with either &lt;code&gt;--ask-vault-pass&lt;/code&gt; or &lt;code&gt;--vault-password-file&lt;/code&gt; as mentioned earlier.&lt;/p&gt;




&lt;h3&gt;
  
  
  9. &lt;strong&gt;Specifying Vault Password in ansible.cfg&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To avoid entering the vault password every time, you can specify the vault password file in your &lt;code&gt;ansible.cfg&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[defaults]
vault_password_file = /path/to/vault_pass.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is especially useful for automation purposes, but be cautious about the security implications of storing your vault password on disk.&lt;/p&gt;




&lt;h3&gt;
  
  
  10. &lt;strong&gt;Changing the Encryption Key (Rekeying)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If you need to change the encryption key of a vault-encrypted file:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ansible-vault rekey secret.yml&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will prompt you for the current vault password and then ask for a new password. It's a good practice to periodically rekey your vault-encrypted files for security reasons.&lt;/p&gt;




&lt;h2&gt;
  
  
  Best Practices for Ansible Vault
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use a Strong Vault Password&lt;/strong&gt;: Always choose a strong and unique password to protect your encrypted files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Version Control Safety&lt;/strong&gt;: It is safe to store vault-encrypted files in version control, but &lt;strong&gt;never&lt;/strong&gt; commit the vault password or password file to version control.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Separate Vault Files&lt;/strong&gt;: If possible, separate sensitive data into dedicated vault files to minimise the exposure of credentials across different environments or teams.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use &lt;code&gt;--vault-password-file&lt;/code&gt; for Automation&lt;/strong&gt;: When automating playbook runs in CI/CD pipelines (e.g., GitLab CI), use the &lt;code&gt;--vault-password-file&lt;/code&gt; option to avoid manual password entry.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Restrict File Access&lt;/strong&gt;: Ensure that only authorised users and systems have access to the vault password file and the encrypted files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Environment-Specific Vaults&lt;/strong&gt;: Use separate vaults for different environments (development, staging, production) to ensure proper security segregation.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Summary of Commands
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Create a new encrypted file&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ansible-vault create secret.yml&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Edit an existing encrypted file&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ansible-vault edit secret.yml&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;View an encrypted file without editing&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ansible-vault view secret.yml&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Encrypt an existing file&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ansible-vault encrypt existing_file.yml&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Decrypt an encrypted file&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ansible-vault decrypt secret.yml&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Encrypt a string&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ansible-vault encrypt_string 'secret_password' --name 'db_password'&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Create encrypted files with different vault IDs&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;ansible-vault create --vault-id dev@prompt secret_dev.yml&lt;/code&gt;&lt;br&gt;&lt;code&gt;ansible-vault create --vault-id prod@prompt secret_prod.yml&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Run a playbook with vault-encrypted files&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;ansible-playbook site.yml --ask-vault-pass&lt;/code&gt;&lt;br&gt;&lt;code&gt;ansible-playbook site.yml --vault-password-file /path/to/vault_pass.txt&lt;/code&gt;&lt;br&gt;&lt;code&gt;ansible-playbook site.yml --vault-id dev@prompt --vault-id prod@prompt&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Change the encryption key of a vault-encrypted file&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ansible-vault rekey secret.yml&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Remember: When using &lt;code&gt;ansible-vault&lt;/code&gt;, you'll be prompted for the vault password unless you specify a password file.&lt;/p&gt;




&lt;h6&gt;
  
  
  Jeremy Stretch 241024
&lt;/h6&gt;

</description>
      <category>ansible</category>
      <category>vault</category>
      <category>security</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
