Ternary or when in ansible

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

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.

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

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

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 }}

Thanks Dick that’s what I needed