Skip to content

Instantly share code, notes, and snippets.

@dmsimard
Last active May 2, 2017 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmsimard/cc39f4f5283ae31ce9e2ab0cb0b73fc0 to your computer and use it in GitHub Desktop.
Save dmsimard/cc39f4f5283ae31ce9e2ab0cb0b73fc0 to your computer and use it in GitHub Desktop.
callback race condition
from __future__ import (absolute_import, division, print_function)
from ansible.plugins.callback import CallbackBase
class CallbackModule(CallbackBase):
CALLBACK_VERSION = 2.0
CALLBACK_TYPE = 'notification'
CALLBACK_NAME = 'callback_race'
def __init__(self):
super(CallbackModule, self).__init__()
self.loop_items = []
def v2_playbook_on_task_start(self, task, is_conditional, is_handler=False):
print("playbook_on_task_start")
def v2_runner_item_on_ok(self, result):
print("runner_item_on_ok")
self.loop_items.append(result)
def v2_runner_on_ok(self, result, **kwargs):
print("runner_on_ok")
print(self.loop_items)
$ ansible-playbook playbook.yml
[WARNING]: Host file not found: /etc/ansible/hosts
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [Create fake hosts] *****************************************************************************************************************************************************************************************************************************************************************************************************
TASK [Add fake hosts in inventory] *******************************************************************************************************************************************************************************************************************************************************************************************
playbook_on_task_start
changed: [localhost] => (item=host1)
runner_item_on_ok
changed: [localhost] => (item=host2)
runner_item_on_ok
changed: [localhost] => (item=host3)
runner_item_on_ok
runner_on_ok
[<ansible.executor.task_result.TaskResult object at 0x7f7d639b2650>, <ansible.executor.task_result.TaskResult object at 0x7f7d63a8c1d0>, <ansible.executor.task_result.TaskResult object at 0x7f7d639d6ad0>]
PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0
$ ansible-playbook playbook.yml
[WARNING]: Host file not found: /etc/ansible/hosts
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [Create fake hosts] *****************************************************************************************************************************************************************************************************************************************************************************************************
TASK [Add fake hosts in inventory] *******************************************************************************************************************************************************************************************************************************************************************************************
playbook_on_task_start
changed: [localhost] => (item=host1)
runner_item_on_ok
changed: [localhost] => (item=host2)
runner_item_on_ok
runner_on_ok
[<ansible.executor.task_result.TaskResult object at 0x7febe5456650>, <ansible.executor.task_result.TaskResult object at 0x7febe55301d0>]
changed: [localhost] => (item=host3)
runner_item_on_ok
PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0
- name: Create fake hosts
hosts: localhost
gather_facts: no
tasks:
- name: Add fake hosts in inventory
add_host:
name: "{{ item }}"
ansible_host: "127.0.0.1"
ansible_connection: "local"
with_items:
- host1
- host2
- host3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment