Or maybe a series of set_fact: statements:
- set_fact
group: 'BAD'
- set_fact:
group: grp1
when: c == 1
- set_fact:
group: grp2
when: c == 2
- set_fact:
group: grp3
when: c == 3
[...etc...]
- fail:
msg: "Invalid value for 'c' or 'c' not set!"
when: group == 'BAD'
Regards, K.
On Mon, Jun 3, 2019 at 4:52 PM S C Rigler <[email protected]> wrote:
> In your example of what you're trying to do, you're setting "group" inside
> of the template but never returning it to the task. What you might want is
> something like this:
>
> - name: Set Group
> set_fact:
> group: |
> {% if c == 1 %}
> grp1
> {% elif c ==2 %}
> grp3
> {% else %}
> grp4
> {% endif %}
>
> -- Steve
>
> On Mon, Jun 3, 2019 at 12:49 AM Ansar Sahit <[email protected]> wrote:
>
>> Hi All,
>>
>> Im trying to choose ansible inventory host group conditionally.
>>
>> - name: Set group
>> set_fact:
>> group: |
>> {% if c == 1 %}
>> {% set group = 'grp1' %}
>> {% elif c == 2 %}
>> {% set group == 'grp3' %}
>> {% else %}
>> {% group == 'grp4' %}
>> {% endif %}
>>
>> - debug:
>> var=group
>>
>>
>> This doesnt seem to work.
>>
>> Whats the right way to use if-else with ansible set??
>>
>> Kinldy assist.
>>
>> --
>> 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 post to this group, send email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ansible-project/de181027-dc27-4190-8d1e-1e5feec12546%40googlegroups.com
>> <https://groups.google.com/d/msgid/ansible-project/de181027-dc27-4190-8d1e-1e5feec12546%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
> --
> 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 post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAFbiokdBbOsnz5nVCMbX-9ouTTL4K8jL2WNoQKz-F-MBYZq0fg%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAFbiokdBbOsnz5nVCMbX-9ouTTL4K8jL2WNoQKz-F-MBYZq0fg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CA%2B%2BT08Tc%3Db5qVFEOA14dYp68cxcOZGoDS4eyR1wD%3DaVBQMX3SQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.