Ansible Cheat Sheet with Real-Time Use Cases - Part 1π

π Hello! I'm passionate about DevOps and have over 1+ years of experience in the field. I'm proficient in a variety of cutting-edge technologies and always motivated to expand my knowledge and skills. Let's connect and grow together!
SKILLS:
πΉ Languages & Runtimes: Python, Shell Scripting, HCL, YAML πΉ Cloud Technologies: AWS, Microsoft Azure, GCP πΉ Infrastructure Tools: Docker, Terraform, AWS CloudFormation πΉ Other Tools: Linux, Git and GitHub Actions, Jenkins, Jira, GitLab (beginner), Docker, AWS DevOps πΉ Web Development: HTML, CSS, Bootstrap, Python, SQL
Job & Responsibilities:
π Improved development efficiency by implementing CI/CD pipelines, resulting in a 30% reduction in deployment time on the test server. π Strengthened deployment and testing reliability by utilizing Docker containers and optimizing Dockerfile, reducing development issues on the test server by 20%. βοΈ Automated S3 bucket log creation with Shell scripting, eliminating 100% of manual search and saving 2 hours per week. π Scheduled EC2 instance start/stop using Lambda functions and Event Bridge, leading to a 25% decrease in infrastructure costs. π§ Utilized AWS, Linux, Python, Docker, Shell scripting, Terraform, Jenkins Pipelines, and automation to streamline workflows and improve overall system performance.
I'm very detail-oriented and possess strong written and verbal communication skills. As a high performer with a possibility mindset, I strive to solve problems using efficient approaches.
Let's Connect & Grow:
If you find my profile suitable for the role you are searching for, please feel free to reach out to me at sumanprasad9766@gmail.com.
Introduction
Ansible is a powerful open-source automation tool that simplifies the configuration management, application deployment, and task automation processes. This cheat sheet provides a quick reference to commonly used Ansible commands and concepts, accompanied by real-time use cases to illustrate their practical application.
Ansible Cheat Sheet
1. Inventory Management
Command:
ansible-inventoryUse Case: List all hosts in the inventory.
ansible-inventory --list
2. Ad-hoc Commands
Command:
ansibleUse Case: Run a command on all hosts in the inventory.
ansible all -m command -a "uptime"
3. Playbooks
Command:
ansible-playbookUse Case: Execute a playbook.
ansible-playbook deploy-app.yaml
4. Variables
Command:
{{ variable_name }}Use Case: Use a variable in a playbook.
tasks: - name: Ensure package is installed apt: name: "{{ package_name }}" state: present
5. Loops
Command:
with_itemsUse Case: Iterate over a list in a playbook.
tasks: - name: Create users user: name: "{{ item }}" state: present with_items: - user1 - user2
6. Conditionals
Command:
whenUse Case: Add a condition to a task.
tasks: - name: Restart Apache if config file changes service: name: apache2 state: restarted when: "'apache.conf' in ansible.builtin.changed_files"
7. Roles
Command:
ansible-galaxyUse Case: Create a new role.
ansible-galaxy init my-role
8. Vault
Command:
ansible-vaultUse Case: Encrypt sensitive data.
ansible-vault encrypt secrets.yaml
Real-Time Use Cases
Deploying a Web Application:
- Use Ansible to deploy a web application on multiple servers, ensuring consistency and scalability.
Configuring Monitoring Agents:
- Automate the installation and configuration of monitoring agents on servers using Ansible playbooks.
Scaling Infrastructure:
- Dynamically scale your infrastructure by adding or removing servers based on demand using Ansible.
Continuous Integration/Continuous Deployment (CI/CD):
- Integrate Ansible into your CI/CD pipeline to automate deployment tasks and ensure efficient release management.
Database Configuration:
- Use Ansible to automate the setup and configuration of databases, ensuring a standardized environment.
Security Patching:
- Implement Ansible to automate the process of applying security patches across multiple servers simultaneously.
9. Dynamic Inventory
Command:
ansible-inventory --graphUse Case: Display the inventory structure in a graph.
ansible-inventory --graph
10. Handlers
Command:
handlersUse Case: Define a handler to restart a service only if a task triggers it.
handlers: - name: restart apache service: name: apache2 state: restarted
11. Tags
Command:
--tagsand--skip-tagsUse Case: Run specific tasks or skip others based on tags.
ansible-playbook deploy-app.yaml --tags "install,config"
12. Ansible Vault Encryption and Decryption
Command:
ansible-vault encrypt_stringUse Case: Encrypt sensitive strings for use in playbooks.
ansible-vault encrypt_string 'secret_password' --name 'db_password'
13. Custom Modules
Command:
ansible-doc -l | grep your_moduleUse Case: Develop and use custom Ansible modules.
ansible-doc -l | grep custom_module
14. Jinja2 Templating
Command:
{{ variable | filter }}Use Case: Use Jinja2 filters for dynamic content.
tasks: - name: Set dynamic variable set_fact: dynamic_value: "{{ base_value | upper }}"
15. Notify and Wait for Completion
Command:
asyncandpollUse Case: Execute tasks asynchronously and wait for their completion.
tasks: - name: Long-running task command: /path/to/long_running_script.sh async: 300 poll: 0 register: job_result - name: Wait for completion async_status: jid: "{{ job_result.ansible_job_id }}" until: job_result.finished retries: 30
Real-Time Use Cases (Continued)
Automated Backup Strategy:
- Utilize Ansible to create an automated backup strategy, including regular backups and cleanup tasks.
Multi-Environment Deployment:
- Manage deployments across multiple environments (e.g., development, staging, production) using dynamic inventories and environment-specific playbooks.
Infrastructure Monitoring Setup:
- Automate the deployment of monitoring tools and configurations across servers to ensure real-time visibility into infrastructure health.
Custom Module for Application-specific Tasks:
- Develop a custom Ansible module to handle application-specific tasks, providing flexibility in automation.
Rolling Updates:
- Implement rolling updates for services, ensuring zero downtime during updates by using Ansible's serial and max_fail_percentage features.
Git Repository Management:
- Automate tasks related to Git repositories, such as cloning, pulling updates, and managing branches.




