如何过滤OS类别

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
posted @ 2023-05-28 02:15  MacoPlus  阅读(5)  评论(0编辑  收藏  举报