Day04 - harishgorla5/Ansible GitHub Wiki

πŸ§‘β€πŸ« Ansible Vault, Tags, Error Handling & Final Project


πŸ” 1. Ansible Vault β€” Secure Sensitive Data

βœ… What is Ansible Vault?

Ansible Vault lets you encrypt secrets like passwords, API tokens, and keys β€” so they don’t appear in plain text in your playbooks or variables.


πŸ”Έ Create an Encrypted File

ansible-vault create secrets.yml

Enter your secret content:

db_password: SuperSecure123

πŸ”Έ Use Encrypted Vars in Playbook

vars_files:
  - secrets.yml

tasks:
  - name: Show password
    debug:
      msg: "Password is {{ db_password }}"

πŸ”Έ Commands

Command Description
ansible-vault edit file.yml Edit encrypted file
ansible-vault view file.yml View contents
ansible-vault encrypt file.yml Encrypt existing file
ansible-vault decrypt file.yml Decrypt a file

πŸ”’ Run Playbook with Vault

If using a password file:

ansible-playbook play.yml --vault-password-file pass.txt

🏷️ 2. Tags β€” Run Specific Parts of a Playbook

βœ… Use Case:

Run only selected tasks instead of the full playbook.


πŸ”Έ Example:

tasks:
  - name: Install Apache
    yum:
      name: httpd
      state: present
    tags: apache

  - name: Install MariaDB
    yum:
      name: mariadb-server
      state: present
    tags: db

πŸ”Έ Run with Tag

ansible-playbook setup.yml --tags apache

Skip a tag:

ansible-playbook setup.yml --skip-tags db

πŸ›‘ 3. Error Handling in Ansible

By default, if a task fails, Ansible stops execution.


πŸ”Έ Ignore Task Failure

- name: Try to stop nginx
  service:
    name: nginx
    state: stopped
  ignore_errors: true

πŸ”Έ Block / Rescue / Always (like try/catch/finally)

tasks:
  - block:
      - name: Install app
        command: /usr/bin/install_app
    rescue:
      - name: Send failure notification
        debug:
          msg: "Installation failed!"
    always:
      - name: Always cleanup temp files
        file:
          path: /tmp/install.log
          state: absent

πŸ§ͺ 4. Final Project β€” Combine All Concepts

🧩 Goal:

Build a real-world multi-role, dynamic, secure automation with:

  • Apache and MariaDB roles
  • Encrypted DB password
  • Custom homepage with template
  • Use tags and error handling

πŸ“ Directory Structure:

project/
β”œβ”€β”€ inventory
β”œβ”€β”€ vault_pass.txt
β”œβ”€β”€ secrets.yml (encrypted)
β”œβ”€β”€ site.yml
β”œβ”€β”€ roles/
β”‚   β”œβ”€β”€ apache/
β”‚   β”‚   β”œβ”€β”€ tasks/main.yml
β”‚   β”‚   └── templates/index.html.j2
β”‚   └── mariadb/
β”‚       β”œβ”€β”€ tasks/main.yml
β”‚       └── templates/my.cnf.j2

πŸ”Έ secrets.yml (Encrypted)

db_root_password: MyS3cr3t!

πŸ”Έ site.yml

- name: Web Role
  hosts: web
  become: true
  roles:
    - apache

- name: DB Role
  hosts: db
  become: true
  vars_files:
    - secrets.yml
  roles:
    - mariadb

πŸ”Έ roles/apache/tasks/main.yml

- name: Install Apache
  yum:
    name: httpd
    state: present
  tags: apache

- name: Deploy homepage
  template:
    src: index.html.j2
    dest: /var/www/html/index.html
  notify: Restart Apache

handlers:
  - name: Restart Apache
    service:
      name: httpd
      state: restarted

πŸ”Έ roles/mariadb/tasks/main.yml

- block:
    - name: Install MariaDB
      yum:
        name: mariadb-server
        state: present

    - name: Configure DB
      template:
        src: my.cnf.j2
        dest: /etc/my.cnf

    - name: Set root password
      mysql_user:
        name: root
        password: "{{ db_root_password }}"
        login_unix_socket: /var/lib/mysql/mysql.sock

  rescue:
    - name: Log DB setup failure
      debug:
        msg: "MariaDB setup failed!"

βœ… Summary (End of Course)

Topic Mastered
Vault βœ…
Tags βœ…
Error Handling βœ…
Roles, Templates, Variables βœ…
Project Use Case βœ