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

前端 未结 4 559
情书的邮戳
情书的邮戳 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 }}"
    

提交回复
热议问题