Help - generating a table of task results in ansible

26 views
Skip to first unread message

piya

unread,
Sep 3, 2020, 7:48:09 PM9/3/20
to Ansible Project
Hello devs,

First time posting here, apologies if not the done thing or the wrong place to post.

I'm not looking for a fix to an issue I've run into, rather I'm interested to know whether a strategy I'm thinking about sounds feasible or not, and whether there's any recommendations on better approaches.. let's say I'm on a fact finding mission.

Current state
I run some drift detection/correction & smoke tests from a scheduled ansible play, this play might include up to 20 tasks which get invoked by a main.yml. The schedule is run every hour against a few k8s clusters/endpoints/nodes by awx which posts a message via a slack webhook to tell me if the job ran or failed and posts some output too. The trouble is, I often have to look in awx or read some output, to tell me what happened when something went wrong. 

That can take the form of stdout_lines, ansible output etc but always results in me having to look at some output.

Project Layout
.
├── CHANGELOG.md
├── README.md
├── ansible.cfg
├── inventory
│   ├── dev.all.aws_ec2.yaml
│   ├── staging.all.aws_ec2.yaml
│   └── prod.all.aws_ec2.yaml
├── playbooks
|   ├── drift.yaml
│   └── smoke_tests.yaml
└── tasks
    ├── main.yaml
    ├── check_cluster_health_api.yaml
    ├── check_active_passive_cluster.yaml
    ├── check_dns.yaml
    ├── check_http_statuses.yaml
    ├── check_internet_connectivity.yaml
    ├── check_tcp_ports.yaml
    ├── check_kube_system_pods.yaml
    ├── check_etcd_cluster_health.yaml
...

Desired state
These tests already assert success or failure and if the job fails, I know some task failed. I'd like to capture the result of each task (say, 0 for success, 1 for fail), template something like an html table or ASCII table and then send that to slack instead.

What main looks like

---

- name: Cluster Health API
import_tasks: check_cluster_health_api.yaml
tags: oc_health

- name: Check A/P cluster
import_tasks: check_active_passive_cluster.yaml
tags: ap_cluster

- name: Check DNS
import_tasks: check_dns.yaml
tags: dnsmasq

etc...

Some ideas
  • Set a fact to an empty dict in main, run each task from main, insert some data into dict. Template it at the end, send to slack. Bit of code smell, lots of dupe statements. Not sure whether this is possible, to have a global var, not defined in  each task individually.
  • Use a handler? Not totally sure how this would work, but the thinking is to register some facts per task and have a subsequent task notify the handler to do something (append to dict for e.g.). Template at the end. As above not sure how var precedence would behave here, but I like the idea of treating a handler like a "module" and passing it some input, getting some output.
  • Move all of my tests into a single task, or play as it'll be trivial to create and append to an obj in a single task. Don't want to do this really unless absolutely necessary.

Have looked through some other posts in the google group, the most relevant one I found was this. This it's a pretty simple example.

Interested to know if people with more experience have seen a requirement like this, if any of the above thoughts seem viable or even if people think this is a bad idea.

Anyway - all suggestions welcome!

Have a good one wherever you are.

Vladimir Botka

unread,
Sep 4, 2020, 12:52:34 AM9/4/20
to piya, ansible...@googlegroups.com
On Thu, 3 Sep 2020 16:48:09 -0700 (PDT)
piya <p.att...@gmail.com> wrote:

> ... I'd like to capture the result of each task (say, 0 for
> success, 1 for fail), template something like an html table or ASCII table
> and then send that to slack instead.

Don't make the code more complex. Use ansible-runner to run the
projects and analyse the potential problems from outside
https://ansible-runner.readthedocs.io/en/latest/

ansible-runner artifacts will provide you with all the details
https://ansible-runner.readthedocs.io/en/latest/intro.html#runner-artifacts-directory-hierarchy
See for example
https://gist.github.com/vbotka/31ccd5380a2b230be11b1df78cc402a1

FWIW. See the example of a script how to run projects via
ansible-runner and create simple log of what succeeded and failed
https://github.com/vbotka/ansible-runner/blob/master/contrib/ansible-cron-audit.bash
https://github.com/vbotka/ansible-runner/blob/master/contrib/arwrapper.bash

The idea is to run the script from cron and receive an email on
failures only. The log can be scanned manually or sent to an external
logging. If a playbook fails go to the ansible-runner artifacts and
find the details. Next step might be the automation of the artifacts'
analysis.

--
Vladimir Botka

Vladimir Botka

unread,
Sep 4, 2020, 12:59:13 AM9/4/20
to piya, ansible...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages