You could use set_fact: stanzas instead, with when: clauses.

- set_fact:
    user_list: 'user1, user2, user3'
  when: server_type = 'db_server'

Even better, use lists rather than strings. You can always turn one into
the other, and lists are more flexible:

- set_fact:
    user_list: ['user1', 'user2', 'user3']
  when: server_type = 'db_server'
Or something like this:
vars:
   db_users: ['user1', 'user2', 'user3']
   web_users: ['user4', 'user5', 'user6']
   standard_users: 'user7', 'user8', 'user9']

- set_fact:
    user_list: "{{ db_users }}"
            when: server_type = 'db_server'
Or this:
vars:
   user_groups:
      {
         'db_server': ['user1', 'user2', 'user3'],
         'web_server': ['user4', 'user5', 'user6'],
         'standard_server': ['user7', 'user8', 'user9']
      }

- set_fact:
    user_list: "{{ user_groups[ server_type ] }}"

The syntax is off the top of my head so probably full of errors, but it
should give you some ideas.

Regards, K.

On Tue, Sep 3, 2019 at 11:28 PM Cade Lambert <[email protected]> wrote:

> The problem I come across is I'll need logic to decide on the content of a
> variable and that logic will turn into a long string.  For example, when
> determining which users to add to a system, we might have a variable like:
>
> user_list: "{{ 'user1,user2,user3' if server_type=db_server else
> 'user4,user5,user6' if server_type=web_server else 'user7,user8,user1' if
> server_type=standard_server }}'
>
> These can get pretty long depending on what we're trying to do, and fairly
> hard to read. I could do multi-line variables I guess, but was wondering if
> there's a more clever way to handle stuff like this.
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/38a5d109-d789-465b-9694-470c0b6b59d7%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/38a5d109-d789-465b-9694-470c0b6b59d7%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Karl Auer

Email  : [email protected]
Website: http://2pisoftware.com

GPG/PGP : 301B 1F4E 624D AD99 242C 7A68 EC24 7113 E854 4A4E
Previous: 958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CA%2B%2BT08Tn1fbfSx1g3BkQ7R3g1p3RjAZZm2v8-X%3DnsAX90-CVQA%40mail.gmail.com.

Reply via email to