# A way to include a string separator e.g. \`/\` based on whether a value exists in a Jinja2 template

**URL:** <https://forum.ansible.com/t/a-way-to-include-a-string-separator-e-g-based-on-whether-a-value-exists-in-a-jinja2-template/5149>\
**Category:** Get Help\
**Created:** [April 25, 2024, 12:13pm UTC](https://forum.ansible.com/t/a-way-to-include-a-string-separator-e-g-based-on-whether-a-value-exists-in-a-jinja2-template/5149 "2024-04-25T12:13:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShanDesai](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@ShanDesai](https://forum.ansible.com/u/ShanDesai)\
**Post date:** [April 25, 2024, 12:13pm UTC](https://forum.ansible.com/t/a-way-to-include-a-string-separator-e-g-based-on-whether-a-value-exists-in-a-jinja2-template/5149/1 "2024-04-25T12:13:26Z")

</div>

I have a bunch of container images running on my internal registry and some images which I wish to pull from the Public Docker Hub.

Docker Hub’s registry is the default value when there is no specific registry name defined.

The way the docker client can pull the image from distinct registries is by the following string:

```auto
registry_uri/namespace/image:version

```

for the following variables that can be injected into a playbook how can set some sensible defaults so that:

when `registry_uri` exists add the `/` to the Jinja2 template else just use the `namespace/image:version`?

I am currently stuck with:

```yaml
- community.docker.docker_pull:
    name: |
      {{ item.registry_uri | default('docker.io') }}/
      {{ item.namespace | default( 'library') }}/
      {{ item.image_name }}:{{ item.image_version | default('latest') }}
  loop: "{{ images }}"

```

where `images`:

```yaml

images:
  - registry_uri: myregistry.internal # should pull from internal reg
    namespace: myns
    image_name: img1
 - image_name: centos # should pull from docker hub

```

The string separator `/` is a bit mischievous when it comes to pulling images from distinct registries.

Any help would appreciated.

### In a nutshell

for images from docker hub i would like the jinja2 template to resolve to just the name of the image if registry / namespace are not mentioned

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [April 25, 2024, 1:06pm UTC](https://forum.ansible.com/t/a-way-to-include-a-string-separator-e-g-based-on-whether-a-value-exists-in-a-jinja2-template/5149/2 "2024-04-25T13:06:00Z")

</div>

Hi, how about using `ansible.builtin.path_join`?

```yaml
- community.docker.docker_pull:
    name: "{{ image | ansible.builtin.path_join }}"
  vars:
    image:
      - "{{ item.registry_uri | default('') }}"
      - "{{ item.namespace | default('') }}"
      - "{{ item.image_name }}:{{ item.image_version | default('latest') }}"
  loop: "{{ images }}"

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 25, 2024, 1:06pm UTC](https://forum.ansible.com/t/a-way-to-include-a-string-separator-e-g-based-on-whether-a-value-exists-in-a-jinja2-template/5149/3 "2024-05-25T13:06:27Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
