04.04.2020 Views

Mastering Ansible

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 1

As we can see, both hosts backend.example.name and mastery.example.name

were operated on. Let's see what happens if we supply a limit, specifically to limit

our run to only frontend systems:

We can see that only mastery.example.name was operated on this time. While there

are no visual clues that the entire inventory was parsed, if we dive into the Ansible

code and examine the inventory object, we will indeed find all the hosts within, and

see how the limit is applied every time the object is queried for items.

It is important to remember that regardless of the host's pattern used in a play, or the

limit supplied at runtime, Ansible will still parse the entire inventory set during each

run. In fact, we can prove this by attempting to access host variable data for a system

that would otherwise be masked by our limit. Let's expand our playbook slightly and

attempt to access the ansible_ssh_port variable from backend.example.name:

---

- name: limit example play

hosts: all

gather_facts: false

tasks:

- name: tell us which host we are on

debug:

var: inventory_hostname

- name: grab variable data from backend

debug:

var: hostvars['backend.example.name']['ansible_ssh_port']

[ 11 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!