1

I have two variables:

  • assets: holds a list of assets
  • asset_index: holds the index I am interested in ("0" when printed via debug)

How can I get the asset list entry at the specified position?

- debug:
    var: assets[0]

works but has the hardcoded index.

- debug:
    var: assets[ asset_index ]

gives me The error was: 'list object' has no attribute '0'

- debug:
    var: assets[ {{ asset_index }} ]

gives me template error while templating string: expected token ':', got '}'. String: {{ assets[ {{ asset_index }} ] }}'

1 Answer 1

1

Now that I had formulated the question I could better search on for some solution. Inspired by Converting string to integer in Ansible Playbook I tried

- debug:
    var: assets[ asset_index | int ]

...and so far I was setting the variable using that filter but it always came back as string. It seems in Ansible you have to perform conversion when using the value, not when assigning it to a variable.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .