如何过滤OS类别

发布时间 2023-05-28 02:17:27作者: MacoPlus

ansible_os_family


  1. CentOS
    ---
    - name: Gather facts and filter CentOS hosts
      hosts: os_version
      gather_facts: true
    
      tasks:
        - name: Check CentOS hosts
          assert:
            that:
              - ansible_os_family == 'RedHat'
            fail_msg: "Non-CentOS host: {{ inventory_hostname }}"
            success_msg: "CentOS host: {{ inventory_hostname }}"
          changed_when: false
  2. Ubuntu

    ---
    - name: Gather facts and filter Ubuntu hosts
      hosts: os_version
      gather_facts: true
    
      tasks:
        - name: Check Ubuntu hosts
          assert:
            that:
              - ansible_os_family == 'Debian'
            fail_msg: "Non-Ubuntu host: {{ inventory_hostname }}"
            success_msg: "Ubuntu host: {{ inventory_hostname }}"
          changed_when: false

ansible_distribution


  1. CentOS
    ---
    - name: Gather facts and filter CentOS hosts
      hosts: os_version
      gather_facts: true
    
      tasks:
        - name: Check CentOS hosts
          assert:
            that:
              - ansible_distribution == 'CentOS'
            fail_msg: "Non-CentOS host: {{ inventory_hostname }}"
            success_msg: "CentOS host: {{ inventory_hostname }}"
          changed_when: false
  2. Ubuntu
    ---
    - name: Gather facts and filter Ubuntu hosts
      hosts: os_version
      gather_facts: true
    
      tasks:
        - name: Check Ubuntu hosts
          assert:
            that:
              - ansible_distribution == 'Ubuntu'
            fail_msg: "Non-Ubuntu host: {{ inventory_hostname }}"
            success_msg: "Ubuntu host: {{ inventory_hostname }}"
          changed_when: false