Table of contents
- ๐ ๏ธ Step 1: Install Ansible
- ๐ Step 2: Set Up Your Inventory
- ๐ Step 3: Configure SSH Access
- ๐ Step 4: Write Your First Playbook
- โถ๏ธ Step 5: Run the Playbook
- ๐งช Step 6: Verify the Changes
- ๐ Step 7: Using Variables
- ๐ฆ Step 8: Modularize with Roles
- ๐ Step 9: Documentation and Comments
- ๐ Step 10: Testing and Validation
- ๐ Step 11: Secure Your Secrets
- ๐ก Conclusion
๐ ๏ธ Step 1: Install Ansible
First, we need to install Ansible on your control machine.
Update your package manager:
shCopy codesudo apt update
Install Ansible:
shCopy codesudo apt install ansible
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.
Create an inventory file:
shCopy codenano ~/inventory
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.
Generate an SSH key:
shCopy codessh-keygen
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.
Create a playbook file:
shCopy codenano install_nginx.yml
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.
Run the playbook:
shCopy codeansible-playbook -i ~/inventory install_nginx.yml
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.
SSH into the web server:
shCopy codessh user@webserver1
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.
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.
Create a role directory structure:
shCopy codemkdir -p roles/nginx/{tasks,handlers,files,templates,vars,defaults,meta}
Move your tasks to a role:
shCopy codenano roles/nginx/tasks/main.yml
Add the task content:
yamlCopy code--- - name: Install NGINX apt: name: "{{ nginx_package }}" state: present
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.
Install Molecule:
shCopy codepip install molecule docker
Initialize a Molecule scenario:
shCopy codemolecule init scenario -r nginx -d docker
Run the tests:
shCopy codemolecule test
๐ Step 11: Secure Your Secrets
Use Ansible Vault to encrypt sensitive data like passwords and API keys.
Create an encrypted variable file:
shCopy codeansible-vault create secrets.yml
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
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! ๐