jinja2 split filter in ansible

Viewed 303

I am passing docker image as an argument to ansible playbook. In the playbook, I need to grab the registry, repository names and tag into separate variables.

I am trying to use split filter.

- set_fact:
    registry: "{{ source_image.split('/')[0] }}"
    repo_and_tag: "{{ source_image.split('/')[1] }}"

- set_fact:
    repo: "{{ repo_and_tag.split(':')[0] }}"
    tag: "{{ repo_and_tag.split(':')[1] }}"

Instead of using split filter multiple times, is there any better way of doing this?

Thanks.

1 Answers

You can create the lists first, e.g.

- set_fact:
    registry: "{{ _array.0 }}"
    repo_and_tag: "{{ _array.1 }}"
  vars:
    _array: "{{ source_image.split('/') }}"

- set_fact:
    repo: "{{ _array.0 }}"
    tag: "{{ _array.1 }}"
  vars:
    _array: "{{ repo_and_tag.split(':') }}"
Related