My requirement is I want to run apt-get update and apt-get upgrade, and then I want to print the containers and their status

My requirement is I want to run apt-get update and apt-get upgrade, and then I want to print the containers and their status.
Below is my code.

  1. I want to list all the containers with their status(Running/Exited)
  2. Print the value (debug) as a list. Here I have a seperate debug for each value
---
- name: 'Run sudo apt update'
  hosts: myhost
  become: yes
  tasks:
    - name: 'Run the equivalent of "apt-get update" as a separate step'
      ansible.builtin.apt:
        update_cache: yes
        upgrade: yes

    - name: Get info on docker host
      community.docker.docker_host_info:
      register: result

    - name: Get info on docker host and list images
      community.docker.docker_host_info:
        images: true
      register: result

    - debug:
        var: result.host_info.Containers
    - debug:
        var: result.host_info.ContainersRunning
    - debug:
        var: result.host_info.ContainersStopped

Try this playbook:

---
- name: 'Run sudo apt update'
  hosts: all
  become: yes
  tasks:
    - name: 'Run the equivalent of "apt-get update" as a separate step'
      ansible.builtin.apt:
        update_cache: yes
        upgrade: yes

    - name: Get info on docker host
      community.docker.docker_host_info:
      register: result

    - name: Get info on docker host and list images
      community.docker.docker_host_info:
        images: true
        containers: true
      register: result

    - debug:
        var: result.host_info.Containers
    - debug:
        var: result.host_info.ContainersRunning
    - debug:
        var: result.host_info.ContainersStopped

    - name: Display information about all Docker containers
      debug:
        msg: "Container ID: {{ item.Id }} - Name: {{ item.Names[0] }} - Status: {{ item.Status }}"
      with_items: "{{ result.containers }}"

That is working thanks, but little modification, It is printing the item details also, which is making difficult to read the exact msg. Below is the exact output…

Expected:

"msg": "Name: /containername - Status: Up 9 hours"

Actual:

(
item={
'Id': 'adb87841e75vddg65ee9ddd2dbdccc0c90e70da9ea5ee8f55966f2b79fea17bb290e',
'Image': 'ImageName',
'Command': 'java -Djava.security.egd=file:/dev/./urandom -jar xyz.jar', 
'Created': 1714029071, 
'Status': 'Up 9 hours', 
'Ports': [{'IP': '0.0.0.0', 
'PrivatePort': 8802, 
'PublicPort': 8083, 
'Type': 'tcp'}, 
{
'IP': '::', 
'PrivatePort': 8802, 
'PublicPort': 8083, 
'Type': 'tcp'
}
], 
'Names': 
['/containername']
}) => {
    "msg": "Name: /containername - Status: Up 9 hours"
}
)

You can add this to your ansible.cfg:

[defaults]
display_args_to_stdout = False