8.16-ansible的应用
ansible
ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。
格式
ansible 主机ip|域名|组名|别名 -m ping|copy|... '参数'
1.ping模块
m0
# 查看有没有安装epel [root@m0 ~]# yum list installed|grep epel epel-release.noarch 7-11 @extras # 安装ansible [root@m0 ~]# yum -y install ansible # 查看ansible的版本 [root@m0 ~]# ansible --version ansible 2.9.27config file = /etc/ansible/ansible.cfgconfigured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python2.7/site-packages/ansibleexecutable location = /usr/bin/ansiblepython version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] # 查找ansible的配置文件 [root@m0 ~]# find /etc/ -name "*ansible*" /etc/ansible /etc/ansible/ansible.cfg # 设置免密 [root@m0 ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: 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:OvpbtUtLSVvtPtcexSHBThXhhzywDBB8pzGCbwqSoMk root@m0 The key's randomart image is: +---[RSA 2048]----+ | ooo. o..+o| |. . o +o.*o. | |oo . . o ==.+o.| |oEo . o . o.oo| | . . oS o . . o| | .. o = . .| | o . * ...| | . o o o .. +| | ..o. o .+.| +----[SHA256]-----+ [root@m0 ~]# ls ./.ssh/ id_rsa id_rsa.pub # 给s0和s1设置免密登录 [root@m0 ~]# ssh-copy-id -i 192.168.2.110(s0) [root@m0 ~]# ssh-copy-id -i 192.168.2.111(s1) [root@m0 ~]# vim /etc/ansible/hosts # 110和111都在m0上设置了免密登录 [group01] 192.168.2.110 192.168.2.111 # 112主机没有设置免密登录 [group02] 192.168.2.110 192.168.2.111 192.168.2.112
# ping 9igroup01的第一个ip
[root@m0 ~]# ansible 192.168.2.110 -m ping
192.168.2.110 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"
}
# ping group01
[root@m0 ~]# ansible group01 -m ping
192.168.2.110 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"
}
192.168.2.111 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"
}
# ping group02(第三台没做免密,ping的时候会报错)
[root@m0 ~]# ansible group02 -m ping
The authenticity of host '192.168.2.112 (192.168.2.112)' can't be established.
ECDSA key fingerprint is SHA256:E2ARFFif/HyOpjlCgDRoPqYSl2OL4PwdcX1h9cPRJiY.
ECDSA key fingerprint is MD5:35:b0:cd:3b:e0:fa:10:4a:22:5e:94:aa:b7:5c:e2:79.
Are you sure you want to continue connecting (yes/no)? 192.168.2.110 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"
}
192.168.2.111 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"
}
yes
192.168.2.112 | UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '192.168.2.112' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", "unreachable": true
}
# 解决没做免密的ip报错
# 给没有设置免密登录的ip设置账号,密码
[root@m0 ~]# vim /etc/ansible/hosts
other ansible_ssh_host=192.168.2.112 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=1
[group02]
192.168.2.110
192.168.2.111
other
# 进行测试(不会报错)
[root@m0 ~]# ansible group02 -m ping
192.168.2.110 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"
}
192.168.2.111 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"
}
other | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"
}
# 也可以单独ping other模块
[root@m0 ~]# ansible other -m ping
other | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"
}
小结
主机清单的作用:服务器分组
主机清单的常见功能:
1.可以通过IP范围来分,主机名名字的范围来分
2.如果ssh端口不是22的,可以传入新的端口
3.没有做免密登录,可以传密码
练习
不论你用到哪种环境(免密或者不免密,端口是否是22),请最终将两台被管理机器加入到group1组即可
# 没有设置免密的话需要设置别名 web01 ansible_ssh_host=192.168.2.200 ansible_ssh_user=root ansible_ssh_pass=1 ansible_ssh_port=22 web01 ansible_ssh_host=192.168.2.201 ansible_ssh_user=root ansible_ssh_pass=1 ansible_ssh_port=22 [group1] web01 web02
帮助手册
# 查看ansible的用法 [root@m0 ~]# ansible-doc -l # 查看ping在ansible中的用法 [root@m0 ~]# ansible-doc -l ping
2.hostname模块
# 将group02组中主机的主机名都改成ab.haha
[root@m0 ~]# ansible group02 -m hostname -a 'name=ab.haha'
192.168.2.111 | CHANGED => {"ansible_facts": {"ansible_domain": "haha", "ansible_fqdn": "ab.haha", "ansible_hostname": "ab", "ansible_nodename": "ab.haha", "discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "name": "ab.haha"
}
other | CHANGED => {"ansible_facts": {"ansible_domain": "haha", "ansible_fqdn": "ab.haha", "ansible_hostname": "ab", "ansible_nodename": "ab.haha", "discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "name": "ab.haha"
}
192.168.2.110 | CHANGED => {"ansible_facts": {"ansible_domain": "haha", "ansible_fqdn": "ab.haha", "ansible_hostname": "ab", "ansible_nodename": "ab.haha", "discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "name": "ab.haha"
}
# 验证
[root@s0 ~]# hostname
ab.haha
[root@s1 ~]# hostname
ab.haha
[root@s2 ~]# hostname
ab.haha
3.file模块
创建目录
# 在group01组中的主机(包含other(没有设置免密的那台主机))中的/tmp/中创建abc文件
# -m 表示调用模块
# state=directory 表示当前的状态被设置为“目录”
[root@m0 ~]# ansible group01 -m file -a 'path=/tmp/abc state=directory'
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/abc", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 6, "state": "directory", "uid": 0
}
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/abc", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 6, "state": "directory", "uid": 0
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/abc", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 6, "state": "directory", "uid": 0
}
[root@s0 ~]# ls -l /tmp/
总用量 4
drwxr-xr-x. 2 root root 6 8月 16 11:43 abc
[root@s1 ~]# ls -l /tmp/
总用量 4
drwxr-xr-x. 2 root root 6 8月 16 11:43 abc
[root@s2 ~]# ls -l /tmp/
总用量 4
drwxr-xr-x. 2 root root 6 8月 16 11:43 abc
创建文件
# 给group02组中的主机中的/tmp/abc/中创建def文件
[root@m0 ~]# ansible group02 -m file -a 'path=/tmp/abc/def state=touch'
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "dest": "/tmp/abc/def", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 0
}
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "dest": "/tmp/abc/def", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 0
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "dest": "/tmp/abc/def", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 0
}
[root@s1 ~]# ll /tmp/abc
总用量 0
-rwxrwxrwt. 1 bin daemon 0 8月 16 14:09 def
递归修改
[root@m0 ~]# ansible group02 -m file -a 'path=/tmp/abc recurse=yes owner=bin group=daemon mode=1777'(属主,属组,权限)
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "gid": 2, "group": "daemon", "mode": "01777", "owner": "bin", "path": "/tmp/abc", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 17, "state": "directory", "uid": 1
}
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "gid": 2, "group": "daemon", "mode": "01777", "owner": "bin", "path": "/tmp/abc", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 17, "state": "directory", "uid": 1
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "gid": 2, "group": "daemon", "mode": "01777", "owner": "bin", "path": "/tmp/abc", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 17, "state": "directory", "uid": 1
}
递归修改验证

