Add an item to a list dependent on a conditional in ansible

前端 未结 4 560
情书的邮戳
情书的邮戳 2021-01-17 16:04

I would like to add an item to a list in ansible dependent on some condition being met.

This doesn\'t work:

  some_dictionary:
    app:
       - some         


        
相关标签:
4条回答
  • 2021-01-17 16:38

    Is there a reason you have to do everything in one go?

    This is pretty easy if you specify the additional item(s) to add in separate vars, as you can just do list1 + list2.

    ---
    - hosts: localhost
      gather_facts: False
      connection: local
      vars:
        mylist:
          - one
          - two
        mycondition: False
        myconditionalitem: foo
      tasks:
        - debug:
            msg: "{{ mylist + [myconditionalitem] if mycondition else mylist }}"
    
    0 讨论(0)
  • 2021-01-17 16:51

    I'd try to avoid this, but if conditional list is absolutely necessary, you can use this trick:

    ---
    - hosts: localhost
      gather_facts: no
      vars:
        a: 1
        b: 1
        c: 2
        some_dictionary:
          app: "{{ '[\"something\", \"something else\"' + (a + b == c) | ternary(', \"something conditional\"',' ') + ']' }}"
      tasks:
        - debug: var=some_dictionary.app
    

    It will form an array-like string (["item1","item2","item3"]) and ansible variable templator will convert it into list before assigning to app.

    0 讨论(0)
  • 2021-01-17 16:51

    You can filter out all falsey values with select(), but remember to apply the list() filter afterwards. This seems an easier and more readable approach for me:

    - name: Test
      hosts: localhost
      gather_facts: no
      vars:
          mylist:
            - "{{ (true)  | ternary('a','') }}"
            - "{{ (false) | ternary('b','') }}"
            - "{{ (true)  | ternary('c','') }}"
     
      tasks:
      - debug:
          var: mylist|select|list
    

    Result:

    TASK [debug] *****************************************************************************************************************************************************************************************************************
    ok: [localhost] => {
        "mylist|select()|list": [
            "a", 
            "c"
        ]
    }
    

    Replace (true) and (false) with whatever test you want.

    0 讨论(0)
  • 2021-01-17 16:54

    Based on Konstantin's solution I developed the following:

    - hosts: localhost
      gather_facts: no
      vars:
        a: "{{ True if var1|d(True) else False }}"
        b: "{{ True if var2|d(False) else False }}"
        n: "{{ True if var2|d(True) else False }}"
        some_list: "{{ '[' +
                         a|ternary('\"item1\",',' ') +
                         b|ternary('\"item2\",',' ') +
                         n|ternary('\"itemN\",',' ') + ']' }}"
      tasks:
        - debug: var=some_list
    

    This will create a list with items "item1" till "itemN", but each item is only appended if the corresponding flag expands to 'True'.

    Hope, this helps.

    0 讨论(0)
提交回复
热议问题