Need help with ansible playbook

Hi All,

I need help with writing an ansible playbook, where it loops over a few pods and checks if any pod is not running, does a describe on it, and prints the events (we get when we describe pods).

I’m stuck on the 2nd task here, where I can fetch ‘stdout’ but cannot filter the pod name from stdout. Please suggest how I can implement this.

I’m passing services_to_upgrade as an argument while running the playbook.

  • name: Get pod information
    command: “kubectl get pods -l app={{ item }} -o json”
    register: pods_info
    with_items: “{{ services_to_upgrade.keys() }}”

  • name: Print pod info
    set_fact:
    pod_name: “{{ pods_info.results| json_query(‘[*].stdout’) }}”

I am looking forward to any suggestions.

Thanks

Impossible without knowing what your variables look like. Provide those and we may be able to help

Thanks for replying.

This is how I’m passing the variables -

ansible-playbook --extra-vars “{‘services_to_upgrade’:{‘hedwig’:‘1.0.9-63’,‘venus’:‘1.0.11-2198’}}”

Try to remove the * from [*] and the square brackets from stdout ( change ‘stdout’ becomes ‘stdout’ ). This will give you all of list of stdout strings. If you the individual lines of stdout then use stdout_lines.

set_fact:
pod_name: “{{ pods_info.results | json_query(‘.stdout’) }}”

Walter