Day 56: Ansible Hands-on
RAHUL SHARMA
DevOps Engineer (Seeking Opportunities as a DevOps Engineer) Linux, git, Github, Docker, kubernetes, terraform, ansible,AWS
Table of contents
Show more
??? Step 1: Install Ansible
First, we need to install Ansible on your control machine.
?? 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.
?? Step 3: Configure SSH Access
Ensure you can SSH into your hosts from the control machine without a password.
?? Step 4: Write Your First Playbook
Ansible playbooks are written in YAML. Let's create a playbook to install NGINX on web servers.
?? Step 5: Run the Playbook
Execute the playbook to apply the desired configurations on the target hosts.
?? Step 6: Verify the Changes
Verify that NGINX has been installed and is running on your web server.
领英推荐
You should see that NGINX is active and running.
?? Step 7: Using Variables
Variables make your playbooks more flexible and reusable.
?? Step 8: Modularize with Roles
Roles allow you to organize tasks, variables, files, and handlers in a structured way.
?? Step 9: Documentation and Comments
Add comments to your playbooks and roles for better readability and maintenance.
COPY
COPY
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.
?? Step 11: Secure Your Secrets
Use Ansible Vault to encrypt sensitive data like passwords and API keys.
?? 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! ??