Hi Rigler ,

Another doubt,

I'm trying to use this "if else block" with "set" inside a variable jinja2
file used in roles templating.

Cat varfile.yml.j2

----
        {% if c == 1 %}
        {% set group = 'grp1' %}
        {% elif c == 2 %}
        {% set group == 'grp3' %}
        {% else %}
        {% group == 'grp4' %}
        {% endif %}

Groupname : {{ group }}
Var: "test"

This groupname should get populated in the variable file.

How can I achieve this?

On Jun 3, 2019 12:22 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.

-- 
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/CABNWynD0dGaNtfu7BPnq4-qi8RoTwRLbzDwUqqHsTbaPgTvxew%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to