Ansible 创建用户和组

标签 ansible

我正在尝试使用 Ansible 剧本创建新用户和组。下面是我的文件夹结构。

tree
.
├── create-users.yaml
└── ubuntu

create-users.yaml 剧本包含创建用户和组任务。请注意,我的目标计算机中没有任何组 (admin_group) 和用户(Rajini、Kamal),而是在运行剧本时创建它们。

---
- name:  Create Users & Groups
  hosts: target1
  gather_facts: false
  tasks:
    - name: Create Users Task
      user:
        name: "{{ item }}"
        state: present
        password: "{{ 'default_user_password' | password_hash('sha512','A512') }}"
        shell: /bin/bash
        groups: "{{ admin_group }}"
      loop:
        - Rajini
        - Kamal

我有另一个名为 ubuntu 的文件来选择组名和密码。运行剧本时出现以下错误。

ansible-playbook --vault-id @prompt create-users.yaml -K
BECOME password:
Vault password (default):

PLAY [Create Users & Groups] *****************************************************************************************************************************************************************

TASK [Create Users Task] *********************************************************************************************************************************************************************
fatal: [target1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'admin_group' is undefined\n\nThe error appears to be in '/home/osboxes/Ansible_Project/web_deployment/Ansible/groups_vars/create-users.yaml': line 6, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n    - name: Create Users Task\n      ^ here\n"}

PLAY RECAP ***********************************************************************************************************************************************************************************
target1                    : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

admin_group: admin
default_user_password: Password1

有人可以帮我解决这个问题吗?

在获得用户 Moon 的帮助后更新输出。

ansible-playbook --vault-id @prompt create-users.yaml -K
BECOME password:
Vault password (default):

PLAY [Create Users & Groups] *****************************************************************************************************************************************************************

TASK [Create Users Task] *********************************************************************************************************************************************************************
changed: [target1] => (item=Rajini)
changed: [target1] => (item=Kamal)

PLAY RECAP ***********************************************************************************************************************************************************************************
target1                    : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

ssh Kamal@192.168.0.1
Kamal@192.168.0.1's password:
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 5.0.0-23-generic x86_64)
Kamal@Ansible_Target1:~$ id
uid=1005(Kamal) gid=1001(admin) groups=1001(admin)

最佳答案

一些事情:

  • 要使用 ubuntu 文件中的变量,您需要在 playbook 中指定 vars 文件。
  • 要将 default_user_password 用作变量,请删除引号 '
  • 如果您希望admin 作为用户的主要组,那么请使用group 属性。 groups 另一方面获取列表并将用户添加到列出的组。

并且,如果该组尚未在目标计算机上创建,则首先使用 group module 创建该组.

上述更改后的剧本。

---
- name: Create Users & Groups
  hosts: target1
  gather_facts: false
  vars_files: ubuntu
  tasks:
    - name: Create group
      group:
        name: "{{ admin_group }}"
        state: present

    - name: Create Users Task
      user:
        name: "{{ item }}"
        state: present
        password: "{{ default_user_password | password_hash('sha512','A512') }}"
        shell: /bin/bash
        group: "{{ admin_group }}"
      loop:
        - Rajini
        - Kamal

关于Ansible 创建用户和组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62358569/

相关文章:

linux - Ansible 任务写入本地日志文件

tomcat - 如何使用ansible运行Tomcat

Ansible:更改 .yml 文件中目录内文件的权限

jinja2 - 模板中的 Ansible 全局变量

ansible - 如何避免由于 dpkg 锁定文件导致的 ansible 部署失败?

vagrant - Ansible apt-get 安装输出

ansible - 您如何在Ansible Tower中同时执行一项以上的工作?

ansible - 如何下载和安装ansible模块?

kubernetes - 如何在不使用引号的情况下使用 k8s Ansible 模块?

linux - 使用 Ansible 获取 Linux 网络接口(interface)名称?