0

What I'm trying to achieve is something like this:

  - set_fact:
      var_1: value_1
    when: input.1 == 'condition_1'
    
  - set_fact:
      var_1: value_2
    when:
      - input.0 != 'condition_1'
      - input.1 != 'condition_2'
    
  - set_fact:
      var_1: value_3
    when: input.0 == 'condition_2'

Variable var_1 will have value according to which condition they are in (value_1, value_2, ..). The input is index from other variable input by the user that I want to use to as comparation to the condition

What should I do?

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
asking
  • 175
  • 2
  • 10
  • @U880D, i get syntax error when i tried to use the solution for my problem, is the use of passing value or AND might be a problem? – asking May 31 '22 at 06:37
  • 1
    Please [edit your question](/posts/72442474/edit) and provide an [mcve](/help/mcve) with a complete scenario containing valid ansible code. None of your examples are syntactically correct. You should also make clear if you want to [set a fact in a task](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/set_fact_module.html) or [declare a simple variable](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html). Make sure you provide a real world example for your inputs rather than pseudo code to rule out a possible [x/y problem](https://xyproblem.info). – Zeitounator May 31 '22 at 07:02
  • @Zeitounator i edited what i'm trying to achieve and adding some explanation under that – asking May 31 '22 at 07:19
  • @U880D, @Frenchy,@ β.εηοιτ.βε: You're all wrong in closing this question as duplicate! The question [Conditionally define variable in Ansible](https://stackoverflow.com/questions/31310688/conditionally-define-variable-in-ansible) asks ``'the variable to remain undefined if the condition does not resolve to true'``. Here the question asks to select a value by multiple conditions. I flagged the action. – Vladimir Botka May 31 '22 at 11:11
  • Still the same. You're wrong in asking to close the question as duplicate. There is no argument on this in your comment. I re-opened the question. – Vladimir Botka May 31 '22 at 12:35

2 Answers2

1

Create a dictionary of the values and conditions

  var_1_switch:
    value_1: "{{ input.1 == 'condition_1' }}"
    value_2: "{{ input.0 != 'condition_1' and input.1 != 'condition_2' }}"
    value_3: "{{ input.0 == 'condition_2' }}"

Then, for example, given the input input: [condition_1, condition_1], the dictionary evaluates to

  var_1_switch:
    value_1: true
    value_2: false
    value_3: false

and the declaration below evaluates the value of var_1

  var_1: "{{ var_1_switch|
             dict2items|
             selectattr('value')|
             map(attribute='key')|first }}"

gives

  var_1: value_1
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
1

Based on already given answers under

I've found the following approach

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    input:
      0: '0'
      1: '1'

  tasks:

  - set_fact:
      conditionalVar: "{{ 'v1' if (input.1 == '1') else 'v2' if (input.0 != '1' and input.1 != '2') else 'v3' if (input.0 == '2') }}"

  - name: Show value
    debug:
      msg: "{{ conditionalVar }}"

resulting into an output of

TASK [Show value] ***
ok: [localhost] =>
  msg: v1

for

    input:
      0: '0'
      1: '1'
TASK [Show value] ***
ok: [localhost] =>
  msg: v2

for

    input:
      0: '0'
      1: '0'

and

TASK [Show value] ***
ok: [localhost] =>
  msg: v3

for

    input:
      0: '2'
      1: '2'

To prevent runtime errors like

TASK [set_fact] ***************************************************************************************************************************************
fatal: [localhost]: FAILED! =>
  msg: |-
    The task includes an option with an undefined variable. The error was: the inline if-expression on line 1 evaluated to false and no else section was defined.

as for

    input:
      0: '1'
      1: '0'

one could implement an additional cleanup statement which comes in place if no condition is met.

  - set_fact:
      conditionalVar: "{{  'v1' if (input.1 == '1')
                      else 'v2' if (input.0 != '1' and input.1 != '2')
                      else 'v3' if (input.0 == '2')
                      else '' }}"
U880D
  • 8,601
  • 6
  • 24
  • 40