Reputation: 341
I need some help on an ansible playbook. Using this sample playbook, I would like to modify this playbook in such a way that if the hostname of webservers1 equals "123.456.000", do not bother running the remaining parts of the playbook.
- name: test play 1
hosts: webservers1
serial: 2
gather_facts: False
tasks:
- name: first task
command: hostname
- name: test play 2
hosts: webservers2
serial: 2
gather_facts: False
tasks:
- name: first task
command: hostname
- name: second task
command: hostname
- name: test play 3
hosts: webservers3
serial: 2
gather_facts: False
tasks:
- name: first task
command: hostname
- name: second task
command: hostname
- name: test play 4
hosts: webservers4
serial: 2
gather_facts: False
tasks:
- name: first task
command: hostname
- name: second task
command: hostname
Upvotes: 0
Views: 41
Reputation: 68004
Q: "If the hostname of webservers1 equals "123.456.000", do not bother running the remaining parts of the playbook."
A: It's not possible to break a playbook from a play, e.g.
- hosts: localhost
tasks:
- meta: end_play
- hosts: localhost
tasks:
- debug:
msg: Start play 2
the playbook proceeds to the second play
PLAY [localhost] **************************************************************
PLAY [localhost] **************************************************************
TASK [debug] ******************************************************************
ok: [localhost] =>
msg: Start play 2
There is still an option to test a variable at the beginning of each play. For example, given the inventory
shell> cat hosts
[webservers1]
srv1 ansible_host=123.456.000
srv2 ansible_host=123.456.001
[webservers2]
srv3 ansible_host=123.456.002
srv4 ansible_host=123.456.003
The playbook below tests the condition in the first play and sets the variable. The next plays test this variable, e.g.
- hosts: all
tasks:
- set_fact:
_break: "{{ '123.456.000' in groups.webservers1|
map('extract', hostvars, 'ansible_host')|
list }}"
run_once: true
- hosts: webservers1
tasks:
- meta: end_play
when: _break|bool
- debug:
msg: Start webservers1
- hosts: webservers2
tasks:
- meta: end_play
when: _break|bool
- debug:
msg: Start webservers2
should break the next two plays
PLAY [all] ********************************************************************
TASK [set_fact] ***************************************************************
ok: [srv1]
PLAY [webservers1] ************************************************************
PLAY [webservers2] ************************************************************
PLAY RECAP ********************************************************************
srv1: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Upvotes: 1