using registered variables from .podman_container_info module

Viewed 40

I'm attempting to use a registered variable from the output from the containers.podman.podman_container_info ansible module. Basically I'm doing this the following

- name: checking if gitlab-runner is present
  containers.podman.podman_container_info:
    name: gitlab-runner
  register: gitlabrunner_status

However, the next step I want to add a conditional to only run the next step if this container is defined based on the name. For testing I'm just using the debug module, so here is what I have tried.

- name: debug
  debug:
    var: gitlabrunner_status.containers.Name
- name: debug
  debug:
    var: gitlabrunner_status["containers"]["Name"]

each attempt I ended up getting VARIABLE IS NOT DEFINED! So how can I just pull the name from the output to use in a conditional, from this complex output.

1 Answers

If this:

- name: debug
  debug:
    var: gitlabrunner_status.containers.Name

Is giving you a VARIABLE IS NOT DEFINED error, then make sure the parent variable has the content you expect. That is, start with a task like this:

- name: debug
  debug:
    var: gitlabrunner_status.containers

Looking at that output, we see that containers is a list that looks like:

"gitlabrunner_status.containers": [
    {
        ...
        "Name": "gitlab-runner",
        ...
    }
]

Because containers is a list, we can't simply ask for gitlabrunner_status["containers"]["Name"]. We need a list index into the containers list:

- name: debug
  debug:
    var: gitlabrunner_status.containers.0.Name
Related