How can I construct a playbook so an individual play is NOT executed on the machine where Ansible is running from?
For instance, if I have a simple two-play playbook:
- name: Patch and reboot
hosts: all
gather_facts: true
tasks:
- name: Fully patch system
package:
name: "*"
state: latest
become: true
register: patchstatus
- name: Reboot if patched
reboot:
become: true
when: patchstatus is changed
But I don't want the reboot to trigger if it is running on the machine running Ansible.
I know that a better maintained inventory wouldn't have my Ansible system in with the others, but this is a lab and I want a simple playbook I can fire off nightly - I'd rather it not reboot my Ansible playbook (at least not until it's the last one to reboot).
Simply put, I am looking for a pre-defined variable so I can use a simple when: ansible_hostname != ansible_execution_server could be simply used to skip the play. (Assuming ansible_execution_server is the name of the system that ansible-playbook is executing from.)
Then the reboot section could be this:
- name: Reboot if patched
reboot:
become: true
when:
- patchstatus is changed
- ansible_hostname != ansible_execution_server