Good evening.

There was such a pickling question, have not yet met an adequate solution.

How to calculate the elapsed time to lose a playbook for each host is required to compile a reporting message in Telegrams.

While this is such a stub, it does not always show the correct time if the playbook is made in parallel for several hosts:

pl-signoff.yml

#!/usr/bin/ansible-playbook - hosts: _signoff gather_facts: false vars: - playbook_name: "pl-signoff" - build_version: 1.41 pre_tasks: - local_action: shell date register: pl_begin become: false roles: - role-1 - role-2 # etc - inspector 

roles / inspector / tasks / main.yml

 - name: "last probe of facts" setup: - local_action: shell date register: pl_end become: false - name: "shout it loud to signoff_test" telegram: msg_format: plain token: 'my_tocken' chat_id: my_id msg: | BEGIN: "{{ pl_begin }}" PL: "{{ playbook_name }}" MSG: task complete for the site "{{ site-id }}" END: "{{ pl_end }}" 

1 answer 1

Everything turned out to be much simpler, just ran preliminary tasks on a local machine and collected facts about time:

 - hosts: _signoff gather_facts: false pre_tasks: - name: "setup localhost" local_action: setup gather_subset=min become: false - name: "set meta facts" set_fact: pl_name: "pl-signoff" pl_version: 1.41 pl_build: "{{ ansible_date_time.epoch }}" pl_begin: "{{ ansible_date_time.iso8601_micro }}" - debug: msg: - "{{ pl_name }}" - "{{ pl_version }}" - "{{ pl_build }}" - "{{ pl_begin }}" 

We get a very accurate start time of the playbook for each host:

 Tuesday 02 January 2018 23:53:04 +0000 (0:00:00.053) 0:00:00.773 ******* ok: [TEST-SiteUbuntu-16-04] => { "msg": [ "pl-signoff", 1.41, "1514937183", "2018-01-02T23:53:03.820574Z" ] } ok: [TEST-SiteUbuntu-12-04] => { "msg": [ "pl-signoff", 1.41, "1514937183", "2018-01-02T23:53:03.817155Z" ] } 

Links