How to Use the Ansible Playbook hosts Option + Examples

Ansible Playbook hosts Option with Examples

Ever wondered how Ansible knows where to run your tasks? That’s the job of the hosts option in your playbooks. Whether you’re deploying updates to your web servers or gathering system information from all your nodes, targeting the right machines is critical.

In this guide, we’ll break down how the hosts option works, explain each use case with real-world examples, and help you avoid common pitfalls.

What Is the hosts Option in Ansible?

The hosts option in an Ansible playbook defines which servers (or groups of servers) the play should target. These hosts are pulled from your inventory file. You can define a single host, a group of hosts, all hosts, or use patterns to include/exclude specific ones.

Sample Inventory File

Here’s an example inventory.ini to support our playbook examples:

[webservers]
webserver1 ansible_host=192.168.1.10 web_package=nginx
webserver2 ansible_host=192.168.1.11 web_package=httpd

[dbservers]
db1 ansible_host=192.168.1.20

How to Define Hosts in a Playbook

The hosts option can take different values:

  • A single host
  • A group of hosts
  • Multiple hosts
  • all (to target all hosts in the inventory)
  • Patterns (to include/exclude specific hosts)

Example 1: Run Playbook on a Single Host

In this example, the playbook will run tasks only on webserver1.

---
- name: Install Nginx on a single host
  hosts: webserver1
  become: true
  tasks:
    - name: Install Nginx
      ansible.builtin.yum:
        name: nginx
        state: present

Explanation:

  • hosts: webserver1 targets a single host named webserver1 from the inventory.
  • The task installs Nginx using the yum module.

Example 2: Run Playbook on a Group of Hosts

You can define groups of hosts in your inventory file (e.g., webservers) and run the playbook on all hosts in that group.

---
- name: Update packages on all web servers
  hosts: webservers
  become: true
  tasks:
    - name: Update all packages
      ansible.builtin.yum:
        name: "*"
        state: latest

In this example, the play targets all hosts in the webservers group, as defined in the inventory.

Explanation:

  • hosts: webservers targets all hosts in the webservers group.
  • The task updates all packages to their latest versions.

Example 3: Run Playbook on All Hosts

If you want to run the playbook on all hosts listed in your inventory, use hosts: all.

---
- name: Gather system information from all hosts
  hosts: all
  tasks:
    - name: Display hostname
      ansible.builtin.command:
        cmd: hostname
      register: result

    - name: Print hostname
      ansible.builtin.debug:
        msg: "The hostname is {{ result.stdout }}"

Explanation:

  • hosts: all targets every host in the inventory.
  • The playbook gathers and displays the hostname of each host.

Example 4: Exclude Specific Hosts Using Patterns

Ansible allows for complex host selection using patterns. For example, to target all hosts except those in the dbservers group:

---
- name: Install Apache on all servers except dbservers
  hosts: all:!dbservers
  become: true
  tasks:
    - name: Install Apache
      ansible.builtin.yum:
        name: httpd
        state: present

This pattern selects all hosts in the inventory except those belonging to the dbservers group.

Explanation:

  • hosts: all:!dbservers targets all hosts except those in the dbservers group.
  • The playbook installs Apache on all non-database servers.

Example 5: Specify Multiple Hosts

You can also specify multiple hosts separated by a colon.

---
- name: Restart services on multiple hosts
  hosts: webserver1:webserver2
  become: true
  tasks:
    - name: Restart Nginx
      ansible.builtin.systemd:
        name: nginx
        state: restarted

Explanation:

  • hosts: webserver1:webserver2 targets both webserver1 and webserver2.
  • The playbook restarts the Nginx service on these two hosts using the systemd module.

Example 6: Using Host Variables

You can use host variables in your playbook to customize tasks based on the target host.

---
- name: Install software based on host variables
  hosts: webservers
  become: true
  tasks:
    - name: Install web server package
      ansible.builtin.yum:
        name: "{{ web_package }}"
        state: present

Explanation:

The web_package variable can be defined in the inventory file for each host.

For example:

[webservers]
webserver1 web_package=nginx
webserver2 web_package=httpd

This allows the playbook to install different packages on different hosts.

Example 7: Using localhost for Local Execution

You can use localhost to run tasks on the Ansible control node itself.

---
- name: Run tasks on localhost
  hosts: localhost
  tasks:
    - name: Display disk usage
      ansible.builtin.command:
        cmd: df -h
      register: disk_usage

    - name: Print disk usage
      ansible.builtin.debug:
        var: disk_usage.stdout

Explanation:

  • hosts: localhost targets the local machine where the playbook is being executed.
  • The playbook displays the disk usage of the local machine.

Conclusion

The hosts option is a powerful way to define which machines your Ansible playbooks will target. It allows for flexible and dynamic automation across different environments.

For more in-depth guides and practical examples on Ansible, visit Linuxbuz.

FAQs

1. What does the hosts option in Ansible playbooks do?

The hosts option specifies which target systems the playbook tasks will run on.

2. Can I use multiple groups in the hosts option?

Yes, you can specify multiple groups or hosts using a comma-separated list, like webservers,dbservers.

3. How do I limit playbook execution to a specific host?

Use the --limit option, e.g., ansible-playbook playbook.yml --limit web1.

4. Can I use an IP address directly in the hosts option?

Yes, you can specify an IP address directly, like hosts: 192.168.1.10, without needing an inventory file.

5. What is the purpose of ansible_host in the inventory file?

The ansible_host variable defines the actual IP address or hostname of a target system if it differs from the inventory name.

About Hitesh Jethva

Experienced Technical writer, DevOps professional with a demonstrated history of working in the information technology and services industry. Skilled in Game server hosting, AWS, Jenkins, Ansible, Docker, Kubernetes, Web server, Security, Proxy, Iptables, Linux System Administration, Domain Name System (DNS), and Technical Writing.

View all posts by Hitesh Jethva