[root@Tang ~]# ssh-keygen -t rsa -P"" # 先创建密钥
Generating public/private rsa key pair.
Enter file in which to save the key(/root/.ssh/id_rsa):
Your identification has been saved in/root/.ssh/id_rsa.
Your public key has been saved in/root/.ssh/id_rsa.pub.
The key fingerprint is:SHA256:YGLI/OM6g0BRdDgHaVcCcKTOg6t2nxDhZ9CWmCVipw0 root@Tang
The key's randomart image is:+---[RSA2048]----+|E=X=+..||.+O*Boo || o***+o ||+..o+o .||o+ ooo S||....+.||o...||o.oo..||o oo.o |+----[SHA256]-----+
2.3 添加主机 Tang-1(172.16.141.209),并进行登陆验证
[root@Tang ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.16.141.209 # 添加无密码认证登陆密钥
/usr/bin/ssh-copy-id:INFO: Source ofkey(s) to be installed:"/root/.ssh/id_rsa.pub"/usr/bin/ssh-copy-id:INFO: attempting to log inwith the newkey(s), to filter out any that are already installed
/usr/bin/ssh-copy-id:INFO:1key(s) remain to be installed --if you are prompted now it is to install the newkeys
root@172.16.141.209's password:
Number ofkey(s) added:1
Now try logging into the machine,with:"ssh 'root@172.16.141.209'"
and check to make sure that only the key(s) you wanted were added.[root@Tang ~]# ssh root@172.16.141.209 # 验证无密码登陆
Last login: Wed Jan 109:09:382014from172.16.141.252[root@Tang-1~]# ip addr list | grep enp1s0 # 可以成功登陆
2: enp1s0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
inet 172.16.141.209/24 brd 172.16.141.255 scope global noprefixroute enp1s0
2.4 添加主机 Tang-2(172.16.141.253),并进行登陆验证
[root@Tang ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.16.141.253/usr/bin/ssh-copy-id:INFO: Source ofkey(s) to be installed:"/root/.ssh/id_rsa.pub"/usr/bin/ssh-copy-id:INFO: attempting to log inwith the newkey(s), to filter out any that are already installed
/usr/bin/ssh-copy-id:INFO:1key(s) remain to be installed --if you are prompted now it is to install the newkeys
root@172.16.141.253's password:
Number ofkey(s) added:1
Now try logging into the machine,with:"ssh 'root@172.16.141.253'"
and check to make sure that only the key(s) you wanted were added.[root@Tang ~]# ssh root@172.16.141.253
Last login: Wed Oct 1614:15:452019from172.16.141.99[root@Tang-2~]# ifconfig enp1s0
enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.141.253 netmask 255.255.255.0 broadcast 172.16.141.255
2.5 进行主机列表查看和 ping 测试
[root@Tang ~]# ansible all --list-hosts
hosts(2):172.16.141.209172.16.141.253
[root@Tang ~]# ansible-doc -s group # 查看 group 模块的帮助
- name: Add or remove groups
group:
gid: # Optional `GID' to set for the group.
local: # Forces the use of "local" command alternatives on platforms that implement it. This is useful in
environments that use centralized authentication when you want to
manipulate the local groups. (e.g. it uses `lgroupadd' instead of`groupadd'). This requires that these commands exist on the targeted
host, otherwise it will be a fatal error.
name: # (required) Name of the group to manage.
non_unique: # This option allows to change the group ID to a non-unique value. Requires `gid'. Not supported on
macOS or BusyBox distributions.
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.[root@Tang ~]# ansible all -m group -a "gid=3000 name=mygrp state=present system=no" # 创建组
172.16.141.253|CHANGED=>{
"ansible_facts":{
"discovered_interpreter_python":"/usr/bin/python"},"changed":true,"gid":3000,"name":"mygrp","state":"present","system":false}172.16.141.209|CHANGED=>{
"ansible_facts":{
"discovered_interpreter_python":"/usr/bin/python"},"changed":true,"gid":3000,"name":"mygrp","state":"present","system":false}
4.1.2 进行验证并查看 LOG
[root@Tang-1~]# tail -1/etc/group
mygrp:x:3000:[root@Tang-1~]# tail -5/var/log/messages
Jan 110:05:17 Tang systemd-logind: Removed session 9.
Jan 110:08:30 Tang systemd-logind: New session10of user root.
Jan 110:08:30 Tang systemd: Started Session 10of user root.
Jan 110:08:31 Tang ansible-group: Invoked with non_unique=False name=mygrp system=False state=present gid=3000 local=False
Jan 110:09:31 Tang systemd-logind: Removed session 10.
[root@Tang-2~]# tail -1/etc/group
mygrp:x:3000:[root@Tang-2~]# tail -5/var/log/messages
Oct 1614:26:08 Tang-2 systemd: Started Cleanup of Temporary Directories.
Oct 1614:27:30 Tang-2 systemd-logind: New session5of user root.
Oct 1614:27:30 Tang-2 systemd: Started Session 5of user root.
Oct 1614:27:31 Tang-2 ansible-group: Invoked with non_unique=False name=mygrp system=False state=present gid=3000 local=False
Oct 1614:28:31 Tang-2 systemd-logind: Removed session 5.
4.1.3 删除组
[root@Tang ~]# ansible all -m group -a "gid=3000 name=mygrp state=absent" # 删除组
4.2 User
4.2.1 查看帮助和创建用户
[root@Tang ~]# ansible-doc -s user
[root@Tang ~]# ansible all -m user -a "uid=5000 name=testuser state=present groups=mygrp shell=/bin/tcsh" # 创建用户
172.16.141.253|CHANGED=>{
"ansible_facts":{
"discovered_interpreter_python":"/usr/bin/python"},"changed":true,"comment":"","create_home":true,"group":5000,"groups":"mygrp","home":"/home/testuser","name":"testuser","shell":"/bin/tcsh","state":"present","system":false,"uid":5000}172.16.141.209|CHANGED=>{
"ansible_facts":{
"discovered_interpreter_python":"/usr/bin/python"},"changed":true,"comment":"","create_home":true,"group":5000,"groups":"mygrp","home":"/home/testuser","name":"testuser","shell":"/bin/tcsh","state":"present","system":false,"uid":5000}
4.2.2 进行验证
[root@Tang-1~]# tail -1/etc/passwd
testuser:x:5000:5000::/home/testuser:/bin/tcsh
[root@Tang-1~]# id testuser
uid=5000(testuser) gid=5000(testuser) groups=5000(testuser),3000(mygrp)[root@Tang-1~]# tail -5/var/log/messages
Jan 110:09:31 Tang systemd-logind: Removed session 10.
Jan 110:13:21 Tang systemd-logind: New session11of user root.
Jan 110:13:21 Tang systemd: Started Session 11of user root.
Jan 110:13:22 Tang ansible-user: Invoked with comment=None ssh_key_bits=0 update_password=always ssh_key_passphrase=NOT_LOGGING_PARAMETER force=False uid=5000 create_home=True password_lock=None non_unique=False home=None skeleton=None append=False ssh_key_type=rsa ssh_key_comment=ansible-generated on Tang-1 group=None system=False state=present role=None hidden=None local=None authorization=None profile=None shell=/bin/tcsh expires=None ssh_key_file=None groups=['mygrp'] move_home=False password=NOT_LOGGING_PARAMETER name=testuser seuser=None remove=False login_class=None generate_ssh_key=None
Jan 110:14:22 Tang systemd-logind: Removed session 11.
[root@Tang-2~]# tail -1/etc/passwd
testuser:x:5000:5000::/home/testuser:/bin/tcsh
[root@Tang-2~]# id testuser
uid=5000(testuser) gid=5000(testuser) groups=5000(testuser),3000(mygrp)[root@Tang-2~]# tail -5/var/log/messages
Oct 1614:28:31 Tang-2 systemd-logind: Removed session 5.
Oct 1614:32:21 Tang-2 systemd-logind: New session6of user root.
Oct 1614:32:21 Tang-2 systemd: Started Session 6of user root.
Oct 1614:32:22 Tang-2 ansible-user: Invoked with comment=None ssh_key_bits=0 update_password=always ssh_key_passphrase=NOT_LOGGING_PARAMETER force=False uid=5000 create_home=True password_lock=None non_unique=False home=None skeleton=None append=False ssh_key_type=rsa ssh_key_comment=ansible-generated on Tang-2 group=None system=False state=present role=None hidden=None local=None authorization=None profile=None shell=/bin/tcsh expires=None ssh_key_file=None groups=['mygrp'] move_home=False password=NOT_LOGGING_PARAMETER name=testuser seuser=None remove=False login_class=None generate_ssh_key=None
Oct 1614:33:23 Tang-2 systemd-logind: Removed session 6.