|
| 1 | +--- |
| 2 | +title: "Variables and Ansible Vault" |
| 3 | +sidebar_label: "5. Variables & Security" |
| 4 | +sidebar_position: 5 |
| 5 | +description: "Learn to make your Ansible playbooks dynamic with variables and secure with encrypted Vaults. Perfect for handling different environments and sensitive data!" |
| 6 | +tags: ["Ansible", "Variables", "Vault", "Secrets Management", "Best Practices"] |
| 7 | +keywords: ["Ansible Variables", "Ansible Vault", "Secrets Management", "Dynamic Playbooks", "Industrial Automation"] |
| 8 | +--- |
| 9 | + |
| 10 | +Static playbooks are useful, but "Industrial Level" automation requires flexibility and security. In this guide, we will learn how to use **Variables** to handle different environments (Dev/Prod) and **Ansible Vault** to protect sensitive data. |
| 11 | + |
| 12 | +:::tip Why Variables and Vault? |
| 13 | +* **Variables** allow you to write reusable playbooks that can adapt to different scenarios without changing the code. |
| 14 | +* **Ansible Vault** ensures that sensitive information like passwords and API keys are encrypted and safe from prying eyes, even in version control. |
| 15 | +::: |
| 16 | + |
| 17 | +## 1. Using Variables |
| 18 | + |
| 19 | +Variables in Ansible allow you to write one playbook and use it for multiple purposes. Instead of hardcoding a version number or a username, you use a placeholder. |
| 20 | + |
| 21 | +### Where to Define Variables? |
| 22 | +Ansible has a specific "Precedence" (priority) for variables, but these are the most common places: |
| 23 | + |
| 24 | +1. **Playbook Level:** Directly inside the `.yml` file. |
| 25 | +2. **Inventory Level:** Inside your `hosts.ini`. |
| 26 | +3. **File Level:** In a dedicated `group_vars` or `host_vars` folder. |
| 27 | + |
| 28 | +```yaml title="Example: Playbook with Variables" |
| 29 | +--- |
| 30 | +- name: Deploy CodeHarborHub App |
| 31 | + hosts: webservers |
| 32 | + vars: |
| 33 | + app_version: "v2.0.4" |
| 34 | + node_port: 3000 |
| 35 | + |
| 36 | + tasks: |
| 37 | + - name: Start the application |
| 38 | + command: "node app.js --port {{ node_port }}" |
| 39 | +``` |
| 40 | +
|
| 41 | +:::tip Syntax Note |
| 42 | +Always wrap variables in double curly braces `{{ var_name }}`. If the variable starts the line, you must wrap the entire value in quotes: `"{{ var_name }}"`. |
| 43 | +::: |
| 44 | + |
| 45 | +## 2. Ansible Vault (Securing Secrets) |
| 46 | + |
| 47 | +At **CodeHarborHub**, we **never** push plain-text passwords, SSH keys, or SSL certificates to GitHub. **Ansible Vault** is a built-in feature that encrypts these files so they can be safely stored in version control. |
| 48 | + |
| 49 | +### Common Vault Operations |
| 50 | + |
| 51 | +| Action | Command | |
| 52 | +| :--- | :--- | |
| 53 | +| **Create** | `ansible-vault create secrets.yml` | |
| 54 | +| **Edit** | `ansible-vault edit secrets.yml` | |
| 55 | +| **Encrypt Existing** | `ansible-vault encrypt my_passwords.txt` | |
| 56 | +| **Decrypt** | `ansible-vault decrypt secrets.yml` | |
| 57 | + |
| 58 | +### How to use Vault in a Playbook |
| 59 | + |
| 60 | +1. Create an encrypted file `vars/secrets.yml`: |
| 61 | + ```yaml title="Example: Encrypted Vault File" |
| 62 | + db_password: "SuperSecretPassword123" |
| 63 | + ``` |
| 64 | +2. Reference it in your playbook: |
| 65 | + ```yaml title="Example: Using Vault in Playbook" |
| 66 | + - name: Setup Database |
| 67 | + hosts: dbservers |
| 68 | + vars_files: |
| 69 | + - vars/secrets.yml |
| 70 | + ``` |
| 71 | +3. Run the playbook by providing the password: |
| 72 | + ```bash title="Running Playbook with Vault" |
| 73 | + ansible-playbook site.yml --ask-vault-pass |
| 74 | + ``` |
| 75 | + |
| 76 | +In this example, Ansible will prompt you for the vault password before it can read the encrypted variables. This way, you can safely store sensitive information in your repository without risking exposure. |
| 77 | + |
| 78 | +## 3. Facts: The Special Variables |
| 79 | + |
| 80 | +Ansible automatically discovers information about the Managed Node before running any tasks. These are called **Facts**. |
| 81 | + |
| 82 | +```mermaid |
| 83 | +graph LR |
| 84 | + A[Control Node] -->|Setup Module| B[Managed Node] |
| 85 | + B -->|Returns JSON| C[Facts: OS, IP, RAM, CPU] |
| 86 | + C --> D[Use in Playbook: ansible_os_family] |
| 87 | +``` |
| 88 | + |
| 89 | +**Example: Conditional Logic using Facts** |
| 90 | + |
| 91 | +```yaml title="Example: Using Facts in Playbook" |
| 92 | +- name: Install Web Server |
| 93 | + apt: |
| 94 | + name: apache2 |
| 95 | + state: present |
| 96 | + when: ansible_os_family == "Debian" |
| 97 | +``` |
| 98 | +
|
| 99 | +## Comparison: Variables vs. Vault |
| 100 | +
|
| 101 | +| Feature | Variables | Ansible Vault | |
| 102 | +| :--- | :--- | :--- | |
| 103 | +| **Visibility** | Plain text / Human readable. | Encrypted / Block of gibberish. | |
| 104 | +| **Purpose** | Configuration (Ports, Paths, Names). | Secrets (Passwords, Keys, Tokens). | |
| 105 | +| **Storage** | Committed directly to Git. | Committed to Git (but encrypted). | |
| 106 | +
|
| 107 | +## Industrial Best Practice: `group_vars` |
| 108 | + |
| 109 | +Instead of cluttering your playbook, create a directory structure like this: |
| 110 | + |
| 111 | +```text title="Best Practice: group_vars Directory Structure" |
| 112 | +. |
| 113 | +├── inventory.ini |
| 114 | +├── playbook.yml |
| 115 | +└── group_vars/ |
| 116 | + ├── all.yml # Variables for all servers |
| 117 | + ├── webservers.yml # Specific for web group |
| 118 | + └── dbservers.yml # Specific for DB group |
| 119 | +``` |
| 120 | + |
| 121 | +Ansible will **automatically** load these variables based on the group names in your inventory! This keeps your playbooks clean and organized, making it easier to manage large infrastructures. |
| 122 | + |
| 123 | +## Final Graduation Challenge |
| 124 | + |
| 125 | +1. Create a variable file named `user_config.yml`. |
| 126 | +2. Add a variable `username: chh_admin`. |
| 127 | +3. Create a playbook that creates a user on your local machine using `{{ username }}`. |
| 128 | +4. Now, encrypt `user_config.yml` using `ansible-vault`. |
| 129 | +5. Run the playbook and see how Ansible asks for the password before it can read the file\! |
| 130 | + |
| 131 | +Congratulations! You've just learned how to make your Ansible playbooks dynamic with variables and secure with Vault. This is a crucial step towards becoming an "Industrial Level" DevOps Engineer at CodeHarborHub! |
0 commit comments