我正在努力正确使用ansible的用户模块。问题是,每次我运行剧本时,即使我已经创建了它们,我创建的用户总是都显示为已更改。
I found other people with the same issue here,尽管我正在努力根据github线程进行修复。可能是我听不懂的最有用的评论?
我可以确认它只是一个bug-添加附加 两项任务的选择使其无法总是撤消 工作,并修复了永久更改的触发器。我做了 无需添加“组:”
这是我的剧本的样子:
- name: Generate all users for the environment
user:
createhome: yes
state: present # to delete
name: "{{ item.user }}"
groups: "{{ 'developers' if item.role == 'developer' else 'customers' }}"
password: "{{ generic_password | password_hash('sha512') }}"
append: yes
with_items:
- "{{ users }}"
我的意图是让每个用户都属于他们自己的私有组(User Private Groups),同时让一个开发人员也属于开发人员组。 当前使用当前配置,并且由于问题而无法回答,因此始终将用户报告为“已更改” 。然后,我将developers
组添加到sudoers文件中;因此,我想将用户添加到developers
组中。
例如
vagrant@ubuntu-bionic:/home$ sudo su - nick
$ pwd
/home/nick
$ touch file.txt
$ ls -al
-rw-rw-r-- 1 nick nick 0 Jul 3 12:06 file.txt
vagrant@ubuntu-bionic:/home$ cat /etc/group | grep 'developers'
developers:x:1002:nick,ldnelson,greg,alex,scott,jupyter
以下是其中一个用户针对流浪汉在本地运行的详细输出:
changed: [192.168.33.10] => (item={'user': 'nick', 'role': 'developer', 'with_ga': False}) => {
"append": true,
"changed": true,
"comment": "",
"group": 1004,
"groups": "developers",
"home": "/home/nick",
"invocation": {
"module_args": {
"append": true,
"comment": null,
"create_home": true,
"createhome": true,
"expires": null,
"force": false,
"generate_ssh_key": null,
"group": null,
"groups": [
"developers"
],
"hidden": null,
"home": null,
"local": null,
"login_class": null,
"move_home": false,
"name": "nick",
"non_unique": false,
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"password_lock": null,
"remove": false,
"seuser": null,
"shell": null,
"skeleton": null,
"ssh_key_bits": 0,
"ssh_key_comment": "ansible-generated on ubuntu-bionic",
"ssh_key_file": null,
"ssh_key_passphrase": null,
"ssh_key_type": "rsa",
"state": "present",
"system": false,
"uid": null,
"update_password": "always"
}
},
"item": {
"role": "developer",
"user": "nick",
"with_ga": false
},
"move_home": false,
"name": "nick",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/sh",
"state": "present",
"uid": 1002
}
应该无关,但是我打算向开发人员组添加一些内容,因为我打算授予某些命令的sudo访问权限。
答案 0 :(得分:3)
generic_password | password_hash('sha512')
不是幂等的。每次函数 password_hash 运行时,哈希值的变化都会改变。
要使其具有幂等性,请使用特定的盐运行它
- name: Generate all users for the environment
user:
password: "{{ generic_password | password_hash('sha512', 'mysecretsalt') }}"
,或仅更新密码 on_create
- name: Generate all users for the environment
user:
update_password: on_create
(或注册return values并声明changed_when)。