删除
[root@m0 ~]# ansible group02 -m file -a 'path=/tmp/abc state=absent'
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "path": "/tmp/abc", "state": "absent"
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "path": "/tmp/abc", "state": "absent"
}
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "path": "/tmp/abc", "state": "absent"
}
# 验证
# 删除之前
[root@s1 ~]# ll /tmp/abc
总用量 0
-rwxrwxrwt. 1 bin daemon 0 8月 16 14:09 def
# 删除之后
[root@s1 ~]# ll /tmp/abc
ls: 无法访问/tmp/abc: 没有那个文件或目录
创建且指定权限的文件
# 创建文件/tmp/aaa,修改属主为bin,属组为daemon,权限为777
[root@m0 ~]# ansible group02 -m file -a 'path=/tmp/aaa state=touch owner=bin group=daemon mode=1777'
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "dest": "/tmp/aaa", "gid": 2, "group": "daemon", "mode": "01777", "owner": "bin", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 1
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "dest": "/tmp/aaa", "gid": 2, "group": "daemon", "mode": "01777", "owner": "bin", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 1
}
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "dest": "/tmp/aaa", "gid": 2, "group": "daemon", "mode": "01777", "owner": "bin", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 1
}
[root@s1 ~]# ls -l /tmp/
总用量 4
-rwxrwxrwt. 1 bin daemon 0 8月 16 14:23 aaa
删除文件
# 删除属主为bin,属组为daemon,权限为777且在/tmp/下文件名为aaa的文件
[root@m0 ~]# ansible group02 -m file -a 'path=/tmp/aaa state=absent owner=bin group=daemon mode=1777'
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "path": "/tmp/aaa", "state": "absent"
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "path": "/tmp/aaa", "state": "absent"
}
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "path": "/tmp/aaa", "state": "absent"
}
[root@s1 ~]# ls -l /tmp/aaa
ls: 无法访问/tmp/aaa: 没有那个文件或目录
创建mysql-files文件,并修改权限
[root@m0 ~]# ansible group02 -m file -a 'path=/usr/local/mysql/mysql-files state=directory owner=mysql group=mysql mode=750'
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "gid": 995, "group": "mysql", "mode": "0750", "owner": "mysql", "path": "/usr/local/mysql/mysql-files", "secontext": "unconfined_u:object_r:usr_t:s0", "size": 6, "state": "directory", "uid": 997
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "gid": 995, "group": "mysql", "mode": "0750", "owner": "mysql", "path": "/usr/local/mysql/mysql-files", "secontext": "unconfined_u:object_r:usr_t:s0", "size": 6, "state": "directory", "uid": 997
}
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "gid": 995, "group": "mysql", "mode": "0750", "owner": "mysql", "path": "/usr/local/mysql/mysql-files", "secontext": "unconfined_u:object_r:usr_t:s0", "size": 6, "state": "directory", "uid": 997
}
# 验证
[root@ab ~]# ll /usr/local/mysql/
总用量 0
drwxr-x---. 2 mysql mysql 6 8月 16 21:38 mysql-files
创建软连接(软连接指向硬链接)
# 创建软连接(软连接指向硬链接)
[root@m0 ~]# ansible group02 -m file -a 'src=/etc/fstab path=/tmp/xxx state=link'
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "dest": "/tmp/xxx", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 10, "src": "/etc/fstab", "state": "link", "uid": 0
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "dest": "/tmp/xxx", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 10, "src": "/etc/fstab", "state": "link", "uid": 0
}
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "dest": "/tmp/xxx", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 10, "src": "/etc/fstab", "state": "link", "uid": 0
}
[root@s1 ~]# ll /tmp/
总用量 8
-rwx------. 1 root root 836 8月 7 00:25 ks-script-pjA4To
drwx------. 3 root root 17 8月 16 10:25 systemd-private-bbe4eb529aa243da930a7edebcedf30b-chronyd.service-Er6p6N
drwx------. 3 root root 17 8月 6 17:35 systemd-private-f93e9a7cc83a4e6ba1ea5a4ff1abcdc2-chronyd.service-2U7zsa
drwx------. 2 root root 6 8月 7 00:25 vmware-root
lrwxrwxrwx. 1 root root 10 8月 16 14:32 xxx -> /etc/fstab
创建硬链接(指向文件)
# 硬链接(指向文件)
[root@m0 ~]# ansible group02 -m file -a 'src=/etc/fstab path=/tmp/xxx2 state=hard'
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "dest": "/tmp/xxx2", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:etc_t:s0", "size": 502, "src": "/etc/fstab", "state": "hard", "uid": 0
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "dest": "/tmp/xxx2", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:etc_t:s0", "size": 502, "src": "/etc/fstab", "state": "hard", "uid": 0
}
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "dest": "/tmp/xxx2", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:etc_t:s0", "size": 502, "src": "/etc/fstab", "state": "hard", "uid": 0
}
[root@s1 ~]# ll /tmp/
总用量 8
-rwx------. 1 root root 836 8月 7 00:25 ks-script-pjA4To
drwx------. 3 root root 17 8月 16 10:25 systemd-private-bbe4eb529aa243da930a7edebcedf30b-chronyd.service-Er6p6N
drwx------. 3 root root 17 8月 6 17:35 systemd-private-f93e9a7cc83a4e6ba1ea5a4ff1abcdc2-chronyd.service-2U7zsa
drwx------. 2 root root 6 8月 7 00:25 vmware-root
lrwxrwxrwx. 1 root root 10 8月 16 14:32 xxx -> /etc/fstab
-rw-r--r--. 2 root root 502 8月 6 16:33 xxx2
-rw-------. 1 root root 0 8月 7 00:21 yum.log
小结
ansible group02 -m file 'path= state= recurse= src= owner= group= mode' # path 文件的地址 # state 方法# directory 创建目录# touch 创建文件# absent 删除文件# link 创建软连接# hard 创建硬链接 # recurse 是否允许递归操作 # src 文件源
4.stat模块
查看信息
[root@m0 ~]# ansible group02 -m stat -a 'path=/etc/fstab'
192.168.2.111 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "stat": {"atime": 1723775545.4809103, "attr_flags": "", "attributes": [], "block_size": 4096, "blocks": 8, "charset": "us-ascii", "checksum": "20fcca9572fbd2d112b32e6446e42070695ce511", "ctime": 1723790032.7200139, "dev": 64768, "device_type": 0, "executable": false, "exists": true, "gid": 0, "gr_name": "root", "inode": 17258899, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mimetype": "text/plain", "mode": "0644", "mtime": 1722933190.7942092, "nlink": 2, "path": "/etc/fstab", "pw_name": "root", "readable": true, "rgrp": true, "roth": true, "rusr": true, "size": 502, "uid": 0, "version": "18446744072287068007", "wgrp": false, "woth": false, "writeable": true, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}
}
192.168.2.110 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "stat": {"atime": 1723775466.468042, "attr_flags": "", "attributes": [], "block_size": 4096, "blocks": 8, "charset": "us-ascii", "checksum": "20fcca9572fbd2d112b32e6446e42070695ce511", "ctime": 1723790032.71537, "dev": 64768, "device_type": 0, "executable": false, "exists": true, "gid": 0, "gr_name": "root", "inode": 17258899, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mimetype": "text/plain", "mode": "0644", "mtime": 1722933190.7942092, "nlink": 2, "path": "/etc/fstab", "pw_name": "root", "readable": true, "rgrp": true, "roth": true, "rusr": true, "size": 502, "uid": 0, "version": "18446744072287068007", "wgrp": false, "woth": false, "writeable": true, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}
}
other | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "stat": {"atime": 1723775623.031594, "attr_flags": "", "attributes": [], "block_size": 4096, "blocks": 8, "charset": "us-ascii", "checksum": "20fcca9572fbd2d112b32e6446e42070695ce511", "ctime": 1723790032.729416, "dev": 64768, "device_type": 0, "executable": false, "exists": true, "gid": 0, "gr_name": "root", "inode": 17258899, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mimetype": "text/plain", "mode": "0644", "mtime": 1722933190.7942092, "nlink": 2, "path": "/etc/fstab", "pw_name": "root", "readable": true, "rgrp": true, "roth": true, "rusr": true, "size": 502, "uid": 0, "version": "18446744072287068007", "wgrp": false, "woth": false, "writeable": true, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}
}
5.copy模块(重点)
[root@m0 ~]# ls
anaconda-ks.cfg mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz
[root@m0 ~]# mv mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz mysql57.tar.gz
[root@m0 ~]# ls
anaconda-ks.cfg mysql57.tar.gz
# 把mysql57.tar.gz传到group02组中的主机中
[root@m0 ~]# ansible group02 -m copy -a 'src=./mysql57.tar.gz dest=~'
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "ca7c056f43922133ac4bfa788849172ff124ce47", "dest": "/root/mysql57.tar.gz", "gid": 0, "group": "root", "md5sum": "d7c8436bbf456e9a4398011a0c52bc40", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 694785800, "src": "/root/.ansible/tmp/ansible-tmp-1723791895.72-3029-205251780527035/source", "state": "file", "uid": 0
}
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "ca7c056f43922133ac4bfa788849172ff124ce47", "dest": "/root/mysql57.tar.gz", "gid": 0, "group": "root", "md5sum": "d7c8436bbf456e9a4398011a0c52bc40", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 694785800, "src": "/root/.ansible/tmp/ansible-tmp-1723791895.62-3027-254129236082512/source", "state": "file", "uid": 0
}
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "ca7c056f43922133ac4bfa788849172ff124ce47", "dest": "/root/mysql57.tar.gz", "gid": 0, "group": "root", "md5sum": "d7c8436bbf456e9a4398011a0c52bc40", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 694785800, "src": "/root/.ansible/tmp/ansible-tmp-1723791895.71-3026-134059210870560/source", "state": "file", "uid": 0
}
# 验证
[root@s0 ~]# ls
aaa anaconda-ks.cfg mysql57.tar.gz
[root@s1 ~]# ls
aaa anaconda-ks.cfg mysql57.tar.gz
[root@s2 ~]# ls
aaa anaconda-ks.cfg mysql57.tar.gz
练习:
创建一个100M的文件,然后同步到110,111,112主机上
[root@m0 ~]# dd if="/dev/zero" of="tst" bs=100M count=1
记录了1+0 的读入
记录了1+0 的写出
104857600字节(105 MB)已复制,0.113386 秒,925 MB/秒
[root@m0 ~]# ansible group02 -m copy -a 'src=./tst dest=~'
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "2c2ceccb5ec5574f791d45b63c940cff20550f9a", "dest": "/root/tst", "gid": 0, "group": "root", "md5sum": "2f282b84e7e608d5852449ed940bfc51", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 104857600, "src": "/root/.ansible/tmp/ansible-tmp-1723792459.28-3139-31521072234907/source", "state": "file", "uid": 0
}
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "2c2ceccb5ec5574f791d45b63c940cff20550f9a", "dest": "/root/tst", "gid": 0, "group": "root", "md5sum": "2f282b84e7e608d5852449ed940bfc51", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 104857600, "src": "/root/.ansible/tmp/ansible-tmp-1723792459.26-3138-81656713137079/source", "state": "file", "uid": 0
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "2c2ceccb5ec5574f791d45b63c940cff20550f9a", "dest": "/root/tst", "gid": 0, "group": "root", "md5sum": "2f282b84e7e608d5852449ed940bfc51", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 104857600, "src": "/root/.ansible/tmp/ansible-tmp-1723792459.29-3140-152165473863162/source", "state": "file", "uid": 0
}
[root@s0 ~]# ls
aaa anaconda-ks.cfg mysql57.tar.gz tst
[root@s1 ~]# ls
aaa anaconda-ks.cfg mysql57.tar.gz tst
[root@s2 ~]# ls
aaa anaconda-ks.cfg mysql57.tar.gz tst
给文件写入内容
给tst文件写入wo shi haha ,并且同步到110,111,112主机上
[root@m0 ~]# ansible group02 -m copy -a 'content="wo shi haha" dest=~/tst'
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "965c5185c9bb99125bfa8c7162dcf4b738f10a77", "dest": "/root/tst", "gid": 0, "group": "root", "md5sum": "3ee4b712d8af9f792d318c9f0a836759", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 11, "src": "/root/.ansible/tmp/ansible-tmp-1723792694.7-3249-240792052218088/source", "state": "file", "uid": 0
}
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "965c5185c9bb99125bfa8c7162dcf4b738f10a77", "dest": "/root/tst", "gid": 0, "group": "root", "md5sum": "3ee4b712d8af9f792d318c9f0a836759", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 11, "src": "/root/.ansible/tmp/ansible-tmp-1723792694.71-3248-228412683621377/source", "state": "file", "uid": 0
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "965c5185c9bb99125bfa8c7162dcf4b738f10a77", "dest": "/root/tst", "gid": 0, "group": "root", "md5sum": "3ee4b712d8af9f792d318c9f0a836759", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 11, "src": "/root/.ansible/tmp/ansible-tmp-1723792694.76-3250-198245487172331/source", "state": "file", "uid": 0
}
[root@s0 ~]# cat tst
wo shi haha
[root@s1 ~]# cat tst
wo shi haha
force=no(不覆盖)
如果ansible将创建文件的命令发布到110,111,112主机上时,这三台主机有这个文件,force=no,就不会覆盖这三台主机上的原来的文件
[root@m0 ~]# ansible group02 -m copy -a 'src=./tst dest=~ force=no'
192.168.2.110 | SUCCESS => {"changed": false, "dest": "/root", "src": "/root/./tst"
}
192.168.2.111 | SUCCESS => {"changed": false, "dest": "/root", "src": "/root/./tst"
}
other | SUCCESS => {"changed": false, "dest": "/root", "src": "/root/./tst"
}
# 验证
# 执行之前
[root@s0 ~]# ls -lh
总用量 663M
-rwxrwxrwt. 1 bin daemon 0 8月 16 14:22 aaa
-rw-------. 1 root root 1.3K 8月 7 00:25 anaconda-ks.cfg
-rw-r--r--. 1 root root 663M 8月 16 15:06 mysql57.tar.gz
-rw-r--r--. 1 root root 11 8月 16 15:18 tst
# 执行之后
[root@s0 ~]# ls -lh
总用量 663M
-rwxrwxrwt. 1 bin daemon 0 8月 16 14:22 aaa
-rw-------. 1 root root 1.3K 8月 7 00:25 anaconda-ks.cfg
-rw-r--r--. 1 root root 663M 8月 16 15:06 mysql57.tar.gz
-rw-r--r--. 1 root root 11 8月 16 15:18 tst
force=yes(覆盖)
force=yes,就会覆盖掉这三台主机上原来存在的tst文件
[root@m0 ~]# ansible group02 -m copy -a 'src=./tst dest=~ force=yes'
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "2c2ceccb5ec5574f791d45b63c940cff20550f9a", "dest": "/root/tst", "gid": 0, "group": "root", "md5sum": "2f282b84e7e608d5852449ed940bfc51", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 104857600, "src": "/root/.ansible/tmp/ansible-tmp-1723798717.23-3585-21726739880805/source", "state": "file", "uid": 0
}
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "2c2ceccb5ec5574f791d45b63c940cff20550f9a", "dest": "/root/tst", "gid": 0, "group": "root", "md5sum": "2f282b84e7e608d5852449ed940bfc51", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 104857600, "src": "/root/.ansible/tmp/ansible-tmp-1723798717.23-3584-128328247495760/source", "state": "file", "uid": 0
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "2c2ceccb5ec5574f791d45b63c940cff20550f9a", "dest": "/root/tst", "gid": 0, "group": "root", "md5sum": "2f282b84e7e608d5852449ed940bfc51", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 104857600, "src": "/root/.ansible/tmp/ansible-tmp-1723798717.23-3586-43347575088517/source", "state": "file", "uid": 0
}
# 验证
# 改之前
[root@s0 ~]# ls -lh
总用量 663M
-rwxrwxrwt. 1 bin daemon 0 8月 16 14:22 aaa
-rw-------. 1 root root 1.3K 8月 7 00:25 anaconda-ks.cfg
-rw-r--r--. 1 root root 663M 8月 16 15:06 mysql57.tar.gz
-rw-r--r--. 1 root root 11 8月 16 15:18 tst
# 改之后
[root@s0 ~]# ls -lh
总用量 763M
-rwxrwxrwt. 1 bin daemon 0 8月 16 14:22 aaa
-rw-------. 1 root root 1.3K 8月 7 00:25 anaconda-ks.cfg
-rw-r--r--. 1 root root 663M 8月 16 15:06 mysql57.tar.gz
-rw-r--r--. 1 root root 100M 8月 16 16:58 tst
backup=yes(备份)
1.删除tst文件
# 删除tst文件
[root@m0 ~]# ansible group02 -m file -a 'path=./tst state=absent backup=yes owner=bin group=daemon mode=1777'
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "path": "./tst", "state": "absent"
}
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "path": "./tst", "state": "absent"
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "path": "./tst", "state": "absent"
}
# 验证
# 删除之前
[root@s0 ~]# ls -lh
总用量 763M
-rwxrwxrwt. 1 bin daemon 0 8月 16 14:22 aaa
-rw-------. 1 root root 1.3K 8月 7 00:25 anaconda-ks.cfg
-rw-r--r--. 1 root root 663M 8月 16 15:06 mysql57.tar.gz
-rwxrwxrwt. 1 bin daemon 100M 8月 16 16:58 tst
# 删除之后
[root@s0 ~]# ls -lh
总用量 663M
-rwxrwxrwt. 1 bin daemon 0 8月 16 14:22 aaa
-rw-------. 1 root root 1.3K 8月 7 00:25 anaconda-ks.cfg
-rw-r--r--. 1 root root 663M 8月 16 15:06 mysql57.tar.gz
2.创建文件/tmp/a.txt
# 创建文件/tmp/a.txt
[root@m0 ~]# ansible group02 -m file -a 'path=/tmp/a.txt state=touch'
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "dest": "/tmp/a.txt", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 0
}
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "dest": "/tmp/a.txt", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 0
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "dest": "/tmp/a.txt", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 0
}
# 验证
[root@s0 ~]# ls /tmp/
a.txt
[root@s1 ~]# ls /tmp/
a.txt
3.进行备份
# 给原来的/tmp/a.txt进行备份(a.txt.4331.2024-08-16@17:23:26~),将新的内容(/etc/fstab)复制到a.txt中,并且修改了权限,属主和属组
[root@m0 ~]# ansible group02 -m copy -a 'src=/etc/fstab dest=/tmp/a.txt backup=yes owner=bin group=daemon mode=1777'
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "backup_file": "/tmp/a.txt.4331.2024-08-16@17:23:26~", "changed": true, "checksum": "20fcca9572fbd2d112b32e6446e42070695ce511", "dest": "/tmp/a.txt", "gid": 2, "group": "daemon", "md5sum": "a7adf78e321c6b78f8576849db9a5e73", "mode": "01777", "owner": "bin", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 502, "src": "/root/.ansible/tmp/ansible-tmp-1723800205.98-4148-210811390920713/source", "state": "file", "uid": 1
}
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "backup_file": "/tmp/a.txt.4258.2024-08-16@17:23:26~", "changed": true, "checksum": "20fcca9572fbd2d112b32e6446e42070695ce511", "dest": "/tmp/a.txt", "gid": 2, "group": "daemon", "md5sum": "a7adf78e321c6b78f8576849db9a5e73", "mode": "01777", "owner": "bin", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 502, "src": "/root/.ansible/tmp/ansible-tmp-1723800206.01-4149-123087950411073/source", "state": "file", "uid": 1
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "backup_file": "/tmp/a.txt.4103.2024-08-16@17:23:26~", "changed": true, "checksum": "20fcca9572fbd2d112b32e6446e42070695ce511", "dest": "/tmp/a.txt", "gid": 2, "group": "daemon", "md5sum": "a7adf78e321c6b78f8576849db9a5e73", "mode": "01777", "owner": "bin", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 502, "src": "/root/.ansible/tmp/ansible-tmp-1723800206.04-4151-203739831332220/source", "state": "file", "uid": 1
}
# 验证
[root@s0 ~]# ls /tmp/
a.txt
a.txt.4331.2024-08-16@17:23:26~
[root@s0 ~]# cat /tmp/a.txt
#
# /etc/fstab
# Created by anaconda on Wed Aug 7 00:21:24 2024
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=ee7d9803-5773-4f48-acdc-7b7344699e0d /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/cdrom /mnt iso9660 defaults 0 0
# 创建的a.txt文件没有添加内容
[root@s0 ~]# cat /tmp/a.txt.4331.2024-08-16@17\:23\:26~
backup参数小结:
使用backup参数控制是否备份文件
backup=yes表示如果拷贝的文件内容与原内容不一样,则会备份一份/tmp/3333(备份文件名加上时间)文件,再拷贝新的文件/tmp/333
master# ansible group01 -m copy -a 'src=/etc/fstab dest=/tmp/333 backup=yes owner=daemon group=daemon'
6.fetch模块
# 回收110,111,112主机上的内容 # 回收110,111,112主机上的网卡信息,将信息放到本机的tmp/目录下 ansible group02 -m fetch -a 'src=/etc/sysconfig/network-scripts/ifcfg-ens33 dest=/tmp'
7.user模块
创建用户
创一个名为aaaa的用户,并且将这个任务发布到110,111,112主机上
[root@m0 ~]# ansible group02 -m user -a 'name=aaaa state=present'
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "comment": "", "create_home": true, "group": 1000, "home": "/home/aaaa", "name": "aaaa", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1000
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "comment": "", "create_home": true, "group": 1000, "home": "/home/aaaa", "name": "aaaa", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1000
}
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "comment": "", "create_home": true, "group": 1000, "home": "/home/aaaa", "name": "aaaa", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1000
}
# 验证
[root@ab ~]# grep aaaa /etc/passwd
aaaa:x:1000:1000::/home/aaaa:/bin/bash
创建名为mysql的用户
[root@m0 ~]# ansible group02 -m user -a 'name=mysql state=present system=yes shell="/sbin/nologin"'
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "comment": "", "create_home": true, "group": 995, "home": "/home/mysql", "name": "mysql", "shell": "/sbin/nologin", "state": "present", "system": true, "uid": 997
}
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "comment": "", "create_home": true, "group": 995, "home": "/home/mysql", "name": "mysql", "shell": "/sbin/nologin", "state": "present", "system": true, "uid": 997
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "comment": "", "create_home": true, "group": 995, "home": "/home/mysql", "name": "mysql", "shell": "/sbin/nologin", "state": "present", "system": true, "uid": 997
}
# 验证
[root@ab ~]# grep mysql /etc/passwd
mysql:x:997:995::/home/mysql:/sbin/nologin
在用户mysql创建一个mysql-files并且权限为750的文件
[root@m0 ~]# ansible group02 -m file -a 'path=/usr/local/mysql/mysql-files state=directory owner=mysql group=mysql mode=750'
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "gid": 995, "group": "mysql", "mode": "0750", "owner": "mysql", "path": "/usr/local/mysql/mysql-files", "secontext": "unconfined_u:object_r:usr_t:s0", "size": 6, "state": "directory", "uid": 997
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "gid": 995, "group": "mysql", "mode": "0750", "owner": "mysql", "path": "/usr/local/mysql/mysql-files", "secontext": "unconfined_u:object_r:usr_t:s0", "size": 6, "state": "directory", "uid": 997
}
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "gid": 995, "group": "mysql", "mode": "0750", "owner": "mysql", "path": "/usr/local/mysql/mysql-files", "secontext": "unconfined_u:object_r:usr_t:s0", "size": 6, "state": "directory", "uid": 997
}
# 验证
[root@ab ~]# ll /usr/local/mysql/
总用量 0
drwxr-x---. 2 mysql mysql 6 8月 16 21:38 mysql-files
传用户的时候传用户的密码
1.在m0上创建一个带密码的用户
[root@m0 ~]# useradd abc [root@m0 ~]# echo "abc"|passwd --stdin abc 更改用户 abc 的密码 。 passwd:所有的身份验证令牌已经成功更新。
2.传用户和密码
[root@m0 ~]# ansible group02 -m user -a 'name=abc state=present uid=1999 password=abc'
[WARNING]: The input password appears not to have been hashed. The 'password'
argument must be encrypted for this module to work properly.
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "comment": "", "create_home": true, "group": 1999, "home": "/home/********", "name": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1999
}
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "comment": "", "create_home": true, "group": 1999, "home": "/home/********", "name": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1999
}
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "append": false, "changed": true, "comment": "", "group": 1001, "home": "/home/********", "move_home": false, "name": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "uid": 1999
}
# 验证
[root@ab ~]# grep abc /etc/passwd
abc:x:1999:1999::/home/abc:/bin/bash
生成随机密码
[root@m0 ~]# echo 123456 | openssl passwd -1 -stdin $1$Erd.NZdm$mAQtHJ60GLd6r0aAHQrlp0 [root@m0 ~]# echo 123456 | openssl passwd -1 -stdin $1$SDbhjmBG$LI3FAKVFPpuoyk2Xvk8Sm/
创建一个普通用户叫hadoop,并产生空密码密钥对
[root@m0 ~]# ansible group02 -m user -a 'name=hadoop generate_ssh_key=yes'
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "comment": "", "create_home": true, "group": 2000, "home": "/home/hadoop", "name": "hadoop", "shell": "/bin/bash", "ssh_fingerprint": "2048 SHA256:Td4nLMeG7EnRVFiPo3aVNQ6ng3sDsSLnDza2/JGEPUs ansible-generated on ab.haha (RSA)", "ssh_key_file": "/home/hadoop/.ssh/id_rsa", "ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6886Gpqr9+U9owjy+1khihyhuQyRj0mLfGa3Aw84CCBplGL3F997suKVB5iL0fFBEn9AcpBRxMu7OAniRoPHNSas0zynL+D8Rm7Fr3CdpSI9FR2hYUW3/sAaBbT1Rhbdy86+/QB7KjcfwhleIK7yUnLa5Ymz0h11Iyk5co3As7ZMvTJrJzmkv90nIRU2gCo6D6sAsGYlIiF2QDDHgx8Q9cIKN/dRcnp5FK1CkTkwY2rDed0KAYLEtP8pn1ydB1HymbujypZpi4EBH5OTOEllZWfCAC8hdDskn5A9EzGe7enylYNS8hKWBxxUUQq1Ck7Jx8+fhA1IfbYT6lXiPk14p ansible-generated on ab.haha", "state": "present", "system": false, "uid": 2000
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "comment": "", "create_home": true, "group": 2000, "home": "/home/hadoop", "name": "hadoop", "shell": "/bin/bash", "ssh_fingerprint": "2048 SHA256:SUbtRxZoEL8EbRV8U7hWXN31ltuYHt0XWa5u3EnPCxo ansible-generated on ab.haha (RSA)", "ssh_key_file": "/home/hadoop/.ssh/id_rsa", "ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC2MSfgn9wn0tYU/rAbmckbDrcw9WGTuO1WwmIIAymsAMKioXRZQFNIvpIwWzpBnlypuS/6AJZlOGlJFkQB3oAZYs+cP1vVg8iRRuyPfZqxLQ2Hkc3cT6ouquP+NKl9x4WieQVA7CitmtuthnLXvTSxvXHIvOKPt4yB2J5TA5K9p9uwes8fI02TMmpz963n1AaWw9+iObmhbxUndegJ6bM2U8Cjh5Itl5iQDLXddfhxNLtPD+ny3zpUvA3UDwOHHfhSHS+Ue4q3LOEnr3TXDzrpUYpxJTy8JBevAVVgJF6qV4HlCKceerJDorKPHMWuKNEx2QJiJvNQjCRkiuVNQ27p ansible-generated on ab.haha", "state": "present", "system": false, "uid": 2000
}
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "comment": "", "create_home": true, "group": 2000, "home": "/home/hadoop", "name": "hadoop", "shell": "/bin/bash", "ssh_fingerprint": "2048 SHA256:l4xaMylvcQTjpNL8ZcpV0KXmIqG5dnyGOHJFmi5DbeM ansible-generated on ab.haha (RSA)", "ssh_key_file": "/home/hadoop/.ssh/id_rsa", "ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDGr80DmhnTzVeB8bmEOx9ERsTw/xUUJYI+/IJWl0jq7wgUa5QkwOhAwsX9ITFfab8+NxxvDu38iEId9nd+Y+z2CZsdc37oR0m8exZBUBKPyYyLhVIlqmg+DEMMKZALE0an4W5dbKgjmDt62azSm/1ye1Z7XRjyHyvbRfjijcNXvdlvPOl2VvjWp8YLPwEp2+HcoemdzD1pOxNouhqiN25tbX9B+jYXKe9VuH0KMKkjdiBGxYlN/fJNVaIOt+GL6qnQjlcGD/yDtlqDM7oZlZ/Q5HzEmvOkENUAXP1BhU8VZQds4KrzQ9xOTIvD24w22R9X7ZbDN2FIiKAqHKTcWCWX ansible-generated on ab.haha", "state": "present", "system": false, "uid": 2000
}
# 验证
[root@ab ~]# grep hadoop /etc/passwd
hadoop:x:2000:2000::/home/hadoop:/bin/bash
[root@ab ~]# ls ./.ssh/
authorized_keys
[root@ab ~]# cat ./.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDrLUA6COFlt/DTnmdEpK8Ho2xvUmVMNBRY5SZDiqDpokxOjwNrfU5eubCn6ibAoGNEdUgdlbpkwEXbsm+Ldh/DfecyZ9EZUX0Oa6fuKSOBswyieZ5KZ+EkeTJQWUFLZIGJduG0Z+xfR8LJHGXe9zD03W/aHbDwA+1mU17IZGKTS+04twYC/M7gEoEpwQpsJz1v9EuYBD2tf4VAF/BfiI+koM6AR5xhVQmaOwse97YEPcC7YVq4ECTx8dqjcmVT0BCg4UDkMYtKEetSUP4439mZOLgz/uW3GNigZqrnxXLVp+L8MQYCjGi07ARu7nJlMC32jyOOCAH0Eb/NJIQomZOL root@m0
删除用户
不会删除用户的家目录
# 删除用户hadoop
[root@m0 ~]# ansible group02 -m user -a 'name=hadoop state=absent'
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "force": false, "name": "hadoop", "remove": false, "state": "absent"
}
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "force": false, "name": "hadoop", "remove": false, "state": "absent"
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "force": false, "name": "hadoop", "remove": false, "state": "absent"
}
# 验证
[root@ab ~]# grep hadoop /etc/passwd
hadoop:x:2000:2000::/home/hadoop:/bin/bash
[root@ab ~]# grep hadoop /etc/passwd
# 删除hadoop,家目录默认没有删除
[root@ab ~]# ls /home
aaaa abc hadoop mysql
删除bbb 用户,家目录也被删除
使用remove=yes参数让其删除用户的同时也删除家目录
master# ansible group02 0m user -a 'name=bbb state=absent remove=yes'
8.cron模块
cron模块用于管理周期性任务时间
创建⼀个cron任务,不指定user的话,默认就是root
如果minute,hour,day,month,week不指定的话,默认都为*
创建计划任务
[root@m0 ~]# ansible group02 -m cron -a 'name="test cron1" user=root job="touch /tmp/111" minute=*/2'
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "envs": [], "jobs": ["test cron1"]
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "envs": [], "jobs": ["test cron1"]
}
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "envs": [], "jobs": ["test cron1"]
}
# 验证
111主机:
[root@ab ~]# crontab -l
#Ansible: test cron1
*/2 * * * * touch /tmp/111
112主机
[root@ab ~]# crontab -l
#Ansible: test cron1
*/2 * * * * touch /tmp/111
110主机
[root@ab ~]# crontab -l
#Ansible: test cron1
*/2 * * * * touch /tmp/111
删除cron任务
[root@m0 ~]# ansible group02 -m cron -a 'name="test cron1" state=absent'
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "envs": [], "jobs": []
}
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "envs": [], "jobs": []
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "envs": [], "jobs": []
}
# 验证
# 删除之前
[root@ab ~]# crontab -l
#Ansible: test cron1
*/2 * * * * touch /tmp/111
# 删除之后
[root@ab ~]# crontab -l
[root@ab ~]#
9.yum模块
[root@m0 ~]# ansible group02 -m yum -a 'name=ntpdate state=present' # 验证 111主机: [root@ab ~]# yum list installed | grep ntpdate ntpdate.x86_64 4.2.6p5-29.el7.centos.2 @base 112主机: [root@ab ~]# yum list installed | grep ntpdate ntpdate.x86_64 4.2.6p5-29.el7.centos.2 @base 110主机: [root@ab ~]# yum list installed | grep ntpdate ntpdate.x86_64 4.2.6p5-29.el7.centos.2 @base
cron模块:创建时间计划任务
# 写一个同步时间的计划任务发布到 110,111,112主机
[root@m0 ~]# ansible group02 -m cron -a 'name="abc" user=root job="/usr/sbin/ntpdate cn.ntp.org.cn" hour=2'
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "envs": [], "jobs": ["abc"]
}
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "envs": [], "jobs": ["abc"]
}
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "envs": [], "jobs": ["abc"]
}
# 验证
110主机:
[root@ab ~]# crontab -l
#Ansible: abc
* 2 * * * /usr/sbin/ntpdate cn.ntp.org.cn
111主机:
[root@ab ~]# crontab -l
#Ansible: abc
* 2 * * * /usr/sbin/ntpdate cn.ntp.org.cn
112主机:
[root@ab ~]# crontab -l
#Ansible: abc
* 2 * * * /usr/sbin/ntpdate cn.ntp.org.cn
10.service模块
# 关闭防火墙(将任务发布到110,111,112主机上) [root@m0 ~]# ansible group02 -m service -a 'name=firewalld state=stopped enabled=false' # 验证 [root@ab ~]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)Active: inactive (dead)Docs: man:firewalld(1) 8月 16 10:25:18 localhost.localdomain systemd[1]: Starting firewalld - dynamic .... 8月 16 10:25:19 localhost.localdomain systemd[1]: Started firewalld - dynamic f.... 8月 16 10:30:57 s0 systemd[1]: Stopping firewalld - dynamic firewall daemon... 8月 16 10:30:58 s0 systemd[1]: Stopped firewalld - dynamic firewall daemon. Hint: Some lines were ellipsized, use -l to show in full.
11.commend模块(执行命令)
将110,111,112主机关机
[root@m0 ~]# ansible group02 -m command -a 'shutdown -h 0' 192.168.2.110 | FAILED | rc=-1 >> Failed to connect to the host via ssh: ssh: connect to host 192.168.2.110 port 22: Connection refused 192.168.2.111 | FAILED | rc=-1 >> Failed to connect to the host via ssh: ssh: connect to host 192.168.2.111 port 22: Connection refused other | FAILED | rc=-1 >> Failed to connect to the host via ssh: ssh: connect to host 192.168.2.112 port 22: Connection refused
验证:

