Score:0

ข้อความแสดงข้อผิดพลาดส่วนกลาง Ansible เมื่องานล้มเหลวบนโฮสต์ทั้งหมด

ธง cn
raw

ฉันต้องการข้อความแสดงข้อผิดพลาดเมื่องานล้มเหลวบนโฮสต์ทั้งหมด ตัวอย่างเช่น:

- ios_facts:
    Gather_subset: นาที
  ล้มเหลวเมื่อ: "{{ ansible_net_hostname }} มี 123"

โดยที่ชื่อโฮสต์ทั้งหมดมี 123 ดังนั้นจึงทริกเกอร์งานที่ล้มเหลว

- ล้มเหลว:
    msg: ชื่อโฮสต์ทั้งหมดประกอบด้วย 123
  เมื่อ: xxxxx
Score:2
ธง br

For example, given the inventory for testing

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

The play below shows how to find the lists

- 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

gives

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']

Compare the length of the lists

    - 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

(Credit @Zeitounator for pointing to this option.)

The query above works if you target all hosts in the inventory. If you'd like to target a group of hosts use the special variable ansible_play_hosts_all and extract the variables from the hostvars. For example, given the inventory

shell> cat hosts
[test_123]
host[0001:1024]

[test_123:vars]
ansible_net_hostname=host_123_A

The play

- 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

gives

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

To evaluate the condition compare the length of the lists as before.

cn flag
raw
ขอขอบคุณ ! ฉันมีอีกกรณีที่ฉันต้องเลือกข้อเท็จจริงที่ว่างเปล่า ฉันจะทำให้มันทำงานได้อย่างไร `_search: "{{ _names|select('search', '')|list }}"`
Zeitounator avatar
fr flag
ฉันคิดว่า OP ต้องการทราบว่าชื่อโฮสต์ทั้งหมด **ใน host play loop** ผ่านการทดสอบหรือไม่ คำตอบของคุณคือการดูสินค้าคงคลังทั้งหมดโดยไม่คำนึงถึงเป้าหมายปัจจุบันที่กำลังเล่นอยู่
Zeitounator avatar
fr flag
@raw คำถามอื่นๆ จะอยู่ในโพสต์ใหม่ หรืออย่างน้อยที่สุดในการแก้ไขคำถามปัจจุบันของคุณ ไม่ใช่ในความคิดเห็น ขอบคุณ.
br flag
@Zeitounator: ไม่มีโฮสต์อื่นใน *hostvars* ยกเว้นโฮสต์ทั้งหมดในการเล่น
Zeitounator avatar
fr flag
@VladimirBotka ฉันต้องไม่เห็นด้วยกับสิ่งสุดท้ายนี้: https://gist.github.com/zeitounator/3a3ac34b96ccd49998523adf7da3e1cd เนื่องจากคุณจะเห็นว่าฉันสร้างการเล่นที่กำหนดเป้าหมายไปยังกลุ่มเฉพาะและ hostvars ยังมีรายการทั้งหมดสำหรับเป้าหมายทั้งหมดในคลังทั้งหมด ในขณะเดียวกันฉันเห็นได้ชัดว่าตีความผิดจากคำถามเนื่องจากดูเหมือนว่าจะทำงานต่อไป;)
br flag
@Zeitounator. เข้าใจแล้ว. ฉันอัปเดตคำตอบแล้ว ขอขอบคุณ.

โพสต์คำตอบ

คนส่วนใหญ่ไม่เข้าใจว่าการถามคำถามมากมายจะปลดล็อกการเรียนรู้และปรับปรุงความสัมพันธ์ระหว่างบุคคล ตัวอย่างเช่น ในการศึกษาของ Alison แม้ว่าผู้คนจะจำได้อย่างแม่นยำว่ามีคำถามกี่ข้อที่ถูกถามในการสนทนา แต่พวกเขาไม่เข้าใจความเชื่อมโยงระหว่างคำถามและความชอบ จากการศึกษาทั้ง 4 เรื่องที่ผู้เข้าร่วมมีส่วนร่วมในการสนทนาด้วยตนเองหรืออ่านบันทึกการสนทนาของผู้อื่น ผู้คนมักไม่ตระหนักว่าการถามคำถามจะมีอิทธิพลหรือมีอิทธิพลต่อระดับมิตรภาพระหว่างผู้สนทนา