目录

主要用途

参数总结

语法示例

1. 收集所有主机的信息

2. 过滤特定信息

3. 列出所有内存信息

4. 收集特定变量的信息

5. 使用 gather_subset

Playbook示例

基本语法

使用示例

1. 基本使用

2. 使用 gather_subset 参数

3. 使用 filter 参数

4. 使用 register 捕获输出

5. 使用 gather_timeout 参数

综合示例

进阶示例


 

Ansible的 setup 模块是一个用于收集远程主机信息的内置模块,主要用于获取目标机器的系统信息并返回。这些信息包括操作系统类型、内核版本、网络接口、内存、CPU等各种硬件和软件指标。这些信息会以facts的形式保存,可以在后续的任务中引用和使用。

主要用途

  1. 获取主机信息:自动收集目标主机的详细信息。
  2. 条件化任务:根据目标主机的硬件或软件指标来执行不同的任务。
  3. 调试和排查:通过收集的系统信息来进行调试和系统状态检查。

 

参数总结

  1. filter:

    • 描述:指定仅收集符合此模式的 facts。
    • 类型:字符串或列表
    • 默认值:无
  2. gather_subset:

    • 描述:指定要收集的 facts 子集。
    • 类型:字符串或列表
    • 可选值:
      • all:收集所有信息(默认)
      • min:收集最小信息集
      • hardware:收集硬件信息
      • network:收集网络信息
      • virtual:收集虚拟化信息
      • ohai:收集 Ohai 插件信息(如果安装了 Ohai)
      • facter:收集 Facter 插件信息(如果安装了 Facter)
  3. gather_timeout:

    • 描述:收集 facts 的超时时间(秒)。
    • 类型:整数
    • 默认值:10
  4. fact_path:

    • 描述:指定自定义 facts 的路径(JSON 格式)。
    • 类型:字符串
    • 默认值:无
  5. filter_type:

    • 描述:过滤 facts 时的类型。
    • 类型:字符串
    • 可选值:include(默认),exclude

 

语法示例

1. 收集所有主机的信息

使用 ansible 命令来收集所有主机的信息:

ansible all -m setup

2. 过滤特定信息

通过 filter 参数收集特定类型的信息(例如,只收集与网络接口相关的信息):

ansible all -m setup -a "filter=ansible_interfaces"

 

3. 列出所有内存信息

只收集内存相关的信息:

ansible all -m setup -a "filter=ansible_memtotal_mb"

4. 收集特定变量的信息

指定收集 ansible_distribution(操作系统发行版信息):

ansible all -m setup -a 'filter=ansible_distribution'

5. 使用 gather_subset

通过 gather_subset 参数可以控制收集的信息子集,例如:只收集最小的核心信息:

ansible all -m setup -a "gather_subset=min"

可以指定多个子集,用逗号分隔,收集网络和硬件信息:

ansible all -m setup -a "gather_subset=network,hardware"

Playbook示例

 

基本语法
- name: Task description
  setup:
    gather_subset: # (Optional) 获取特定子集信息,如 min, hardware, network 等
    gather_timeout: # (Optional) 获取信息的超时时间,单位为秒
    filter: # (Optional) 过滤特定信息,如 ansible_eth*, ansible_os_family 等
    fact_path: # (Optional) 自定义的 fact 路径

使用示例

1. 基本使用

在远程主机上收集所有的系统信息:

---
- name: Collect all system information
  hosts: all
  tasks:
    - name: Gather all facts
      setup:

2. 使用 gather_subset 参数

只收集最小的核心信息:

---
- name: Collect minimal system information
  hosts: all
  tasks:
    - name: Gather minimal facts
      setup:
        gather_subset: min

只收集网络相关的信息:

---
- name: Collect network information
  hosts: all
  tasks:
    - name: Gather network facts
      setup:
        gather_subset: network

3. 使用 filter 参数

只收集特定的 facts 信息:

---
- name: Collect specific information
  hosts: all
  tasks:
    - name: Gather specific facts
      setup:
        filter: ansible_all_ipv4_addresses

4. 使用 register 捕获输出

将 setup 模块的输出捕获到变量中,以便在后续任务中使用:

---
- name: Register setup module output
  hosts: all
  tasks:
    - name: Gather all facts
      setup:
      register: setup_facts

    - name: Print the gathered facts
      debug:
        var: setup_facts

5. 使用 gather_timeout 参数

设置获取信息的超时时间为 20 秒:

---
- name: Collect system information with timeout
  hosts: all
  tasks:
    - name: Gather facts with timeout
      setup:
        gather_timeout: 20

综合示例

结合多个参数的使用示例:

---
- name: Comprehensive setup module usage
  hosts: all
  tasks:
    - name: Gather minimal and network facts
      setup:
        gather_subset:
          - min
          - network

    - name: Gather specific facts
      setup:
        filter: ansible_all_ipv4_addresses

    - name: Gather facts with timeout
      setup:
        gather_timeout: 10

    - name: Gather customized facts path
      setup:
        fact_path: /etc/ansible/facts.d

进阶示例

结合 setup 模块收集的信息执行条件化任务:

---
- name: Conditional tasks based on gathered facts
  hosts: all
  tasks:
    - name: Gather system facts
      setup:
      register: system_facts

    - name: Conditionally install package if CentOS
      yum:
        name: httpd
        state: present
      when: system_facts.ansible_facts['ansible_distribution'] == "CentOS"

    - name: Conditionally install package if Ubuntu
      apt:
        name: apache2
        state: present
      when: system_facts.ansible_facts['ansible_distribution'] == "Ubuntu"
本站无任何商业行为
个人在线分享 » Ansible——setup模块
E-->