[ansible-project] Ternary or when in ansible

33 views
Skip to first unread message

Prady A

unread,
Mar 7, 2024, 12:14:31 AM3/7/24
to ansible...@googlegroups.com
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 <UK CAPSUSULE SERVER>
then 
Subscription-manager <US CAPSUSULE SERVER>


Regards 





Todd Lewis

unread,
Mar 7, 2024, 7:42:48 AM3/7/24
to ansible...@googlegroups.com, uto...@gmail.com
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.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAEuB3ArAMzXhjok0YTnJ40a3QbCKm2-pAhR%2Bb%3DRn3m%3DLiPkmgQ%40mail.gmail.com.

-- 
Todd

Prady A

unread,
Mar 7, 2024, 8:44:47 AM3/7/24
to ansible...@googlegroups.com
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

Prady A

unread,
Mar 7, 2024, 10:02:46 AM3/7/24
to ansible...@googlegroups.com
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

Dick Visser

unread,
Mar 7, 2024, 10:36:35 AM3/7/24
to ansible...@googlegroups.com
On Thu, 7 Mar 2024 at 16:02, Prady A <pradyu...@gmail.com> wrote:
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 }}



Prady A

unread,
Mar 7, 2024, 6:38:54 PM3/7/24
to ansible...@googlegroups.com
Thanks Dick that’s what I needed 

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages