Ansible playbooks run multiple tasks, assign roles, and define configurations, deployment steps, and variables. If you’re using multiple servers, Ansible playbooks organize the steps between the assembled machines or servers and get them organized and running in the way the users need them to. Consider playbooks as the equivalent of instruction manuals.
🔶 Task-01
Write an Ansible playbook to create a file on a different server.
--- - name: Create a file on a remote server hosts: all become: yes tasks: - name: Create a file file: path: /home/ubuntu/day58.txt state: touch
ansible-playbook create_file.yml -i /etc/ansible/hosts
Verify that the files are created on different servers.
we can check from the master server too by using the below command.
ansible all -a "ls /home/ubuntu" -i /etc/ansible/hosts
Write an ansible playbook to create a new user.
--- - name: Create a new user hosts: all become: yes tasks: - name: Add a new user chandresh user: name=chandresh
Run ansible-playbook commands:
ansible-playbook create_user.yml -i /etc/ansible/hosts
To check the created user from the master server use the below command:
ansible all -a "cat /etc/passwd" -i /etc/ansible/hosts
Write an Ansible playbook to install docker on a group of servers.
--- - name: Install Docker on multiple servers hosts: all become: yes tasks: - name: Add Docker GPG key apt_key: url: https://download.docker.com/linux/ubuntu/gpg state: present - name: Add Docker APT repository apt_repository: repo: deb https://download.docker.com/linux/ubuntu focal stable state: present - name: Install Docker apt: name: docker.io state: present become: yes - name: Start Docker service systemd: name: docker state: started enabled: yes
To install docker on multiple servers use the below command:
ansible-playbook install_docker.yml -i /etc/ansible/hosts
To verify that Docker has been installed on multiple servers:
ansible all -a "docker --version" -i /etc/ansible/hosts
Watch this video to learn about Ansible Playbooks.
🔶 Task-02: Write a blog about writing Ansible playbooks with the best practices.
Ansible is a powerful open-source automation tool used for configuration management, application deployment, and task automation. One of Ansible's key features is its simplicity and ease of use, making it a popular choice for both beginners and experienced DevOps professionals. To harness the full potential of Ansible, it's crucial to follow best practices when writing playbooks. In this guide, we'll explore some of the best practices for writing Ansible playbooks.
🔶 What Is an Ansible Playbook?
Before diving into best practices, let's briefly understand what an Ansible playbook is. An Ansible playbook is a YAML file that contains a set of tasks and configurations written in a human-readable format. Playbooks define the desired state of the system and the tasks required to achieve that state. These tasks can include package installations, configuration file updates, service restarts, and more.
🔶 Best Practices for Writing Ansible Playbooks
1. Organize Your Playbook Structure
A well-structured playbook is easier to understand and maintain. Here are some structural best practices:
Use Meaningful Names: Give your playbook files and roles meaningful names. This aids in quickly identifying their purpose.
Organize by Role: Group related tasks into roles. Roles are reusable and can be shared across multiple playbooks.
Follow the Directory Structure: Adopt a consistent directory structure for your playbooks, such as roles, tasks, and files. Ansible Galaxy provides a recommended structure.
2. Use Descriptive Task Names
Task names should be clear and concise, describing what each task does. This enhances playbook readability and aids troubleshooting.
- name: Install Apache web server
apt:
name: apache2
state: present
3. Document Your Playbook
Use comments and documentation blocks to explain the playbook's purpose, input variables, and expected outcomes. Documentation makes it easier for others (or your future self) to understand the playbook's intent.
---
- name: Configure a web server
hosts: web_servers
tasks:
- name: Install Apache web server
apt:
name: apache2
state: present
4. Use Variables
Avoid hardcoding values in your playbooks. Instead, use variables to make playbooks more flexible and reusable. Define variables in separate files or use Ansible Vault for sensitive data.
---
- name: Configure a web server
hosts: web_servers
vars:
http_port: 80
tasks:
- name: Install Apache web server
apt:
name: apache2
state: present
vars:
http_port: "{{ http_port }}"
5. Handle Errors Gracefully
Include error handling to ensure your playbook can recover from failures. Use ignore_errors
, failed_when
, or block
and rescue
constructs as needed.
- name: Ensure a package is installed
apt:
name: mypackage
state: present
ignore_errors: yes
6. Test Playbooks Thoroughly
Before deploying playbooks in production, test them in a safe environment. Ansible provides a --syntax-check
option and the ansible-lint
tool for playbook validation.
ansible-playbook --syntax-check myplaybook.yml
ansible-lint myplaybook.yml
7. Use Roles for Reusability
Roles encapsulate sets of tasks, handlers, templates, and variables into reusable components. This promotes modularity and simplifies playbook maintenance.
8. Version Control Your Playbooks
Store your playbooks in a version control system (e.g., Git) to track changes, collaborate with others, and roll back to previous versions if needed.
9. Secure Sensitive Data
Use Ansible Vault to encrypt sensitive data like passwords and API keys. This ensures that confidential information remains secure in your playbooks.
10. Monitor and Maintain Playbooks
Regularly review and update your playbooks. Systems and requirements evolve, and your playbooks should stay in sync. Monitor playbook runs for errors and performance issues.
Conclusion
Writing Ansible playbooks following best practices leads to more efficient, readable, and maintainable automation scripts. Whether you're a beginner or an experienced DevOps engineer, adhering to these guidelines will help you make the most of Ansible's automation capabilities while keeping your infrastructure stable and secure. Happy automating!
Happy Learning!
Stay in the loop with my latest insights and articles on cloud ☁️ and DevOps ♾️ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).
Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.
#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker #Jenkins #Kubernetes #AWS #Ansible