FreeKB - Ansible command - target hosts (inventory.yml)
Ansible command - target hosts (inventory.yml)

Home > Search


This assumes you have installed Ansible Tower and that you are familiar with the ansible command

Ansible commands are issued against hosts (eg. target servers). You will define the target servers in a file, such as inventory.yml. In this example, inventory.yml contains four target servers, two on Windows and two on Linux.

---
all:
  children:
    windows:
      hosts:
        server1.example.com:
        server2.example.com:
    linux:
      hosts:
        server3.example.com:
        server4.example.com:


Then, in a playbook.yml, you can perform tasks against every server in inventory.yml, like this.

- hosts: all

 

Or against only the Windows servers, like this.

- hosts: windows

 

Or against only the Linux servers, like this.

- hosts: linux

 

Or against both Windows and Linux servers, like this.

- hosts: windows:linux

 

A single playbook can run tasks/plays against different hosts, like this.

- hosts: windows
  tasks:
    - name: Windows task

- hosts: linux
  tasks:
    - name: Linux task

 

Or you can create a variable that contains the servers. In this example, the linux_servers variable will contain the Linux servers in inventory.yml.

- hosts: windows:linux
  - set_fact: linux_servers="{{groups['linux']}}"

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 5ac59 in the box below so that we can be sure you are a human.




Comments