Change exit code of ansible-playbook if there are any changes

215 views
Skip to first unread message

Łukasz Górski

unread,
Jul 28, 2016, 2:32:43 PM7/28/16
to ansible...@googlegroups.com
Hi,

is there any sensible option to make ansible-playbook exit with non-0 exit code if there are any changes to be made to target hosts?

The rationale for this is: we want to start using Jenkins to test if our production infrastructure has not diverged from the state described in Ansible roles and playbooks. It seems to be a good idea to run the playbooks in check mode and report any changes, but apart from parsing the playbook run output with shell tools, we haven't found (yet) any reasonable solution to only detect (successful) changes and not failures.

It's quite likely the approach is not really the best and there is some simple and elegant option to achieve the same result (e.g. some Jenkins plugin). We'll be happy with anything that works; thank you in advance for any ideas and comments :)

--
Łukasz Górski
Administrator Systemu Linux
e-mail: l.go...@trojmiasto.pl

Tel. 058 321 95 65 Kom.
Moje miasto to Trójmiasto!

Trojmiasto.pl Sp. z o.o. ul. Wały Piastowskie 1, 80-855 Gdańsk
tel: 58 321-95-00 fax: 58 321-95-13 e-mail: in...@trojmiasto.pl
NIP 583-30-12-490, REGON 220563678
Sąd Rejonowy Gdańsk-Północ w Gdańsku, VII Wydział Gospodarczy Krajowego Rejestru Sądowego, KRS 0000300002, Kapitał zakładowy 2 150 000 PLN
fbTrojmiasto.pl na Facebooku. Dołącz do nas!

Ryan Groten

unread,
Jul 28, 2016, 5:29:50 PM7/28/16
to Ansible Project
How about detecting the changed task(s) from within the playbook itself and create another task that calls a Jenkins API (assuming that's possible)?
Eg:
- name: Check for divergence
  command
: <check_for_divergence.sh>
 
register: is_diverged

- name: Call Jenkins to un-diverge
  command
: <cool Jenkins command or API call>
 
when: is_diverged | changed

Łukasz Górski

unread,
Jul 31, 2016, 2:15:15 PM7/31/16
to ansible...@googlegroups.com
It works ok for single tasks, but not for a 'site' type playbook run (i.e. hundreds of tasks), unless you sprinkle your playbooks and roles all over with such handlers. I suppose I could venture into writing custom callback plugins and use one to notify some API.

I found a simpler solution though - there's a Log Parser plugin for Jenkins. It uses regexps for parsing the console output and it's quite easy to spot changed tasks (say, /^changed: \[/) and set the build status to unstable.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/87f583f9-178c-462b-9698-2d72a350889c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages