如何通过Ansible延长用户的账户到期日期?

问题描述 投票:0回答:1

我对 Ansible 很陌生(还在学习中)。

我们有一个脚本来创建新用户,提供 sudo 访问权限,定义首次登录时要更改的密码、用户详细信息、帐户到期日期,然后定义密码到期信息:

---
- name: Create user with key
  hosts: all
  user: ansible
  sudo: True

  vars:

    password: $6$VhuG16emtCgF0zrK$dzE8swYNnLQeUkdgcgpTqr0stY8enepxcske8ATLAERjORpKkqYcGD6r6ssTNe4EdwwRs2UjjiAM/8tWCeEnN/
    raw: Password@1

  tasks:

  - name: Create New User
    user: name=TestUser comment="Test User" createhome=yes groups=wheel state=present password={{ password }}
  - command: chage -E 2023-10-18 TestUser
  - command: chage -d 0 TestUser
  - command: chage -M 120 TestUser
    register: newuser

我们将使用

-i
在 playbook 命令中定义主机,然后是主机文件的名称(其中包含将在其上创建用户帐户的机器的 IP),例如:

ansible-playbook create-newuser1.yml -i hosts.servers

目前要扩展用户帐户,我们必须去每个主机并在本地扩展帐户:

sudo chage -E 2023-12-28 TestUser

对于 Ansible 中的“专家”来说,这听起来像是一个直截了当的问题,但我无法找到有关如何使用上面类似脚本的信息(我尝试调整上面的脚本但失败了)来延长现有用户帐户的到期日期并在文件或 playbook 命令中定义主机。

尝试搜索

user
模块,找不到任何东西。还快速浏览了Managing user accounts using Ansible playbooks

仍在挣扎。

更新 1

刚灵机一动,我只需要创建一个 yml 文件(命名为:

extend-TestUser.yml
)说明:

- command: chage -E 2023-12-28 TestUser

然后陈述这个命令:

ansible-playbook extend-TestUser.yml -i hosts.servers
bash shell ansible redhat
1个回答
1
投票

您可以查看文档

user
模块——管理用户帐户和一个最小的示例剧本

---
- hosts: test
  become: true
  gather_facts: false

  vars:

    PASSWORD: 'Password@1'

  tasks:

  - name: Gather date and time only
    setup:
      gather_subset:
        - "date_time"
        - "!min"
    when: ansible_date_time is not defined

  - name: Configure or update user
    user:
      name: "TestUser"
      password: "{{ PASSWORD | password_hash('sha512') }}"
      state: present
      expires: "{{ '%s' | strftime( ( ansible_date_time.epoch | int ) + ( 86400 * 120 )  ) }}"

它显示了如何将到期日期从当前日期延长 120 天或设置新密码。

感谢

© www.soinside.com 2019 - 2024. All rights reserved.