【问题标题】:Remove empty element from list in ansible从ansible列表中删除空元素
【发布时间】:2021-06-17 03:24:42
【问题描述】:
input:
  - tests_to_run

tasks:
  modify_user_input:
         < Remove the empty elements from tests_to_run>


已经搜索了几个小时。有谁知道过滤输入的方法,所以我可以操作 tests_to_run 列表,所以它没有任何空元素。

示例输入和输出

Sample tests_to_run = ['test_a','','test_b','test_c','yrls']
Expected variable to be used in later tasks test_to_run_clean = ['test_a','test_b','test_c','yrls']

【问题讨论】:

    标签: ansible yaml


    【解决方案1】:

    从列表中删除空元素:

    ---
    
    - name: Sample playbook
      connection: local
      gather_facts: false
      hosts: localhost
      vars:
        mylist:
          - abc
          - def
          - 
          - jkl
    
      tasks:
    - set_fact:
            mylist: "{{ mylist|select()|list }}"
    

    【讨论】:

    • 这种方法不会操纵 my_list,对吧?
    • 对,assert 模块只是检查(断言)条件。所以您的原始列表不会改变。
    • 尝试更新的答案。这将通过从列表中删除空元素来修复您的列表。
    猜你喜欢
    • 2012-01-23
    • 1970-01-01
    • 2013-10-28
    • 2019-04-06
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    相关资源
    最近更新 更多