1

I have been looking for a solution, none seems to work so far.

I have a task output registered as:

"action_list.start_artifacts": [
    {
        "account-troubletree-v1-1-0-18-21001": "docker run -d -p 21001:8080 -v /apps/projects/logs/account-troubletree-v1-1-0-18-21001:/logs -v /apps/projects/logs/account-troubletree-v1-1-0-18-21001/.configmap:/tibco/config -v /apps/gwportal/appdynamics/:/apps/gwportal/appdynamics/ --env-file /apps/projects/logs/account-troubletree-v1-1-0-18-21001/config.env --name account-troubletree-v1-1-0-18-21001 account-troubletree-v1:1.0.18"
    },
    {
        "account-troubletree-v1-1-0-17-21002": "docker run -d -p 21002:8080 -v /apps/projects/logs/account-troubletree-v1-1-0-17-21002:/logs -v /apps/projects/logs/account-troubletree-v1-1-0-17-21002/.configmap:/tibco/config -v /apps/gwportal/appdynamics/:/apps/gwportal/appdynamics/ --env-file /apps/projects/logs/account-troubletree-v1-1-0-17-21002/config.env --name account-tree-v1-1-0-17-21002 account-tree-v1:1.0.17"
    }
]

I am trying to get the key name using below task, but its failing.

- debug:
    msg: "{{ item.key }}"
  loop: "{{ action_list.start_artifacts }}"
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Venu S
  • 3,251
  • 1
  • 9
  • 25
  • are the dictionary variables dynamic ? i mean do the variables (`account-troubletree-v1-1-0-18-21001`, `account-troubletree-v1-1-0-17-21002`) always have the same value ? – codeaprendiz Feb 24 '21 at 08:23
  • Yes they are dynamic , their value can change – Venu S Feb 24 '21 at 08:47
  • ah okay, then why not use regex to search though the output and set facts using regex search - [link1](https://stackoverflow.com/questions/55046531/ansible-regex-search-with-variable) [link2](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html) – codeaprendiz Feb 24 '21 at 08:50

2 Answers2

1

The task below does the job

    - debug:
        msg: "{{ item.keys()|first }}"
      loop: "{{ action_list.start_artifacts }}"

Next option is

    - debug:
        msg: "{{ (item|dict2items).0.key  }}"
      loop: "{{ action_list.start_artifacts }}"
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • Thanks Vladimir, you are always a savior on ansible questions. can you explain why ansible isn't able to get the key if I mention "{{ item[0].key }}" ? – Venu S Feb 24 '21 at 15:20
  • 1
    This is a wrong question. Why do you expect the `key` method should be available? – Vladimir Botka Feb 24 '21 at 16:43
  • Hmm, nevermind, I thought since looped item is a dictionary here, we could simply get the key of the dictionary but this .key attribute. – Venu S Feb 24 '21 at 18:28
  • 2
    There is no `key` attribute. It's possible to create one by `item|dict2items`. I've added an example. – Vladimir Botka Feb 24 '21 at 19:27
  • @VenuS [`.keys()`](https://docs.python.org/3/library/stdtypes.html#dict.keys) is a Python function on dictionaries. You can't just go and make it a singular, then expect it to work. – β.εηοιτ.βε Feb 24 '21 at 19:44
0

You can use the following to loop over dictionary variables is the variables are static of-course

  • default.yaml having the variable
action_list.start_artifacts:
  - account-troubletree-v1-1-0-18-21001: "docker run -d -p 21001:8080 -v /apps/projects/logs/account-troubletree-v1-1-0-18-21001:/logs -v /apps/projects/logs/account-troubletree-v1-1-0-18-21001/.configmap:/tibco/config -v /apps/gwportal/appdynamics/:/apps/gwportal/appdynamics/ --env-file /apps/projects/logs/account-troubletree-v1-1-0-18-21001/config.env --name account-troubletree-v1-1-0-18-21001 account-troubletree-v1:1.0.18"
    account-troubletree-v1-1-0-17-21002: "docker run -d -p 21002:8080 -v /apps/projects/logs/account-troubletree-v1-1-0-17-21002:/logs -v /apps/projects/logs/account-troubletree-v1-1-0-17-21002/.configmap:/tibco/config -v /apps/gwportal/appdynamics/:/apps/gwportal/appdynamics/ --env-file /apps/projects/logs/account-troubletree-v1-1-0-17-21002/config.env --name account-tree-v1-1-0-17-21002 account-tree-v1:1.0.17"
  • task to use the variable
- debug:
  msg: "{{ item.account-troubletree-v1-1-0-18-21001 }} "
  with_items: "{{ action_list.start_artifacts }}"

However if they are dynamic then we can go for solutions like regex-search regex-filter

codeaprendiz
  • 2,703
  • 1
  • 25
  • 49