相关文章:
8.16-ansible的应用
ansible ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。 格式 ansible 主机ip|域名|组名|别名 -m ping|copy|... 参数 1.ping模块 m0 # 查看有没有安装epel [rootm0 ~]#…...
相似度计算方法-编辑距离 (Edit Distance)
定义 编辑距离(Edit Distance),也称为Levenshtein距离,是一种衡量两个字符串相似度的方法。它定义为从一个字符串转换为另一个字符串所需的最少单字符编辑操作次数,这些操作包括插入、删除或替换一个字符。 计算方法 …...
初识FPGA
大学的时候有一门verilog语言,觉得很难,不愿学。有学习套件是黑金的一块FPGA开发板,可能当时点灯和点数码管了。全都忘了。 今项目需要,使用FPGA中的ZYNQ,需要c语言开发,随即开始学习相关知识。 ZYNQ内部…...
探索 JavaScript:从入门到精通
目录 1. JavaScript 的介绍与基础 示例:弹出欢迎信息 JavaScript,作为网络时代最流行的脚本语言之一,赋予了网页生动活泼的动态功能。无论是新手还是经验丰富的开发者,掌握 JavaScript 的核心概念和技能都是开启网络编程之门的钥…...
这4款视频压缩软件堪称是压缩界的神器!
视频在我们的日常设备当中会占用相对较多的空间,尤其是喜欢用视频记录的朋友。但是过多过大的视频不仅会给我们的设备带来了压力,也不利于分享和管理。今天我就要给大家分享几个视频压缩的小妙招。 1、福昕压缩 直通车:www.foxitsoftware.cn…...
【ARM 芯片 安全与攻击 5.6 -- 侧信道与隐蔽信道的区别】
文章目录 侧信道与隐蔽信道的区别侧信道攻击(Side-channel Attack)侧信道攻击简介侧信道攻击 使用方法侧信道攻击示例隐蔽信道(Covert Channel)隐蔽信道简介隐蔽信道使用方法隐蔽信道代码示例侧信道与隐蔽信道在芯片及系统安全方面的使用侧信道的应用隐蔽信道的应用Summary…...
C#:Bitmap类使用方法—第4讲
大家好,今天接着上一篇文章继续讲。 下面是今天的方法: (1)Bitmap.MakeTransparent 方法:使此 Bitmap的默认透明颜色透明。 private void MakeTransparent_Example1(PaintEventArgs e) { // Create a Bitmap object…...
Vue是如何实现nextTick的?
你好同学,我是沐爸,欢迎点赞、收藏和关注。个人知乎 Vue.js 的 nextTick 函数是一个非常重要的功能,它用于延迟执行代码块到下次 DOM 更新循环之后。这在 Vue.js 的异步更新队列机制中非常有用,尤其是在你需要基于更新后的 DOM 来…...
rabbitmq镜像集群搭建
用到的ip地址 ip地址端口192.168.101.65(主)15672192.168.101.7515672192.168.101.8515672 安装erlang和rabbitmq 安装 安装三个包 yum install esl-erlang_23.0-1_centos_7_amd64.rpm -y yum install esl-erlang-compat-18.1-1.noarch.rpm -y rpm -…...
《c++并发编程实战》 笔记
《c并发编程实战》 笔记 1、你好,C的并发世界为什么要使用并发 第2章 线程管理2.1.1 启动线程2.2 向线程函数传递参数2.5 识别线程 第3章 线程间共享数据3.2.1 C中使用互斥量避免死锁的进阶指导保护共享数据的替代设施 第4章 同步并发操作4.1 等待一个事件或其他条件…...
57qi5rW35LqRZUhS pc.mob SQL注入漏洞复现
0x01 产品简介 57qi5rW35LqRZUhS是大中型企业广泛采用人力资源管理系统。某云是国内顶尖的HR软件供应商,是新一代eHR系统的领导者。 0x02 漏洞概述 57qi5rW35LqRZUhS pc.mob 接口存在SQL注入漏洞,未经身份验证的远程攻击者除了可以利用 SQL 注入漏洞获取数据库中的信息(例…...
微信小程序--27(自定义组件4)
一、父子组件之间通信的3种方式 1、属性绑定 用于父组件向子组件的只当属性设置数据,但只能设置JSON兼容的数据 2、事件绑定 用于子组件向父组件传递数据,可以传递任意数据 3、获取组件实例 父组件还可以通过this.select Component()获取子组件的实…...
Linux | Linux进程万字全解:内核原理、进程状态转换、优先级调度策略与环境变量
目录 1、从计算机组成原理到冯诺依曼架构 计算机系统的组成 冯诺依曼体系 思考:为什么计算机不能直接设计为 输入设备-CPU运算-输出设备 的结构? 2、操作系统(Operator System) 概念 设计OS的目的 描述和组织被管理对象 3、进程 基本概念 进程id和父进程…...
VBA技术资料MF184:图片导入Word添加说明文字设置格式
我给VBA的定义:VBA是个人小型自动化处理的有效工具。利用好了,可以大大提高自己的工作效率,而且可以提高数据的准确度。“VBA语言専攻”提供的教程一共九套,分为初级、中级、高级三大部分,教程是对VBA的系统讲解&#…...
在函数设计中应用单一职责原则:函数分解与职责分离
在函数设计中应用单一职责原则:函数分解与职责分离 引言 单一职责原则(Single Responsibility Principle, SRP)是面向对象设计原则中的核心原则之一,强调一个类或函数应该只有一个责任或理由去改变。在函数设计中,应…...
多线程锁机制面试
目录 乐观锁的底层原理 ReentrantLock的实现原理 读写锁 ReentrantReadWriteLock synchronized 底层原理 Lock和synchronized的区别 乐观锁的底层原理 版本号机制 在数据库表中添加一个版本号字段(如 version),每次更新数据时都会将版本号…...
《SQL 中计算地理坐标两点间距离的魔法》
在当今数字化的世界中,地理数据的处理和分析变得越来越重要。当我们面对一个包含地理坐标数据的表时,经常会遇到需要计算两点之间距离的需求。无论是在物流配送路线规划、地理信息系统应用,还是在基于位置的服务开发中,准确计算两…...
微服务可用性设计
一、隔离 对系统或资源进行分割,实现当系统发生故障时能限定传播范围和影响范围。进一步的,通过隔离能够降低系统之间得耦合度,使得系统更容易维护和扩展。某些业务场景下合理使用隔离技巧也能提高整个业务的性能。我理解隔离本质就是一种解…...
【扒代码】dave readme文档翻译
jerpelhan/DAVE (github.com) 摘要 低样本计数器估算选定类别对象的数量,即使在图像中只有少量或没有标注样本的情况下。目前最先进的技术通过对象位置密度图的总和来估算总数量,但这种方法无法提供单个对象的位置和大小,这对于许多应用来说…...
c语言---文件
这一节我准备分三个部分来带领大家了解文件 ——一、有关文件的基础知识 ————二、文件的简单操作 ————————三、文件结束的判定 ————————————四、文件缓冲区 一、文件的基础知识: 首先在了解文件之前,我们需要了解C/C程序内存…...
2024年赣州旅游投资集团社会招聘笔试真
2024年赣州旅游投资集团社会招聘笔试真 题 ( 满 分 1 0 0 分 时 间 1 2 0 分 钟 ) 一、单选题(每题只有一个正确答案,答错、不答或多答均不得分) 1.纪要的特点不包括()。 A.概括重点 B.指导传达 C. 客观纪实 D.有言必录 【答案】: D 2.1864年,()预言了电磁波的存在,并指出…...
C# 类和继承(抽象类)
抽象类 抽象类是指设计为被继承的类。抽象类只能被用作其他类的基类。 不能创建抽象类的实例。抽象类使用abstract修饰符声明。 抽象类可以包含抽象成员或普通的非抽象成员。抽象类的成员可以是抽象成员和普通带 实现的成员的任意组合。抽象类自己可以派生自另一个抽象类。例…...
学习STC51单片机32(芯片为STC89C52RCRC)OLED显示屏2
每日一言 今天的每一份坚持,都是在为未来积攒底气。 案例:OLED显示一个A 这边观察到一个点,怎么雪花了就是都是乱七八糟的占满了屏幕。。 解释 : 如果代码里信号切换太快(比如 SDA 刚变,SCL 立刻变&#…...
AI病理诊断七剑下天山,医疗未来触手可及
一、病理诊断困局:刀尖上的医学艺术 1.1 金标准背后的隐痛 病理诊断被誉为"诊断的诊断",医生需通过显微镜观察组织切片,在细胞迷宫中捕捉癌变信号。某省病理质控报告显示,基层医院误诊率达12%-15%,专家会诊…...
SQL慢可能是触发了ring buffer
简介 最近在进行 postgresql 性能排查的时候,发现 PG 在某一个时间并行执行的 SQL 变得特别慢。最后通过监控监观察到并行发起得时间 buffers_alloc 就急速上升,且低水位伴随在整个慢 SQL,一直是 buferIO 的等待事件,此时也没有其他会话的争抢。SQL 虽然不是高效 SQL ,但…...
纯 Java 项目(非 SpringBoot)集成 Mybatis-Plus 和 Mybatis-Plus-Join
纯 Java 项目(非 SpringBoot)集成 Mybatis-Plus 和 Mybatis-Plus-Join 1、依赖1.1、依赖版本1.2、pom.xml 2、代码2.1、SqlSession 构造器2.2、MybatisPlus代码生成器2.3、获取 config.yml 配置2.3.1、config.yml2.3.2、项目配置类 2.4、ftl 模板2.4.1、…...
如何应对敏捷转型中的团队阻力
应对敏捷转型中的团队阻力需要明确沟通敏捷转型目的、提升团队参与感、提供充分的培训与支持、逐步推进敏捷实践、建立清晰的奖励和反馈机制。其中,明确沟通敏捷转型目的尤为关键,团队成员只有清晰理解转型背后的原因和利益,才能降低对变化的…...
Oracle11g安装包
Oracle 11g安装包 适用于windows系统,64位 下载路径 oracle 11g 安装包...
如何配置一个sql server使得其它用户可以通过excel odbc获取数据
要让其他用户通过 Excel 使用 ODBC 连接到 SQL Server 获取数据,你需要完成以下配置步骤: ✅ 一、在 SQL Server 端配置(服务器设置) 1. 启用 TCP/IP 协议 打开 “SQL Server 配置管理器”。导航到:SQL Server 网络配…...
【java】【服务器】线程上下文丢失 是指什么
目录 ■前言 ■正文开始 线程上下文的核心组成部分 为什么会出现上下文丢失? 直观示例说明 为什么上下文如此重要? 解决上下文丢失的关键 总结 ■如果我想在servlet中使用线程,代码应该如何实现 推荐方案:使用 ManagedE…...
