Skip to main content

Command Palette

Search for a command to run...

Ansible

Updated
6 min readView as Markdown
Ansible
S

*Shreyash Bhise | Aspiring Mern Stack Developer and DevOps enthusiast,

Here's a simple Ansible playbook with two tasks: one task to install Docker and another task to install Kubernetes components (Kubectl and Minikube):

---
- name: Simple Playbook for Docker and Kubernetes
  hosts: target_hosts
  become: true

  tasks:
    - name: Install Docker
      apt:
        name: docker.io
        state: present

    - name: Install Kubernetes components
      apt:
        name: 
          - kubectl
          - minikube
        state: present

In this playbook:

  • The play is named "Simple Playbook for Docker and Kubernetes" and targets the hosts specified in the target_hosts group.

  • The become: true statement allows the playbook to run with escalated privileges, typically using sudo.

  • The playbook consists of two tasks:

    • The first task uses the apt module to install Docker using the package docker.io on the target hosts.

    • The second task uses the apt module to install Kubernetes components, including kubectl and minikube.

To execute this playbook, make sure you have an inventory file (inventory.ini) that lists the target hosts under the [target_hosts] group.

Run the playbook using the following command in the terminal:

ansible-playbook -i inventory.ini playbook.yml

This playbook will install Docker and Kubernetes components (kubectl and Minikube) on the target hosts.

Keep in mind that the playbook assumes you are targeting Debian/Ubuntu-based systems. If your target hosts are using a different package manager or operating system, you may need to adjust the playbook accordingly.

As always, please ensure that you are running this playbook on test or development environments first, and thoroughly understand the implications of installing Docker and Kubernetes components on your production systems before proceeding.

Here's another simple Ansible playbook to install Nginx, this time using the apt module for package management on Debian/Ubuntu systems and including a task to start the Nginx service:

---
- name: Install and Start Nginx
  hosts: webservers
  become: true
  tasks:
    - name: Install Nginx package
      apt:
        name: nginx
        state: present

    - name: Start Nginx service
      service:
        name: nginx
        state: started
        enabled: yes

In this playbook:

  • The play is named "Install and Start Nginx" and targets the hosts in the webservers group.

  • The become: true statement allows the playbook to run with escalated privileges.

  • Two tasks are defined:

    • The first task uses the apt module to install the Nginx package, specifying the package name (nginx) and the desired state (present).

    • The second task uses the service module to start the Nginx service, ensuring it is running and enabled to start at boot (enabled: yes).

Make sure you have an inventory file (e.g., inventory.ini) that lists the target hosts under the [webservers] group.

Run the playbook using the following command:

ansible-playbook -i inventory.ini playbook.yml

This playbook will install Nginx on the target hosts defined in the inventory using the apt package manager and start the Nginx service.

Feel free to modify the playbook to suit your specific needs, such as adjusting package names, package managers, or adding further configuration tasks for Nginx.

In Ansible, you can use the ansible command-line tool to perform various tasks, such as running playbooks, executing ad-hoc commands, gathering facts, and managing inventories. Here are some common ansible command examples:

  1. Ad-hoc Command:

    • Run a simple command on all hosts:

        ansible all -m command -a "ls -l"
      
    • Install a package on all hosts (Debian/Ubuntu):

        ansible all -m apt -a "name=package_name state=present"
      
    • Install a package on all hosts (Red Hat/CentOS):

        ansible all -m yum -a "name=package_name state=present"
      
  2. Running a Playbook:

    • Execute a playbook named playbook.yml on the target_hosts group:

        ansible-playbook -i inventory.ini playbook.yml
      
  3. Gathering Facts:

    • Collect system information from all hosts:

        ansible all -m setup
      
  4. Managing Inventory:

    • Check the connectivity of all hosts defined in the inventory file:

        ansible all -m ping
      
  5. Using Variables:

    • Set variables dynamically using the -e flag:

        ansible-playbook -i inventory.ini playbook.yml -e "variable_name=value"
      
  6. Filtering Hosts:

    • Run a command on hosts that match a specific pattern:

        ansible 'web*' -m command -a "ls -l"
      
  7. Specifying the User:

    • Run a command with a specific user:

        ansible all -m command -a "ls -l" -u username
      
  8. Using Privilege Escalation:

    • Run a command with privilege escalation (sudo):

        ansible all -m command -a "ls -l" -b
      
  9. Checking Playbook Syntax:

    • Validate the syntax of a playbook:

        ansible-playbook playbook.yml --syntax-check
      

These are just a few examples of how you can use the ansible command-line tool. The command syntax and options may vary based on your specific use case, inventory configuration, and requirements. You can find more information and options in the official Ansible documentation.

In a daily use Ansible playbook, you might utilize various modules to perform routine tasks. Below are some common Ansible modules that you may include in your daily playbook:

  1. apt/yum: Used for package management on Debian/Ubuntu and Red Hat-based systems, respectively. You can install, update, and remove packages using these modules.

  2. service: Used to manage services on remote hosts. You can start, stop, restart, and enable/disable services with this module.

  3. file: Used to manage files and directories on remote hosts. You can create, delete, or change file permissions using this module.

  4. copy: Used to copy files from the control node to the remote hosts. This is useful for transferring configuration files or scripts.

  5. template: Similar to the copy module but allows you to use Jinja2 templates to customize files before copying them to the remote hosts.

  6. command/shell: Used to run commands or shell scripts on remote hosts. The command module is preferred for simple tasks, while shell is more suitable for complex shell commands.

  7. cron: Used to manage cron jobs on remote hosts. You can add or remove cron jobs using this module.

  8. user: Used to manage user accounts on remote hosts. You can create, delete, or modify users using this module.

  9. group: Used to manage user groups on remote hosts. You can create, delete, or modify groups using this module.

  10. lineinfile: Used to modify lines in a file on remote hosts. It's helpful for editing configuration files.

  11. get_url: Used to download files from the internet and place them on the remote hosts.

  12. git: Used to manage Git repositories. You can clone, pull, or checkout specific branches with this module.

  13. pip: Used for managing Python packages on remote hosts. You can install, upgrade, or uninstall Python packages with this module.

  14. systemd: Used for managing systemd services on modern Linux distributions. You can enable, disable, start, stop, or restart services.

These are just a few examples of the many modules available in Ansible. Depending on your specific use case and tasks, you might explore other modules as well. Ansible's extensive library of modules enables you to automate various tasks and configurations in your daily DevOps operations.

More from this blog

Shreyash Bhise's blog

55 posts