DEV Community

Harsh Mishra
Harsh Mishra

Posted on

Ansible Patterns for Ad-Hoc Commands and Playbooks: Complete Guide for Beginners

Mastering Ansible Patterns: Targeting Hosts and Groups

Ansible patterns are highly flexible, enabling you to execute commands or run playbooks against a precise subset of your infrastructure. Whether you're dealing with a single host, a group of hosts, or a more complex combination of groups with exclusions or intersections, understanding patterns is crucial for optimizing your workflow.

What Are Ansible Patterns?

Ansible patterns are used to specify which hosts or groups you want to target during the execution of your commands or playbooks. Patterns allow you to:

  • Target a single host
  • Target multiple hosts
  • Target groups of hosts
  • Exclude hosts or groups
  • Perform regular expression matching
  • Combine patterns for more granular control

Let's dive into the details and see how these patterns work in both ad-hoc commands and playbooks.

Common Ansible Patterns

Ansible patterns can be simple or highly specific, depending on your needs. Here's a breakdown of the most common patterns you can use:

Description Pattern(s) Targets
All hosts all (or *) All hosts in your inventory
One host host1 A single host named host1
Multiple hosts host1:host2 (or host1,host2) Multiple hosts host1 and host2
One group webservers All hosts in the webservers group
Multiple groups webservers:dbservers All hosts in webservers and dbservers
Excluding groups webservers:!atlanta All hosts in webservers, except those in atlanta
Intersection of groups webservers:&staging Hosts in both webservers and staging

Pattern Examples in Ad-Hoc Commands

To use these patterns with Ansible ad-hoc commands, you can apply them directly using the ansible command. Here's how to use some of the common patterns:

  1. Target All Hosts

    • Command:
     ansible all -m ping
    
  2. Target One Host

    • Command:
     ansible host1 -m ping
    
  3. Target Multiple Hosts

    • Command:
     ansible host1:host2 -m ping
    
  4. Target One Group

    • Command:
     ansible webservers -m ping
    
  5. Target Multiple Groups

    • Command:
     ansible webservers:dbservers -m ping
    
  6. Exclude Hosts from a Group

    • Command:
     ansible webservers:!atlanta -m ping
    
  7. Intersection of Groups

    • Command:
     ansible webservers:&staging -m ping
    
  8. Combine Multiple Patterns (Complex Example)

    • Command:
     ansible webservers:dbservers:&staging:!phoenix -m ping
    

This command targets hosts in both webservers and dbservers groups, ensures they are also in the staging group, and excludes any host in the phoenix group.

Pattern Examples in Playbooks

You can use the same patterns in your playbooks as well. The syntax for targeting hosts or groups in playbooks is very similar to the ad-hoc command format.

Basic Playbook Example

---
- name: Ping all hosts
  hosts: all
  tasks:
    - name: Ping each host
      ping:
Enter fullscreen mode Exit fullscreen mode

Playbook Targeting One Host

---
- name: Configure host1
  hosts: host1
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present
Enter fullscreen mode Exit fullscreen mode

Playbook Targeting Multiple Hosts

---
- name: Configure multiple hosts
  hosts: host1:host2
  tasks:
    - name: Install Apache
      apt:
        name: apache2
        state: present
Enter fullscreen mode Exit fullscreen mode

Playbook Targeting a Group

---
- name: Configure Web Servers
  hosts: webservers
  tasks:
    - name: Install PHP
      apt:
        name: php
        state: present
Enter fullscreen mode Exit fullscreen mode

Excluding a Group in a Playbook

---
- name: Configure Web Servers excluding Atlanta
  hosts: webservers:!atlanta
  tasks:
    - name: Install MySQL
      apt:
        name: mysql-server
        state: present
Enter fullscreen mode Exit fullscreen mode

Combining Patterns in a Playbook

---
- name: Configure servers
  hosts: webservers:dbservers:&staging:!phoenix
  tasks:
    - name: Install Redis
      apt:
        name: redis-server
        state: present
Enter fullscreen mode Exit fullscreen mode

This playbook installs Redis on all hosts in webservers and dbservers that are also part of the staging group, while excluding any host in the phoenix group.

Advanced Pattern Options

Using Variables in Patterns

Ansible allows the use of variables to dynamically target groups. For example, you can pass variables into your playbook using the -e argument:

ansible-playbook -e "excluded=atlanta required=staging" my_playbook.yml
Enter fullscreen mode Exit fullscreen mode

In your playbook, you can reference these variables in the hosts section:

---
- name: Configure servers
  hosts: webservers:!{{ excluded }}:&{{ required }}
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present
Enter fullscreen mode Exit fullscreen mode

Using Group Position in Patterns

Ansible also supports indexing into groups to target specific hosts based on their position in the group.

Example Group

[webservers]
cobweb
webbing
weber
Enter fullscreen mode Exit fullscreen mode

You can then target hosts by their index:

  1. First Host:
   hosts: webservers[0]
Enter fullscreen mode Exit fullscreen mode
  1. Last Host:
   hosts: webservers[-1]
Enter fullscreen mode Exit fullscreen mode
  1. Range of Hosts:
   hosts: webservers[0:2]
Enter fullscreen mode Exit fullscreen mode

Using Regular Expressions in Patterns

Ansible also supports regular expressions to match hosts. You can use the ~ symbol to indicate a regular expression pattern:

hosts: ~web.*\.example\.com
Enter fullscreen mode Exit fullscreen mode

This pattern will match any host whose name begins with web and ends with .example.com.

Conclusion

Ansible patterns provide a powerful way to target specific hosts, groups, or combinations of both within your inventory. Whether you're running ad-hoc commands or creating complex playbooks, patterns offer the flexibility to manage infrastructure efficiently. By mastering these patterns, you can streamline your automation processes, saving time and effort in your day-to-day tasks.

Top comments (0)