Skip to main content

Command Palette

Search for a command to run...

Ansible Cheat Sheet with Real-Time Use Cases - Part 3πŸš€

Published
β€’4 min read
Ansible Cheat Sheet with Real-Time Use Cases - Part 3πŸš€
P

πŸ‘‹ 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:

These advanced scenarios cover a range of use cases from environment-specific configurations to CI/CD pipeline integration and advanced error handling. Feel free to adapt and customize them based on your specific automation needs.

41. Multi-Environment Configuration

Command: --extra-vars

Use Case: Dynamically configure settings for different environments during playbook execution.

ansible-playbook deploy.yaml --extra-vars "env=staging"

42. Conditional Task Execution

Command: when

Use Case: Execute tasks conditionally based on certain criteria.

- name: Perform task only on Linux
  command: /path/to/linux_script.sh
  when: "'Linux' in ansible_os_family"

43. Docker Container Orchestration

Command: docker_container

Use Case: Orchestrate Docker containers with Ansible for seamless deployment and management.

- name: Start Docker container
  docker_container:
    name: my_container
    image: my_image:latest
    state: started

44. Managing SSH Keys

Command: authorized_key

Use Case: Manage SSH keys on remote hosts for secure access.

- name: Add SSH key for user
  authorized_key:
    user: my_user
    key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

45. System Monitoring Setup

Command: cron

Use Case: Schedule periodic system monitoring tasks with Ansible.

- name: Schedule monitoring script
  cron:
    name: "Run Monitoring Script"
    minute: "0"
    hour: "*/2"
    job: "/path/to/monitoring_script.sh"

46. Service Discovery Integration

Command: uri

Use Case: Integrate Ansible with service discovery tools for dynamic inventory updates.

- name: Register server with Consul
  uri:
    url: "http://consul-server:8500/v1/agent/service/register"
    method: POST
    body: '{"ID": "web-server", "Name": "web", "Port": 80}'

47. Custom Security Policies

Command: lineinfile

Use Case: Enforce custom security policies by modifying specific lines in configuration files.

- name: Set custom security policy
  lineinfile:
    path: /etc/security/policy.conf
    line: "ENFORCE_CUSTOM_POLICY=yes"

48. CI/CD Pipeline Integration (GitLab)

Command: Use Ansible playbooks in GitLab CI/CD pipelines.

Use Case: Seamless integration of Ansible with GitLab for continuous deployment.

stages:
  - deploy

deploy:
  script:
    - ansible-playbook deploy.yaml

49. Managing Environment Variables

Command: environment

Use Case: Set environment variables for tasks during playbook execution.

- name: Run script with custom environment
  command: /path/to/script.sh
  environment:
    CUSTOM_VAR: "value"

50. Advanced Error Handling

Command: block, rescue, always

Use Case: Implement advanced error handling with try-catch blocks.

- name: Advanced Error Handling
  block:
    - name: Attempt a risky task
      command: /path/to/risky_script.sh
  rescue:
    - name: Handle errors gracefully
      debug:
        msg: "Task failed, but we caught it!"
  always:
    - name: Always run cleanup task
      command: /path/to/cleanup_script.sh

51. Network Device Configuration

Command: ios_command (for Cisco devices)

Use Case: Automate configuration changes on network devices.

- name: Configure Cisco router
  ios_command:
    commands:
      - interface GigabitEthernet0/0
      - ip address 192.168.1.1 255.255.255.0
  delegate_to: network_device

52. Cloud Resource Provisioning (AWS)

Command: ec2_instance

Use Case: Dynamically provision EC2 instances on AWS.

- name: Launch EC2 instance
  ec2_instance:
    name: my_instance
    instance_type: t2.micro
    image: ami-12345678
    key_name: my_key
    count: 1
    tags:
      - key: Name
        value: MyInstance

53. Custom Module Development

Command: ansible.module_utils

Use Case: Develop custom Ansible modules to extend functionality.

# Custom module_utils/my_module.py
def my_custom_function():
    return "Hello from my custom module!"

# Playbook using the custom module
- name: Use custom module
  debug:
    msg: "{{ lookup('module_utils', 'my_module.my_custom_function') }}"

54. HashiCorp Vault Integration

Command: hashi_vault

Use Case: Integrate Ansible with HashiCorp Vault for secure secret management.

- name: Retrieve secret from Vault
  hashi_vault:
    path: secret/data/myapp
    field: api_key
  register: vault_result

- name: Use secret in playbook
  debug:
    msg: "API Key: {{ vault_result.secret }}"

55. Windows Server Configuration

Command: win_command, win_shell

Use Case: Configure settings on Windows servers.

- name: Set Windows registry key
  win_shell: New-ItemProperty -Path 'HKLM:\Software\MyApp' -Name 'Setting' -Value '123' -PropertyType 'String'

56. Custom Logging and Auditing

Command: log_callback

Use Case: Implement a custom callback plugin for detailed logging.

export ANSIBLE_STDOUT_CALLBACK=my_custom_logging_callback
ansible-playbook deploy.yaml

57. Container Orchestration (Kubernetes)

Command: k8s

Use Case: Deploy and manage applications in Kubernetes clusters.

- name: Deploy app in Kubernetes
  k8s:
    state: present
    definition:
      apiVersion: v1
      kind: Pod
      metadata:
        name: mypod
      spec:
        containers:
          - name: mycontainer
            image: myimage:latest

58. Git Repository Management

Command: git_config

Use Case: Configure Git settings for automation.

- name: Set Git configuration
  git_config:
    key: user.email
    value: "ansible@company.com"
  delegate_to: localhost

59. Service Scaling in Cloud (Azure)

Command: azure_rm_virtualmachine

Use Case: Dynamically scale the number of virtual machines in Azure.

- name: Scale VM instances in Azure
  azure_rm_virtualmachine:
    resource_group: my_resource_group
    name: my_vm
    vm_size: Standard_DS2_v2
    count: 3
    state: present

60. Dynamic Inventory with Service Discovery

Command: script (for custom dynamic inventory script)

Use Case: Develop a custom dynamic inventory script for fetching host information.

# Custom inventory script (custom_inventory.py)
#!/usr/bin/env python
print('{"web_servers": ["server1", "server2"], "app_servers": ["server3"], "all": {"children": ["web_and_app_servers"]}}')
ansible-inventory -i custom_inventory.py --list

These scenarios cover a range of advanced use cases, including network device configuration, cloud resource provisioning, custom module development, and integration with external tools like HashiCorp Vault and Kubernetes. Feel free to adapt these examples to suit your specific automation requirements.

More from this blog

D

DeployToCloud

405 posts

πŸ‘‹ Welcome to my Hashnode blog! I'm a DevOps Engineer with 2+ years of experience. Join ~5k followers and explore 320+ blogs on Python, AWS, Docker, Jenkins, Linux, and more. Let's connect & grow πŸš€