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

前端 未结 4 608
情书的邮戳
情书的邮戳 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: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.

提交回复
热议问题