Exit playbook and return an error

30 views
Skip to first unread message

Colm Mcnabola

unread,
Oct 10, 2017, 9:35:24 AM10/10/17
to Ansible Project
Hi,

I want to insert an failure on my playbook where if the hostgroup isnt in the inventory, it fails. 

Inventory path is 
inventory/"environment_name"/inventory

and looks similiar to this 
[NodeGroup1]
node1@node

[NodeGroup2]
node2@node.
node3@node.


When running a playbook using --extra vars and declaring the name of the host group like so 
ansible-playbook deploy_rpm.yml -i environment/testEnv --extra-vars='{"hostgroup": "NodeGroup1", "var1": "test-rpm", "version": "0.1"}'
It passes as NodeGroup1 is in the inventory

When the hostgroup is named wrong, 'NodeGroup999' for example.
ansible-playbook deploy_rpm.yml -i environment/testEnv --extra-vars='{"hostgroup": "NodeGroup999", "var1": "test-rpm", "version": "0.1"}'

It just silently fails but doesnt return an error message or stop. 
I have added this in as a task but it doesnt seem to like it. 

  - name: "Grep inventory to check hostgroup exists."
      fail: msg:="Hostgroup '{{ hostgroup }}' is not defined"
      when:
        - inventory_hostname in groups['hostgroup']|default([]) 


How can i improve this task to stop the playbook and return a failure? Is it best to put the path of the inventory files inside the 'when' part of the task? 

Kai Stian Olstad

unread,
Oct 10, 2017, 12:34:43 PM10/10/17
to ansible...@googlegroups.com
I think you only option is to add the following play on top of your
playbook.

- hosts: localhost
gather_facts: no
become: no
tasks:
- fail:
msg: Hostgroup '{{ hostgroup }}' is not defined
when: hostgroup not in groups


--
Kai Stian Olstad

Colm Mcnabola

unread,
Oct 11, 2017, 5:16:29 AM10/11/17
to ansible...@googlegroups.com
Is this possible to do the script locally and then still be able to connect to hosts in a later stage of the same playbook?

for example 

task: 
      - name: local run of script against inventory

 



--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/aq2yRAaM_VE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/642d3c37-74a9-a068-7d3a-0e89a96c2f6b%40olstad.com.

For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Oct 11, 2017, 5:35:15 AM10/11/17
to ansible...@googlegroups.com
On 11.10.2017 11:16, 'Colm Mcnabola' via Ansible Project wrote:
> Is this possible to do the script locally and then still be able to
> connect
> to hosts in a later stage of the same playbook?
>
> for example
>
> task:
> - name: local run of script against inventory

Yes, the play I posted has "- hosts: localhost", meaning it will run on
the Ansible controller.
If this play fails, it's will not run subsequent plays in the playbook.

The subsequent play can therefore have - hosts: "{{ hostgroup }}"


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages