# Ternary or when in ansible

**URL:** <https://forum.ansible.com/t/ternary-or-when-in-ansible/38707>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [March 7, 2024, 5:14am UTC](https://forum.ansible.com/t/ternary-or-when-in-ansible/38707 "2024-03-07T05:14:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [March 7, 2024, 5:14am UTC](https://forum.ansible.com/t/ternary-or-when-in-ansible/38707/1 "2024-03-07T05:14:08Z")

</div>

Hi Ansible experts

I am confused which one to use

I want to subscribe US servers default but if the server is in UK I want to subscription should Point to UK capsule server…

If IS\_UK= TRUE  
Subscription-manager   
then

Subscription-manager

Regards

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [March 7, 2024, 12:42pm UTC](https://forum.ansible.com/t/ternary-or-when-in-ansible/38707/2 "2024-03-07T12:42:33Z")

</div>

Is “Subscription-manager” a command you want to run, or part of a string you’re trying to initialize, or are you trying to set a variable for later use?  
Is “IS\_UK” a variable you already have, or is the problem how to create or set “IS\_UK” itself?

In other words, you’ve described very well what you want to do, but you haven’t described at all the problem(s) you’ve encountered doing it with Ansible.

---

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [March 7, 2024, 1:44pm UTC](https://forum.ansible.com/t/ternary-or-when-in-ansible/38707/3 "2024-03-07T13:44:18Z")

</div>

Hi TODD,

Thanks for your reply.  
Yes I ve that variable(IS\_UK= TRUE)  
defined already and it is coming from upstream.  
Just need to the logic for efficient if then syntax in ansible.

Secondly subscription manager is shell command.

Regards  
Prady

---

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [March 7, 2024, 3:02pm UTC](https://forum.ansible.com/t/ternary-or-when-in-ansible/38707/4 "2024-03-07T15:02:24Z")

</div>

Hi again

For simplicity I thought to do like this.

Current code

- name: Subscribe my repo  
shell: |  
subscription-manager register bla bla

**After**

- name: Subscribe US REPO  
shell: |  
subscription-manager register US server

When:  
IS\_UK = false

- name: Subscribe UK REPO  
shell: |  
subscription-manager register UK server

When:  
IS\_UK = True

Regards  
PD

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [March 7, 2024, 3:36pm UTC](https://forum.ansible.com/t/ternary-or-when-in-ansible/38707/5 "2024-03-07T15:36:03Z")

</div>

I would use a helper variable to you can reuse in some places, for example in the task title:

vars:  
IS\_UK: false  
server: “{{ IS\_UK | ternary(‘UK-server’, ‘US-server’) }}”

tasks:

- name: “Subscribe to {{ server }} repo”  
ansible.builtin.shell:  
echo subscription-managed register {{ server }}

---

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [March 7, 2024, 11:38pm UTC](https://forum.ansible.com/t/ternary-or-when-in-ansible/38707/6 "2024-03-07T23:38:30Z")

</div>

Thanks Dick that’s what I needed
