Ansible format output and html

Viewed 28

This it the query I use to get the output:

- name: extract specific parameters
    set_fact:
      results: "{{ output.json | json_query(query1) | sort(attribute='Name') }}"
    vars:
      query1: "[*].{Name: name, StateA: state_a, StateB: state_b, StateC: state_c}"

Output is as following:

"results": [
    {
        "StateA": "OK",
        "StateB": "Normal",
        "Name": "name1",
        "StateC": "Ok"
    },
    {
        "StateA": "OK",
        "StateB": "Normal",
        "Name": "name2",
        "StateC": "Ok"
    },
    {
        "StateA": "OK",
        "StateB": "Normal",
        "Name": "name3",
        "StateC": "Ok"
    }
]

or in email sent by Ansible email module (with html formatting) :

- {StateA: OK, StateB: Normal, Name: name1, StateC: Ok} - {StateA: OK, StateB: Normal, Name: name2, StateC: Ok} - {StateA: OK, StateB: Normal, Name: name3, StateC: Ok}

I need to:

 1. Add a new line / break after the end of each list. Note - when email is not html subtype, I can get this with adding filter to_yaml - {{ results | to_yaml }}, but when I put it to html it does not work.
 2. Remove {} and -
 3. Be able to do some simple text formatting, for example to bold just the Name, or italic something else
 4. Sort each line so that first parameter is "Name", not "stateA"

Final result sent in email should be like this:

**Name**: name1, StateA: OK, StateB: Normal, *StateC*: Ok 
**Name**: name1, StateA: OK, StateB: Normal, *StateC*: Ok 
**Name**: name1, StateA: OK, StateB: Normal, *StateC*: Ok

The thing that complicates it further is that output can have more then just 3 rows, so this formatting has to be done for each line (list) not matter the number.

Any idea how this can be done?

Thanks!

0 Answers
Related