This content has moved to pzel.name

How to grab all hosts but the first, in Ansible

Today I learned (2019-03-20)
Tagged: ansible jinja2

Today I was trying to figure out how to run a particular ansible play on one host out of a group, and another play on all the other hosts.

The answer was found in a mailing group posting from 2014, but in case that service goes down, here's my note-to-self on how to do it.

Let's say you have a group of hosts called stateful_cluster_hosts in your inventory. You'd like to upload files/leader_script.sh.j2 to the first host, and files/follower_script.sh.j2 to all the others.

The play for the leader host would look like this:

- hosts: stateful_cluster_hosts[0]
  tasks:
  - name: "Upload leader start script"
    template:
      src: files/leader_script.sh.j2
      dest: start.sh
      mode: "u=rwx,g=rx,o=rx"

The play for the follower hosts would look like this:

- hosts:  stateful_cluster_hosts:!stateful_cluster_hosts[0]
  tasks:
  - name: "Upload follower start script"
    template:
      src: files/follower_script.sh.j2
      dest: start.sh
      mode: "u=rwx,g=rx,o=rx"

Where the sytax list:!list[idx] means take list, but filter out list[idx].