Ansible模块——主机名设置和用户/用户组管理
设置主机名
ansible.builtin.hostname
:
-
name
:要设置的主机名 -
use
:更新主机名的方式(默认会自动选择,不指定的话,物理机一般不会有问题,容器可能会有问题,一般是让它默认选择)-
systemd
:使用hostnamectl
(适用于支持 systemd 的系统,如 CentOS 7+、RHEL 7+、Ubuntu 16.04+) -
redhat
:使用传统的方式,如修改/etc/sysconfig/network
(适用于早期 RHEL 系统) -
debian
:修改/etc/hostname
和/etc/hosts
(适用于 Debian、Ubuntu 等) -
freebsd
:FreeBSD 系统的方式 -
suse
:SUSE 系统 -
全部可选项:
alpine
,debian
,freebsd
,generic
,macos
,macosx
,darwin
,openbsd
,openrc
,redhat
,sles
,solaris
,systemd
。
-
- name: Set a hostname specifying strategy
ansible.builtin.hostname:
name: web01
use: systemd
用户和用户组管理
用户组管理
ansible.builtin.group
:用于用户组管理
参数 | 类型 | 默认值 | 说明 |
name | str | null | 指定要创建、修改或删除的组的名称。这个参数是必需的,用来标识组。 |
force | bool | no | 如果为 |
gid | int | null | 设置组的 GID(组 ID),如果不提供,系统会自动分配一个。 |
local | bool | no | 如果为 |
non_unique | bool | no | 如果为 |
state | str | present | 可选值: |
system | bool | no | 如果为 |
- name: Create group
ansible.builtin.group:
name: redhat
gid: 10001
state: present
- name: Delete the group
ansible.builtin.group:
name: redhat
state: absent
用户管理
ansible.builtin.user
:用于用户的管理
选项 | 类型 | 默认值 | 说明 |
name | str | null | 要创建/管理的用户名。必需项。 |
append | bool | false | 添加附加组,而不是替换现有组列表。 |
authorization | str | null | 用于 AIX 系统指定授权信息。 |
comment | str | null | 用户描述(通常为全名)。 |
create_home | bool | true | 是否创建 home 目录。 |
expires | float | null | 账户过期时间,UNIX 时间戳。-1 表示永不过期。 |
force | bool | false | 强制执行用户操作(如移动 home 目录时)。 |
generate_ssh_key | bool | false | 是否自动生成 SSH 密钥对。 |
group | str | null | 指定用户的主组。 |
groups | list | null | 要将用户添加到的附加组列表。 |
hidden | bool | false | 对某些系统隐藏该用户(适用于macOS)。 |
home | str | null | 用户 home 目录路径。 |
local | bool | false | 仅在本地数据库中管理用户。 |
login_class | str | null | BSD 系统上的登录类。 |
move_home | bool | false | 移动 home 目录到新位置。 |
non_unique | bool | false | 允许重复 UID。 |
password | str | null | 用户加密后的密码。 |
password_expire_max | int | null | 密码最大使用天数。 |
password_expire_min | int | null | 密码最小使用天数。 |
password_expire_warn | int | null | 密码到期前的警告天数。 |
password_lock | bool | false | 锁定账户密码。 |
profile | str | null | 用户环境配置文件路径。 |
remove | bool | false | 删除用户时删除其 home 目录。 |
role | str | null | 设定用户登录角色。 |
seuser | str | null | SELinux 用户名。 |
shell | str | null | 用户默认 shell。 |
skeleton | str | null | 创建 home 目录时使用的 skeleton 目录。 |
ssh_key_bits | int | null | SSH 密钥长度。 |
ssh_key_comment | str | ansible-generated on $HOSTNAME | SSH 公钥注释。 |
ssh_key_file | str | .ssh/id_rsa | SSH 公钥存储路径。 |
ssh_key_passphrase | str | null | 用于保护私钥的密码。 |
ssh_key_type | str | rsa | SSH 密钥类型(如 rsa、ecdsa、ed25519)。 |
state | str | present | present 创建/更新用户;absent 删除用户。 |
system | bool | false | 是否为系统用户。 |
uid | int | null | 指定用户 ID。 |
umask | str | null | 用户默认 umask。 |
update_password | str | always | 控制是否更新密码,选项: |
常用选项:
选项 | 类型 | 默认值 | 说明 |
name | str | null | 要创建/管理的用户名。必需项。 |
append | bool | false | 添加附加组,而不是替换现有组列表。 |
comment | str | null | 用户描述(通常为全名)。 |
create_home | bool | true | 是否创建 home 目录。 |
expires | float | null | 账户过期时间,UNIX 时间戳。-1 表示永不过期。 |
generate_ssh_key | bool | false | 是否自动生成 SSH 密钥对。 |
group | str | null | 指定用户的主组。 |
groups | list | null | 要将用户添加到的附加组列表。 |
password | str | null | 用户加密后的密码。 |
password_expire_max | int | null | 密码最大使用天数。 |
password_expire_min | int | null | 密码最小使用天数。 |
password_expire_warn | int | null | 密码到期前的警告天数。 |
password_lock | bool | false | 锁定账户密码。 |
remove | bool | false | 删除用户时删除其 home 目录。 |
shell | str | null | 用户默认 shell。 |
ssh_key_bits | int | null | SSH 密钥长度。 |
ssh_key_comment | str | ansible-generated on $HOSTNAME | SSH 公钥注释。 |
ssh_key_file | str | .ssh/id_rsa | SSH 公钥存储路径。 |
ssh_key_passphrase | str | null | 用于保护私钥的密码。 |
ssh_key_type | str | rsa | SSH 密钥类型(如 rsa、ecdsa、ed25519)。 |
state | str | present | present 创建/更新用户;absent 删除用户。 |
system | bool | false | 是否为系统用户。 |
uid | int | null | 指定用户 ID。 |
umask | str | null | 用户默认 umask。 |
update_password | str | always | 控制是否更新密码,选项: |
- name: Create a user
ansible.builtin.user:
name: redhat
group: redhat
groups:
- wheel
append: true
#shell: /bin/bash
#generate_ssh_key: true
#ssh_key_bits: 2048
#ssh_key_file: .ssh/id_rsa
system: false
uid: 10001
umask: "0002"
comment: "test user"
password: "{{ 'redhat' | password_hash('sha512') }}"
#password: "$6$randomsalt$nyZMZWZT9mAsW3O4fAAX66..."
password_expire_max: 365
password_expire_min: 7
password_expire_warn: 15
#expires: 1746057600
state: present
- name: Delete the user
ansible.builtin.user:
name: redhat
state: absent
remove: true
password: "{{ 'redhat' | password_hash('sha512') }}"
会导致模块不具有幂等性,因为每次生成的加密内容都不同,可以通过openssl passwd -6 -salt randomsalt redhat
设置一个固定的加密密码以满足幂等性。SSH 密钥也可以用
ansible.builtin.authorized_key
来设置.
设置 SSH 密钥
ansible.builtin.authorized_key
用于 SSH 密钥管理。
参数名 | 类型 | 默认值 | 说明 |
comment | str | null | 添加到 key 后的注释(在 |
exclusive | bool | false | 若为 |
follow | bool | false | 是否跟随符号链接到 |
key | str | null | 要添加到用户 authorized_keys 中的公钥内容(通常是 |
key_options | str | null | 为 key 添加 OpenSSH 限制(如 |
manage_dir | bool | true | 是否自动创建 |
path | path | ~/.ssh/authorized_keys | 手动指定 |
state | str | present | 设置为 |
user | str | null | 指定哪个用户的公钥要被修改 |
validate_certs | bool | true | 若 |
- name: set ssh key
ansible.posix.authorized_key:
user: redhat
state: present
key: "ssh-rsa AAAAB3NzaC1yc2EAAAADA...