【问题标题】:Hide skipped ansible tasks without using display_skipped_hosts隐藏跳过的 ansible 任务而不使用 display_skipped_hosts
【发布时间】:2021-05-06 17:20:16
【问题描述】:

我对此有一个可靠的角色:

---
- import_tasks: foo.yml
  when: ShouldRunFoo

如果ShouldRunFoo == false 则跳过该文件,但控制台显示其任务(如“跳过”)。

我可以使用display_skipped_hosts = noconfig option,但这会隐藏所有在剧本中跳过的内容,而不仅仅是foo.yml

有没有办法做到这一点?我想查看跳过的任务,而不是来自foo.yml 的任务(如果被跳过的话)。

【问题讨论】:

    标签: ansible


    【解决方案1】:

    前段时间我为此苦苦挣扎,并使用不同的来源发现了一种不太干净但可以解决问题的方法。 (我假设你知道 display_skipped_hosts=no 并且你仍然想打印一些跳过的)

    您可以使用 Jinja 表达式执行循环以从调试输出中删除消息(将始终显示任务的标题):

    ---
    - hosts: all
      tasks:
        - name: task to skip and display
          debug:
            msg: "HELLO WORLD"
          when: "'SOMETHING' in group_names"
        - name: task to skip and  not display
          debug:
            msg: "HELLO WORLD"
          loop: "{% if 'SOMETHING' in group_names%} {{debug_list}}{% else %}[]{% endif %}"
    

    【讨论】:

    • 谢谢,但是在导入任务时如何使用呢?循环不适用于导入。所有这些变量是什么,没有上下文,例如debug_list?
    【解决方案2】:

    如果在特定用例中,您可以使用import_tasksinclude_tasks,然后执行以下操作:

    $ cat playbook.yml:

    ---
    - hosts: localhost
      tasks:
        - name: subtasks will be printed
          import_tasks: tasks.yml
          when: false
        - name: subtasks will not be printed
          include_tasks: tasks.yml            # <----------
          when: false
    

    $ cat tasks.yml:

    ---
    - debug: msg=1
    - debug: msg=2
    - debug: msg=3
    

    $ ansible-playbook playbook.yml:

    PLAY [localhost] **************************************************************
    
    TASK [Gathering Facts] ********************************************************
    ok: [localhost]
    
    TASK [debug] ******************************************************************
    skipping: [localhost]
    
    TASK [debug] ******************************************************************
    skipping: [localhost]
    
    TASK [debug] ******************************************************************
    skipping: [localhost]
    
    TASK [subtasks will not be printed] *******************************************
    skipping: [localhost]
    
    PLAY RECAP ********************************************************************
    localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=4 rescued=0 ignored=0
    

    我想这并不总是适用,所以它不是一个完美的解决方案。

    【讨论】:

      猜你喜欢
      • 2017-01-04
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多