ansible.builtin.getent 模块 – Unix getent 实用程序的包装器

注意

此模块是 ansible-core 的一部分,包含在所有 Ansible 安装中。在大多数情况下,即使不指定 集合关键字,您也可以使用简短的模块名称 getent。但是,我们建议您使用 完全限定的集合名称 (FQCN) ansible.builtin.getent 以便轻松链接到模块文档并避免与可能具有相同模块名称的其他集合发生冲突。

概要

  • 针对其各种数据库之一运行 getent,并将信息返回到主机的 facts 中,以 getent_<database> 为前缀的变量。

参数

参数

注释

database

字符串 / 必需

目标系统支持的 getent 数据库的名称(passwd、group、hosts 等)。

fail_key

布尔值

如果提供的键丢失,如果为 true,则会使任务失败。

选择

  • false

  • true ← (默认)

key

字符串

要从指定的数据库中返回值的键,否则将返回完整内容。

service

字符串

在 Ansible 2.9 中添加

用指定的 service 覆盖所有数据库

底层系统必须支持 service 标志,该标志并不总是可用。

split

字符串

用于将数据库值拆分为列表/数组的字符,例如 :\t,否则它将尝试根据数据库选择一个。

属性

属性

支持

描述

check_mode

支持:完全支持

可以在 check_mode 下运行并返回更改状态预测,而不会修改目标,如果不支持,则会跳过操作。

diff_mode

支持:不支持

在 diff 模式下,将返回有关已更改内容(或可能在 check_mode 下需要更改的内容)的详细信息

facts

支持:完全支持

操作返回一个 ansible_facts 字典,它将更新现有的主机 facts

platform

平台: posix

可以对其进行操作的目标操作系统/系列

说明

注意

  • 并非所有数据库都支持枚举,请查看系统文档以了解详细信息。

示例

- name: Get root user info
  ansible.builtin.getent:
    database: passwd
    key: root
- ansible.builtin.debug:
    var: ansible_facts.getent_passwd

- name: Get all groups
  ansible.builtin.getent:
    database: group
    split: ':'
- ansible.builtin.debug:
    var: ansible_facts.getent_group

- name: Get all hosts, split by tab
  ansible.builtin.getent:
    database: hosts
- ansible.builtin.debug:
    var: ansible_facts.getent_hosts

- name: Get http service info, no error if missing
  ansible.builtin.getent:
    database: services
    key: http
    fail_key: False
- ansible.builtin.debug:
    var: ansible_facts.getent_services

- name: Get user password hash (requires sudo/root)
  ansible.builtin.getent:
    database: shadow
    key: www-data
    split: ':'
- ansible.builtin.debug:
    var: ansible_facts.getent_shadow

返回的事实

此模块返回的事实将添加到 hostvars 主机 facts 中,并可以像任何其他主机 fact 一样按名称引用。它们不需要注册才能使用。

描述

getent_<database>

列表 / 元素=字符串

结果列表或单个结果作为 db 提供的字段的列表

列表元素取决于查询的数据库,请参阅 getent 手册页以了解结构

从 2.11 开始,它现在返回多个重复条目,以前它只返回最后一个条目

返回:始终

作者

  • Brian Coca (@bcoca)