Ansible——setup模块

目录

主要用途

参数总结

语法示例

[1. 收集所有主机的信息](#1. 收集所有主机的信息)

[2. 过滤特定信息](#2. 过滤特定信息)

[3. 列出所有内存信息](#3. 列出所有内存信息)

[4. 收集特定变量的信息](#4. 收集特定变量的信息)

[5. 使用 gather_subset](#5. 使用 gather_subset)

Playbook示例

基本语法

使用示例

[1. 基本使用](#1. 基本使用)

[2. 使用 gather_subset 参数](#2. 使用 gather_subset 参数)

[3. 使用 filter 参数](#3. 使用 filter 参数)

[4. 使用 register 捕获输出](#4. 使用 register 捕获输出)

[5. 使用 gather_timeout 参数](#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"
相关推荐
groundhappy3 小时前
idalib安装和codex ida-mcp配置
linux·开发语言·python
通信小小昕3 小时前
Ubuntu 26.04 中文输入法安装
linux·运维·ubuntu
张小姐的猫4 小时前
【Linux】网络编程 —— HTTP协议(上)
linux·运维·服务器·网络·http·单例模式·策略模式
栩栩云生5 小时前
AI 写代码犯的错,早被写进了错题集
linux·安全·ai编程
imc.116 小时前
linux基础IO
linux·运维·服务器
BelongPanda8 小时前
Linux Nginx 纯手动 Let‘s Encrypt 泛域名证书配置教程
linux·nginx
酷可达拉斯8 小时前
Linux操作系统-shell编程(0)
linux·运维·服务器·python·云计算
2301_777998348 小时前
Linux中断机制:操作系统如何高效运行
linux·运维·服务器
yunwei378 小时前
eBPF 教程:检查 exec 后真正安装的可执行镜像
linux·开源
笑锝没心没肺9 小时前
yum update 报错:GPG key retrieval failed: [Errno 14] HTTPS Error 404 - Not Found
linux·运维·服务器