some ways to do this would be

using filters (clean way)
   set_fact:
        myvar: "{{ (ansible_os_family == 'RedHat') | ternary(100,200) }}"

or same thing via jinja conditionals

 {{ 100 if ansible_os_family == 'RedHat'  else 200 }}

with some 'or's

    {{ 100 if ansible_os_family == 'RedHat' or ansible_user_id ==
'vagrant'  else 200 }}


On Tue, Aug 8, 2017 at 11:08 AM, Ryan <jryan...@gmail.com> wrote:
> I need to set some variables that are the same no matter the client, and
> other variables need to be set conditionally based on values from Ansible
> facts.  Basically I need an if/else case statement.  What is the correct way
> to do this?
>
> Some simple examples can be found here
> (http://docs.ansible.com/ansible/latest/playbooks_conditionals.html#conditionals)
> but  this doesn't cover else conditions, and if I remove the second instance
> of VAR4, and put in condition that is not met, it still sets VAR4, when the
> match doesn't exist, so it seems like the "when" is not being evaluated.
>
>
> ---
>
> - name: Playbook Name
>
>   hosts:  all
>
>   gather_facts: yes
>
>   tasks:
>
>
>
>      - name: Set variables
>
>        vars:
>
>          VAR1: 5
>
>          VAR2: 10
>
>          VAR3: 0
>
>
>
>          VAR4: 100
>
>          when: (ansible_some_fact == "some_value1") or
>
>                (ansible_some_fact == "some_value2")
>
>
>
>          VAR4: 200
>
>          when: (ansible_some_fact == "some_value3") or
>
>                (ansible_some_fact == "some_value4")
>
> --
> 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-project+unsubscr...@googlegroups.com.
> To post to this group, send email to ansible-project@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/bafd7316-db91-47ec-98ee-2f71bc4005aa%40googlegroups.com.
> 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 ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAFUV_d6Z9xQerEuBNwwutXNZ0y_cX1pPpW7EWAbPhv7GPciXYQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to