Add error handling for ansible_playbook handler

This commit is contained in:
Dmitry Shulyak 2015-07-02 09:17:09 +03:00
parent f7c7d1ad95
commit b6cddda994
5 changed files with 25 additions and 8 deletions

View File

@ -1,4 +1,9 @@
- hosts: '*' - hosts: localhost
sudo: yes sudo: yes
vars:
var1: 'playbook'
roles: roles:
- { role: "test_role" } - { role: "test_role" }
tasks:
- debug: msg="VAR1 value is {{var1}}"
- fail: msg='just test failure'

View File

@ -4,7 +4,7 @@ version: 0.0.1
input: input:
var1: var1:
type: str! type: str!
value: some_value value: meta
uuid: uuid:
type: str! type: str!
value: 'aa1das1231' value: 'aa1das1231'

View File

@ -1,4 +1,6 @@
- hosts: '*' - hosts: '*'
sudo: yes sudo: yes
vars:
default1: playbook
tasks: tasks:
- debug: "my message" - debug: msg="my message {{default1}}"

View File

@ -11,3 +11,6 @@ input:
ssh_key: ssh_key:
type: str! type: str!
value: value:
default1:
type: str!
value: meta

View File

@ -8,6 +8,7 @@ from ansible import callbacks
import ansible.constants as C import ansible.constants as C
from solar.core.handlers import base from solar.core.handlers import base
from solar import errors
class AnsiblePlaybook(base.BaseHandler): class AnsiblePlaybook(base.BaseHandler):
@ -24,16 +25,16 @@ class AnsiblePlaybook(base.BaseHandler):
remote_user = variables.get('ssh_user') or C.DEFAULT_REMOTE_USER remote_user = variables.get('ssh_user') or C.DEFAULT_REMOTE_USER
private_key_file = variables.get('ssh_key') or C.DEFAULT_PRIVATE_KEY_FILE private_key_file = variables.get('ssh_key') or C.DEFAULT_PRIVATE_KEY_FILE
if variables.get('ip'): if variables.get('ip'):
host_list = [variables['ip']] host = variables['ip']
transport = C.DEFAULT_TRANSPORT transport = C.DEFAULT_TRANSPORT
else: else:
host_list = ['localhost'] host = 'localhost'
transport = 'local' transport = 'local'
play = PlayBook( play = PlayBook(
playbook=action_file, playbook=action_file,
remote_user=remote_user, remote_user=remote_user,
host_list = host_list, host_list = [host],
private_key_file=private_key_file, private_key_file=private_key_file,
extra_vars=variables, extra_vars=variables,
callbacks=playbook_cb, callbacks=playbook_cb,
@ -41,4 +42,10 @@ class AnsiblePlaybook(base.BaseHandler):
stats=stats, stats=stats,
transport=transport) transport=transport)
return play.run() play.run()
summary = stats.summarize(host)
if summary.get('unreachable') or summary.get('failures'):
raise errors.SolarError(
'Ansible playbook %s failed with next summary %s',
action_file, summary)