Ansible Playbooks, Host Patterns, and Ad-hoc commands

Playbook

  • Ansible playbook is written in YAM format.

  • Playbook is also like a file where we write code, it consists of vars, tasks, handlers, and roles.

  • Each playbook contains one or more modules.

  • Playbooks are divided into many sections like

    • the target section ( define host against which playbook task executed)

    • Variable section (define variables)

    • Task section (List of all modules that we need to run in order

---                  # --- represents starting a new playbook
- hosts: group1    # defines the host group that we created in inventory file
  user: ansible      # username - ansible
  become: yes        # playbook will run with sudo privileges
  connection: ssh    # connection detail
  gather_facts: no   # no information collected from hosts
  tasks:
    - name: Install httpd package   # task name
      yum:                          # yum is module name
        name: httpd       # httpd is the package name that we are installing 
        state: latest        # latest will install current version of httpd
    - name: check httpd service status
      service:                      # service is module name
        name: httpd
        state: started            # started will start the httpd service
  • Hosts:- hosts field represents the host or host group against which we want to run the task. The host's field is mandatory. When we provide a host group, Ansible will take the host group from the playbook and search in an inventory file. If there is no match, Ansible will skip all the tasks for that host group

  • Tasks:- list of actions

Host Patterns

Suppose we have 8 nodes and among these many nodes we only want to update software in 2 hosts, then to access these 2 hosts we have to use host patterns.

  • "all" pattern refers to all the machines in an inventory.

  • ansible all --list-hosts  - Give detail of all nodes

  • ansible demo --list-hosts  - 'demo' is the group name in the inventory file and this command return all node in the demo group

  • ansible demo[0]  - Give detail of the first node in the demo group, 0 represent 1st node

  • <group name>[0]  - Picks the first node of the group, in place of the group we can give any group name from our inventory file

  • <group name>[1]  - Picks the second node of the group

  • <group name>[-1] - Picks the last node of the group

  • <group name>[1:4] - Range of nodes

  • demo[1:5]:group1[2:10]  - For fetching detail from multiple group

Ad-hoc Commands

Ad-hoc commands are commands which can be run individually to perform quick functions. These ad-hoc commands are not used for configuration management and deployment because these commands are of one time.

The ansible ad-hoc commands do not have idempotency ( idempotency means if we create a file in a node using the "touch" command then again if we try to create the same file using the "touch" command again it will create the file and replace the old file )

Ansible Ad-hoc command examples

  • [ansible@ip] $ ansible demo -a "ls"   

    • demo is the group name and -a represents argument and in between double quotes " " we write Linux commands
  • [ansible@ip] $ ansible demo[0] -a "touch file1"   

    • creates an empty file "file1" in the first node of the demo group
  • [ansible@ip] $ ansible all -a "touch file2" 

    • creates an empty file "file2" in all nodes of the demo group
  • [ansible@ip] $ ansible all -a "ls -al" 

    • Gives the list of all files including hidden files
  • [ansible@ip] $ ansible demo -a "sudo yum install httpd -y" 

    • Installs httpd package in all nodes of the demo group
  • [ansible@ip] $ ansible demo -ba "yum install httpd -y"

    • If we do not want to mention sudo in the command then we can use 'b' ( 'b' means become)

I hope you enjoyed this article, if you have any questions, comments, or feedback or if I made a mistake feel free to comment down below.


Did you find this article valuable?

Support DevOps With CJ by becoming a sponsor. Any amount is appreciated!