Ansible Playbook hosts Option with Examples

Ansible Playbook hosts Option with Examples

In Ansible, the hosts option in a playbook specifies the target hosts or groups of hosts on which tasks will be executed. The hosts directive is defined at the play level and tells Ansible which machines to run the playbook against.

This article explores the hosts option in the Ansible playbook, its importance, and practical examples for using it effectively in Ansible automation.

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 package manager.

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

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

You can exclude specific hosts using the ! pattern.

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

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.

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 Code2DevOps.

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

I am Hitesh Jethva, Founder and Author at Code2DevOps.com. With over 15 years of experience in DevOps and open source technologies, I am passionate about empowering teams through automation, continuous integration, and scalable solutions.

View all posts by Hitesh Jethva