1

I am new to ansible and need understading on below problem statement. Please help me with the code.

Following is the content of json file named, problem.json :-

{
    "items": [
        {
            "type": "node1',
            "id": "11",
            "node": "server1",
            "state": "finished",
            "output": "output1"
        }
    ]

    "items": [
        {
            "type": "node2',
            "id": "22",
            "node": "server2",
            "state": "error",
            "output": "output2"
        }
    ]
}

We need to read the json file and and create a output like below for each respective item (ther are several items in one json" using ansible playbook:-

Check if state is finished then below output:-

[
    {
        "node": "server1",
        "state": "finished",
        "output": "output1"
    }

    {
        "node": "serverx",
        "state": "finished",
        "output": "outputx"
    }

]

If state is error, then display node name with error message.

1
  • Fix the YAML syntax (unbalanced quotation "node1'...) and test it e.g. yamllint.com Commented Dec 3, 2019 at 10:00

1 Answer 1

1

The play

- hosts: localhost
  tasks:
    - include_vars:
        file: problem.json 
        name: test_var
    - debug:
        var: test_var

gives (after having fixed the YAML syntax) only one items, because when reading the file the 2nd items overrides the 1st one

"test_var": {
    "items": [
        {
            "id": "22", 
            "node": "server2", 
            "output": "output2", 
            "state": "error", 
            "type": "node2"
        }
    ]
}

Given valid data. For example

$ cat problem.json

    "items": [
        {
            "type": "node1",
            "id": "11",
            "node": "server1",
            "state": "finished",
            "output": "output1"
        },
        {
            "type": "node2",
            "id": "22",
            "node": "server2",
            "state": "error",
            "output": "output2"
        }
    ]

the play use json_query to process finished and error state.

"If state is error, then display node name with error message."

- hosts: localhost
  vars_files:
    problem.json
  tasks:
    - set_fact:
        finished: "{{ items|
                      json_query('[?state==`finished`].{node: node,
                                                        state: state,
                                                        output: output}')
                      }}"
    - set_fact:
        error: "{{ items|
                   json_query('[?state==`error`].{node: node,
                                                  output: output}')
                   }}"
    - debug:
        msg: "{{ finished + error }}"

gives

"msg": [
    {
        "node": "server1", 
        "output": "output1", 
        "state": "finished"
    }, 
    {
        "node": "server2", 
        "output": "output2"
    }
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.