共计 6232 个字符,预计需要花费 16 分钟才能阅读完成。
Ansible提供了user模块,这样的话在其他服务器上可以使用user模块创建用户了。。
用法
[root@ansible ~]# ansible-doc -s user
less 436
.........................
- name: Manage user accounts
action: user
append # If `yes‘, will only add groups, not set them to just the list in `groups‘.
comment # Optionally sets the description (aka `GECOS‘) of user account.
createhome # Unless set to `no‘, a home directory will be made for the user when the account is created or if
the home directory does not exist.
expires # An expiry time for the user in epoch, it will be ignored on platforms that do not support this.
Currently supported on Linux and FreeBSD.
force # When used with `state=absent‘, behavior is as with `userdel --force‘.
generate_ssh_key # Whether to generate a SSH key for the user in question. This will *not* overwrite an existing SSH
key.
group # Optionally sets the user‘s primary group (takes a group name).
groups # Puts the user in this comma-delimited list of groups. When set to the empty string (‘groups=‘),
the user is removed from all groups except the primary group.
home # Optionally set the user‘s home directory.
login_class # Optionally sets the user‘s login class for FreeBSD, OpenBSD and NetBSD systems.
move_home # If set to `yes‘ when used with `home=‘, attempt to move the user‘s home directory to the
specified directory if it isn‘t there already.
name= # Name of the user to create, remove or modify.
non_unique # Optionally when used with the -u option, this option allows to change the user ID to a non-unique
value.
password # Optionally set the user‘s password to this crypted value. See the user example in the github
examples directory for what this looks like in a playbook. See
http://docs.ansible.com/ansible/faq.html#how-do-i-generate-
crypted-passwords-for-the-user-module for details on various ways
to generate these password values. Note on Darwin system, this
value has to be cleartext. Beware of security issues.
remove # When used with `state=absent‘, behavior is as with `userdel --remove‘.
shell # Optionally set the user‘s shell.
skeleton # Optionally set a home skeleton directory. Requires createhome option!
ssh_key_bits # Optionally specify number of bits in SSH key to create.
ssh_key_comment # Optionally define the comment for the SSH key.
ssh_key_file # Optionally specify the SSH key filename. If this is a relative filename then it will be relative
to the user‘s home directory.
ssh_key_passphrase # Set a passphrase for the SSH key. If no passphrase is provided, the SSH key will default to
having no passphrase.
ssh_key_type # Optionally specify the type of SSH key to generate. Available SSH key types will depend on
implementation present on target host.
state # Whether the account should exist or not, taking action if the state is different from what is
stated.
system # When creating an account, setting this to `yes‘ makes the user a system account. This setting
cannot be changed on existing users.
uid # Optionally sets the `UID‘ of the user.
update_password # `always‘ will update passwords if they differ. `on_create‘ will only set the password for newly
created users.
需要特别说明的是,password后面指定的密码不能是明文,后面这一串密码会被直接传送到被管理主机的/etc/shadow文件中,而登陆的时候输入的密码会被hash加密以后再去与/etc/shadow中存放的密码去做对比,会出现不一致的现象。所以需要先将密码字符串进行加密处理:openssl passwd -salt -1 “123456”,然后将得到的字符串放到password中即可。
示例:
在web组机器上创建一个test用户,密码为123456,家目录为/home/test下,uid位678,comment注释为this is a ansible test user,shell为/bin/sh等等
[root@ansible ~]# openssl passwd -salt -1 "123456"
-1DhUWqz2JZqc
[root@ansible ~]# ansible webser -m user -a "name=test password=1DhUWqz2JZqc home=/home/test uid=678 comment=‘this is a ansible test user‘ shell=/bin/sh"
webser | SUCCESS => {
"changed": true,
"comment": "this is a ansible test user",
"createhome": true,
"group": 678,
"home": "/home/test",
"name": "haha",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/sh",
"state": "present",
"stderr": "useradd: warning: the home directory already exists.\nNot copying any file from skel directory into it.\n",
"system": false,
"uid": 678
}
验证创建的用户
[root@ansible ~]# ansible webser -m raw -a ‘tail /etc/passwd|grep test‘
webser | SUCCESS | rc=0 >>
test:x:678:678:this is a ansible test user:/home/test:/bin/sh
group组模块
用法
[root@ansible ~]# ansible-doc -s group
less 436
Copyright (C) 1984-2009 Mark Nudelman
less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less
- name: Add or remove groups
action: group
gid # Optional `GID‘ to set for the group.
name= # Name of the group to manage.
state # Whether the group should be present or not on the remote host.
system # If `yes‘, indicates that the group created is a system group.
创建一个组为g1,gid为666 state状态在远程主机上要么创建(present)要么删除absent,system是否为系统用,有些选择都是可选的
[root@ansible ~]# ansible webser -m group -a ‘name=g1 gid=666 state=present system=yes‘
web | SUCCESS => {
"changed": true,
"gid": 666,
"name": "g1",
"state": "present",
"system": true
}
验证下创建的组g1
[root@ansible ~]# ansible web -m raw -a ‘tail /etc/group|grep g1‘
web | SUCCESS | rc=0 >>
g1:x:666:
正文完