Ansible - Inventory groups (all | ungrouped | other)

Let's say you have the following servers defined in your default hosts file or your own inventory file.

INI

server1.example.com
server2.example.com
[linux]
server3.example.com
server4.example.com
[windows]
server5.example.com
server6.example.com

 

YAML

all:
  hosts:
    server1.example.com:
    server1.example.com:
  children:
    linux:
      hosts:
        server3.example.com:
        server4.example.com:
    windows:
      hosts:
        server5.example.com:
        server6.example.com:

 

 

By default, Ansible recongizes "all" and "ungrouped" as groups that contain managed nodes (e.g. target systems).

 


all

The "all" group contains every server. For example, the following command will be performed against every managed node.

ansible all -m ping

 

Similarly, if you have a playbook with the following, the play will be performed against every of the managed nodes.

- hosts: all

 


ungrouped

The "ungrouped" group contains every managed node that is not part of a group. In this example, server1.example.com and server2.example.com would be in the ungrouped group. The following command will be performed against the ungrouped nodes.

ansible ungrouped -m ping

 

Similarly, if you have a playbook with the following, the play will be performed against the ungrouped nodes.

- hosts: ungrouped

 


custom groups

You can created your own group. In this example, there are two custom groups, "linux" and "windows". The following command will be performed against the linux nodes.

ansible linux -m ping

 

If you have a playbook with the following, the play will be performed against the windows nodes.

- hosts: windows

 


servers in multiple groups

A managed node can be in more than one group. In this example, server3.example.com is in both the "linux" and "test" group.

INI

server1.example.com
server2.example.com
[linux]
server3.example.com
server4.example.com
[windows]
server5.example.com
server6.example.com
[test]
server3.example.com

 

YAML

all:
  children:
    linux:
      hosts:
        server1.example.com:
        server2.example.com:
        server3.example.com:
    windows:
      hosts:
        server4.example.com:
        server5.example.com:
        server6.example.com:
    test:
      hosts:
        server3.example.com:

 


Group of groups

Let's say you have a group of web servers and database servers and file servers. Sometimes, you may want to run a task against both the web servers and database servers, but not the file servers. In this example, a group called "web" contains the web servers and database servers, but not the file servers.

all:
  children:
    webservers:
      hosts:
        server1.example.com:
        server2.example.com:
    dbservers:
      hosts:
        server3.example.com:
        server4.example.com:
    fileservers:
      hosts:
        server5.example.com:
        server6.example.com:
    web:
      children:
        webservers:
        dbservers:

 

 


Regular Expression / Ranges

To simplify your list of nodes, ranges can be used, like this.

INI

server[1:2].example.com
[linux]
server[3:4].example.com
[windows]
server[5:6].example.com
[test]
server3.example.com

YAML

all:
  hosts:
    children:
      linux:
        hosts:
          server[1:3].example.com:
      windows:
        hosts:
          server[4:6].example.com:
      test:
        hosts:
          server3.example.com:

 



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 824a6 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |