Day 56: Ansible Hands-on

Day 56: Ansible Hands-on

ยท

4 min read

๐Ÿ› ๏ธ Step 1: Install Ansible

First, we need to install Ansible on your control machine.

  1. Update your package manager:

     shCopy codesudo apt update
    
  2. Install Ansible:

     shCopy codesudo apt install ansible
    
  3. Verify the installation:

     shCopy codeansible --version
    

    You should see the Ansible version and configuration details.

๐Ÿ“‚ Step 2: Set Up Your Inventory

An inventory file defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate.

  1. Create an inventory file:

     shCopy codenano ~/inventory
    
  2. Add your hosts:

     iniCopy code[webservers]
     webserver1 ansible_host=192.168.1.101
    
     [dbservers]
     dbserver1 ansible_host=192.168.1.102
    

๐Ÿ”‘ Step 3: Configure SSH Access

Ensure you can SSH into your hosts from the control machine without a password.

  1. Generate an SSH key:

     shCopy codessh-keygen
    
  2. Copy the SSH key to your hosts:

     shCopy codessh-copy-id user@webserver1
     ssh-copy-id user@dbserver1
    

๐Ÿ“œ Step 4: Write Your First Playbook

Ansible playbooks are written in YAML. Let's create a playbook to install NGINX on web servers.

  1. Create a playbook file:

     shCopy codenano install_nginx.yml
    
  2. Add the playbook content:

     yamlCopy code---
     - name: Install NGINX on web servers
       hosts: webservers
       become: yes
    
       tasks:
         - name: Install NGINX
           apt:
             name: nginx
             state: present
    

โ–ถ๏ธ Step 5: Run the Playbook

Execute the playbook to apply the desired configurations on the target hosts.

  1. Run the playbook:

     shCopy codeansible-playbook -i ~/inventory install_nginx.yml
    
  2. Check the status: Ansible will show you the tasks being executed and their status.

๐Ÿงช Step 6: Verify the Changes

Verify that NGINX has been installed and is running on your web server.

  1. SSH into the web server:

     shCopy codessh user@webserver1
    
  2. Check the NGINX status:

     shCopy codesudo systemctl status nginx
    

You should see that NGINX is active and running.

๐Ÿ“‹ Step 7: Using Variables

Variables make your playbooks more flexible and reusable.

  1. Update the playbook to use variables:

     yamlCopy code---
     - name: Install NGINX on web servers
       hosts: webservers
       become: yes
    
       vars:
         nginx_package: nginx
    
       tasks:
         - name: Install NGINX
           apt:
             name: "{{ nginx_package }}"
             state: present
    

๐Ÿ“ฆ Step 8: Modularize with Roles

Roles allow you to organize tasks, variables, files, and handlers in a structured way.

  1. Create a role directory structure:

     shCopy codemkdir -p roles/nginx/{tasks,handlers,files,templates,vars,defaults,meta}
    
  2. Move your tasks to a role:

     shCopy codenano roles/nginx/tasks/main.yml
    
  3. Add the task content:

     yamlCopy code---
     - name: Install NGINX
       apt:
         name: "{{ nginx_package }}"
         state: present
    
  4. Update the playbook to use the role:

     yamlCopy code---
     - name: Install NGINX on web servers
       hosts: webservers
       become: yes
    
       vars:
         nginx_package: nginx
    
       roles:
         - nginx
    

๐Ÿ“ Step 9: Documentation and Comments

Add comments to your playbooks and roles for better readability and maintenance.

yamlCopy code---
- name: Install NGINX on web servers
  hosts: webservers
  become: yes

  # Variables used in the playbook
  vars:
    nginx_package: nginx

  # Define roles to be executed
  roles:
    - nginx

๐Ÿ“ˆ Step 10: Testing and Validation

Regularly test your playbooks to ensure they work as expected. Use tools like Molecule for role testing.

  1. Install Molecule:

     shCopy codepip install molecule docker
    
  2. Initialize a Molecule scenario:

     shCopy codemolecule init scenario -r nginx -d docker
    
  3. Run the tests:

     shCopy codemolecule test
    

๐Ÿ” Step 11: Secure Your Secrets

Use Ansible Vault to encrypt sensitive data like passwords and API keys.

  1. Create an encrypted variable file:

     shCopy codeansible-vault create secrets.yml
    
  2. Include the encrypted file in your playbook:

     yamlCopy code---
     - name: Install NGINX on web servers
       hosts: webservers
       become: yes
       vars_files:
         - secrets.yml
    
       roles:
         - nginx
    
  3. Edit the encrypted file:

     shCopy codeansible-vault edit secrets.yml
    

๐Ÿ’ก Conclusion

By following these steps, you have learned how to set up Ansible, write and run playbooks, use variables, modularize with roles, document your code, test your configurations, and handle secrets securely. Ansible makes IT automation simple, efficient, and powerful. Happy automating! ๐Ÿš€

ย