AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1089300
Accepted
raw
raw
Asked: 2022-01-08 03:40:50 +0800 CST2022-01-08 03:40:50 +0800 CST 2022-01-08 03:40:50 +0800 CST

当所有主机上的任务都失败时,Ansible 全局失败消息

  • 772

当所有主机上的任务都失败时,我需要一条失败消息。例如:

- ios_facts:
    gather_subset: min
  failed_when: "{{ ansible_net_hostname }} contains 123"

其中所有主机名都包含 123,因此它会触发失败任务

- fail:
    msg: all of the hostnames contains 123
  when: xxxxx
ansible
  • 0 0 个回答
  • 153 Views

0 个回答

  • Voted
  1. Best Answer
    Vladimir Botka
    2022-01-08T05:15:50+08:002022-01-08T05:15:50+08:00

    例如,给定用于测试的库存

    shell> cat hosts
    host1 ansible_net_hostname=host_123_A
    host2 ansible_net_hostname=host_123_B
    host3 ansible_net_hostname=host_123_C
    

    下面的播放显示了如何找到列表

    - hosts: all
      gather_facts: false
      tasks:
        - debug:
            var: ansible_net_hostname
        - debug:
            msg: |
              All names: {{ _names }}
              Search names: {{ _search }}
          vars:
            _names: "{{ hostvars|json_query('*.ansible_net_hostname') }}"
            _search: "{{ _names|select('search', '123')|list }}"
          run_once: true
    

    给

    TASK [debug] ***********************************************************
    ok: [host1] => 
      ansible_net_hostname: host_123_A
    ok: [host2] => 
      ansible_net_hostname: host_123_B
    ok: [host3] => 
      ansible_net_hostname: host_123_C
    
    TASK [debug] ***********************************************************
    ok: [host1] => 
      msg: |-
        All names: ['host_123_A', 'host_123_B', 'host_123_C']
        Search names: ['host_123_A', 'host_123_B', 'host_123_C']
    

    比较列表的长度

        - debug:
            msg: all of the hostnames contains 123
          vars:
            _names: "{{ hostvars|json_query('*.ansible_net_hostname') }}"
            _search: "{{ _names|select('search', '123')|list }}"
          when: _names|length == _search|length
          run_once: true
    

    (信用@Zeitounator 指向这个选项。)

    如果您以清单中的所有主机为目标,则上述查询有效。如果您想定位一组主机,请使用特殊变量ansible_play_hosts_all并从hostvars中提取变量。例如,给定库存

    shell> cat hosts
    [test_123]
    host[0001:1024]
    
    [test_123:vars]
    ansible_net_hostname=host_123_A
    

    表演

    - hosts: test_123
      gather_facts: false
      tasks:
        - debug:
            msg: |
              All names: {{ _names|length }}
              Search names: {{ _search|length }}
          vars:
            _names: "{{ ansible_play_hosts_all|
                        map('extract', hostvars, 'ansible_net_hostname')|
                        list }}"
            _search: "{{ _names|select('search', '123')|list }}"
          run_once: true
    

    给

    TASK [debug] *****************************************************
    ok: [host0001] => 
      msg: |-
        All names: 1024
        Search names: 1024
    

    要评估条件,请像以前一样比较列表的长度。

    • 2

相关问题

  • Ansible:将字符串转换为字典

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve