Day39
作者:孙鹏鹏
归档:课后笔记
时间:2019/4/24
快捷键:
Ctrl + 1 标题1
Ctrl + 2 标题2
Ctrl + 3 标题3
Ctrl + 4 标题4
Ctrl + 5 程序代码
Ctrl + 6 正文
Ctrl + 7 实例1-1
格式说明:
蓝色字体:注释
黄色背景:重要
绿色背景:注意
老男孩教育教学核心思想6重:重目标、重思路、重方法、重实践、重习惯、重总结
学无止境,老男孩教育成就你人生的起点!
联系方式:
网站运维QQ交流群:
Linux 385168604架构师390642196
Python 29215534大数据421358633
官方网站:
目 录
第一章:
#!/bin/bash
ssh-keygen-f ~/.ssh/id_rsa -P '' -q
forip in 7 8 41 31
do
sshpass -p123456 ssh-copy-id -i~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.$ip
done
ansible模块查看和帮助*****
查找模块
ansible-doc-l #模块就Linux命令了。
查看某个模块的具体参数帮助
ansible-doc-s command #Linux命令参数
command模块 *****
功能说明:
command Executes a command on a remote node
功能说明:执行一个命令在远程节点上
操作实践:
ansibleoldboy -m command -a "free -m"
ansibleoldboy -m command -a "df -h"
ansibleoldboy -m command -a "ls /root"
ansibleoldboy -m command -a "cat redhat-release"
ansibleoldboy -m command -a "cat /etc/redhat-release"
最通用的功能。
[root@m01~]# ansible oldboy -m command -a "cat /etc/redhat-release"
172.16.1.7| CHANGED | rc=0 >>
CentOSLinux release 7.6.1810 (Core)
[root@m01~]# cat /server/scripts/cmd.sh
for n in 31 41
do
echo "=====172.16.1.$n======"
ssh 172.16.1.$n "$1"
done
[root@m01~]# sh /server/scripts/cmd.sh "cat /etc/redhat-release"
=====172.16.1.31======
CentOS Linux release 7.6.1810 (Core)
=====172.16.1.41======
CentOS Linux release 7.6.1810 (Core)
特殊:不支持的东西,例如> < | &等$HOME,替代方案用shell模块
ansibleoldboy -m shell -a "ps -ef|grep ssh"
ansibleoldboy -m shell -a "echo oldboy >/tmp/a.log"
参数:chdir=/tmp配置相当于cd /tmp
[root@m01~]# ansible oldboy -m command -a"pwd chdir=/etc"
ansibleoldboy -m shell -a "cd/etc/;pwd"
参数:creates=/etc 相当于条件测试 [ -e /etc ]||pwd和下面removes相反
[root@m01~]# ansible oldboy -m command -a"pwd creates=/etc"
参数:removes=/root 相当于条件测试 [ -e /root ]&&ls
/root
ansibleoldboy -m command -a "ls /rootremoves=/root"
ansibleoldboy -m shell -a "[ -d /etc]||pwd"
[root@m01~]# ansible oldboy -m command -a"cat /etc/hosts removes=/etc/hosts"
参数:warn=False 忽略警告
[root@m01 ~]# ansible oldboy -m command -a "chmod 000 /etc/hostswarn=False"
shell模块功能说明:
功能说明:执行一个命令在远程节点上
shell Execute commands in nodes.
实践:增加文本文件
[root@m01 ~]# ansible oldboy -m shell
-a "echo oldboy >/tmp/tmp.txt"
172.16.1.41| CHANGED | rc=0 >>
172.16.1.31| CHANGED | rc=0 >>
[root@m01 ~]# ansible oldboy -m shell
-a "cat /tmp/tmp.txt"
172.16.1.41| CHANGED | rc=0 >>
oldboy
172.16.1.31| CHANGED | rc=0 >>
oldboy
要执行的脚本必须在远程机器上存在:
[root@m01 ~]# ansible oldboy -m shell
-a "sh /server/scripts/bak.sh"
172.16.1.41| FAILED | rc=127 >>
sh:
/server/scripts/bak.sh: 没有那个文件或目录non-zero return code
172.16.1.31 | CHANGED | rc=0 >>
copy模块功能说明:
功能说明:复制文件到远程主机
实践1:把/etc/hosts拷贝到/opt下,权限设置400,用户和组设置root
ansibleoldboy -m copy -a "src=/etc/hosts dest=/opt mode=0400 owner=rootgroup=root backup=yes"
实践2:把/etc/passwd拷贝/tmp下改名为oldgirl,用户和组为oldboy,权限600,如果有存在同名文件覆盖
ansible oldboy -m copy -a"src=/etc/passwd dest=/tmp/oldgirl.txt owner=oldboy group=oldboy mode=0600force=yes"
script模块功能说明:
功能说明:远程节点上运行本地脚本模块
shell模块和script模块执行脚本区别:
[root@m01 /server/scripts]# ansible
oldboy -m shell -a "sh /server/scripts/bak.sh"
172.16.1.41| FAILED | rc=127 >>
sh:
/server/scripts/bak.sh: 没有那个文件或目录non-zero return code
172.16.1.31| FAILED | rc=127 >>
sh:
/server/scripts/bak.sh: 没有那个文件或目录non-zero return code
本地脚本,在远端执行。
[root@m01
/server/scripts]# ansible oldboy -m script -a
"/server/scripts/new.sh"
file模块功能说明:
功能说明:设置文件属性
参数实践:创建数据文件(普通文件目录 软链接文件)
ansibleoldboy -m file -a "dest=/tmp/oldboy_dir state=directory"
ansibleoldboy -m command -a "mkdir -p /tmp/oldboy_dir1 warn=false"
ansibleoldboy -m file -a "dest=/tmp/oldboy1 state=touch"
ansibleoldboy -m command -a "touch /tmp/oldboy_file1.txt warn=false"
替代方案:
ansibleoldboy -m command -a "chmod 777/etc/hosts warn=false"
ansibleoldboy -m command -a "chmod 644/etc/hosts warn=false"
ansibleoldboy -m command -a "chown oldboy/etc/hosts warn=false"
ansibleoldboy -m command -a "chown root/etc/hosts warn=false"
实践操作
创建目录:mkdir
/tmp/oldboy_dir
ansibleoldboy -m file -a "dest=/tmp/oldboy_dir state=directory"
递归设置权限:
ansibleoldboy -m file -a "dest=/tmp/oldboy_dir state=directory mode=644recurse=yes"
创建文件:touch
/tmp/oldboy_file
ansibleoldboy -m file -a "dest=/tmp/oldboy_file state=touch"
删除文件:rm
-f /tmp/oldboy_file
ansibleoldboy -m file -a "dest=/tmp/oldboy_file state=absent"
创建链接文件:ln
-s /etc/hosts /tmp/link_file
ansible oldboy -m file -a"src=/etc/hosts dest=/tmp/link_file state=link"
yum模块功能说明:
功能说明:yum包管理模块
实践操作
ansibleoldboy -m command -a "yum installnginx -y"
ansibleoldboy -m yum -a "name=nginx state=installed"
ansibleoldboy -m yum -a "name=nc state=installed"
[root@nfs01 oldboy_dir]# rpm -qa nginx
nginx-1.10.2-1.el6.x86_64
###不要用yum卸载,可用rpm -e卸载。
systemd模块功能说明:(service模块)
实践:
ansibleoldboy -m systemd -a "name=crond.service enabled=no state=stopped "
ansibleoldboy -m command -a "systemctl status crond"
ansible oldboy -m systemd -a"name=crond.service enabled=yes state=started"
#service模块功能说明:
功能说明:启动停止服务
#相当于
#service crond stop|/etc/init.d/crondstop
#chkconfig crond off
ansibleoldboy -m service -a "name=crond state=stop enabled=no"
#相当于/etc/init.d/crond start
chkconfig crond on
ansibleoldboy -m service -a "name=crond state=started enabled=yes"
ansible oldboy -m command -a"name=crond state=started enabled=yes"
ron模块功能说明:
功能说明:管理定时任务条目信息模块
cron Manage cron.d and crontab entries
定时任务格式:
* * * * * CMD
定时任务时间参数:
minute: # ( 0-59, *, */2, etc )
hour: # ( 0-23, *, */2, etc )
day: # ( 1-31, *, */2, etc )
month: # ( 1-12, *, */2, etc )
weekday: # ( 0-6 for Sunday-Saturday, *,etc )
job: #命令
创建定时任务:
ansible oldboy -m cron -a"name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov>/dev/null 2>&1'"
管理配置好:
1、创建分发秘钥
2、安装ansible工具。
3、一键执行各服务脚本
具体服务一键实现的几个步骤:
1、计划要做。
2、单机安装好,步骤抽出来。
3、写成脚本,一键安装。
4、拿到管理机安装
1)一键完成rsync服务端安装。
剧本:
#1)安装
#yuminstall rsync -y
#2)配置配置文件/etc/rsyncd.conf
cp/etc/rsyncd.conf{,.ori}
cat>/etc/rsyncd.conf<
#rsync_config_______________start
#createdby oldboy
#site:http://www.oldboyedu.com
uid= rsync
gid= rsync
usechroot = no
fakesuper = yes
maxconnections = 200
timeout= 600
pidfile = /var/run/rsyncd.pid
lockfile = /var/run/rsync.lock
logfile = /var/log/rsyncd.log
ignoreerrors
readonly = false
list= false
hostsallow = 172.16.1.0/24
hostsdeny = 0.0.0.0/32
authusers = rsync_backup
secretsfile = /etc/rsync.password
[backup]
comment= welcome to oldboyedu backup!
path= /backup/
EOF
#3)创建用户和备份目录
useraddrsync
idrsync
mkdir-p /backup
chown-R rsync.rsync /backup/
ls-ld /backup/
#4)启动和检查
systemctlstart rsyncd
systemctlenable rsyncd
systemctlstatus rsyncd
ps-ef|grep sync|grep -v grep #检查进程
netstat-lntup|grep 873 #检查端口
#5)配置密码文件
echo"rsync_backup:oldboy" > /etc/rsync.password
chmod600 /etc/rsync.password
cat/etc/rsync.password
ls-l /etc/rsync.password
#rsync服务端配置完成。
#最终脚本路径/server/scripts/install_rsync_server.sh,需提前测试成功。
2)一键完成rsync客户端安装。
#方法1:认证密码文件
echo"oldboy" > /etc/rsync.password
chmod600 /etc/rsync.password
cat/etc/rsync.password
ls-l /etc/rsync.password
rsync-avz /etc/hosts rsync_backup@172.16.1.41::backup--password-file=/etc/rsync.password
#最终脚本路径/server/scripts/install_rsync_client.sh,需提前测试成功。
3)配置管理机61-m01:
1)实现批量分发秘钥,免秘钥管理
#!/bin/bash
yuminstall ansible -y #含sshpass
[~/.ssh/id_rsa ]&& rm -fr ~/.ssh
ssh-keygen-f ~/.ssh/id_rsa -P '' -q
forip in 31 41 7 8
do
sshpass -p123456 ssh-copy-id -f -i~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.$ip
ssh 172.16.1.$ip "ifconfig eth0"
done
#脚本路径/server/scripts/create_key.sh
4)实现文件分发和命令管理
方法1:脚本开发分发工具
[root@m01/server/scripts]# cat fenfa.sh
#!/bin/sh
./etc/init.d/functions
if[ $# -ne 2 ]
then
echo "usage:/bin/sh $0 localfileremotedir"
exit 1
fi
forn in `cat /etc/ssh/hosts`
do
scp -P 22 -rp $1 root@$n:$2&>/dev/null
if [ $? -eq 0 ]
then
action "$n successful" /bin/true
else
action "$n failure"/bin/false
fi
done
=============
[root@m01/server/scripts]# cat fenfa.sh
#!/bin/sh
forn in 7 31 41
do
scp -P 22 -rp $1 root@$n:$2&>/dev/null
done
[root@m01/server/scripts]# cat cmd.sh
forn in 31 41 7
do
echo "=====172.16.1.$n======"
ssh 172.16.1.$n "$1"
done
方法2:使用ansible工具
yuminstall ansible -y
[root@m01/server/scripts]# cat /etc/ansible/hosts
[oldboy]
172.16.1.31
172.16.1.41
172.16.1.7
2)优化所有机器SSH
优化目标sshd_config
[root@m01/server/scripts]# sed -n '17,22p' /etc/ssh/sshd_config
####Startby oldboy#2020-04-26###
PermitEmptyPasswordsno
UseDNSno
GSSAPIAuthenticationno
#ListenAddress172.16.1.7:22
####Endby oldboy#2018-04-26###
方法1:脚本分发
[root@m01/server/scripts]# sh fenfa.sh /etc/ssh/sshd_config /etc/ssh/
7successful [ 确定 ]
31successful [ 确定 ]
41successful [ 确定 ]
[root@m01/server/scripts]#
[root@m01/server/scripts]#
[root@m01/server/scripts]#
[root@m01/server/scripts]# sh cmd.sh "systemctl restart sshd"
=====172.16.1.31======
=====172.16.1.41======
=====172.16.1.7======
方法2:使用ansible分发
ansibleoldboy -m copy -a "src=/etc/ssh/sshd_config dest=/etc/ssh/sshd_configbackup=yes"
ansibleoldboy -m shell -a "systemctl restart sshd"
从管理机实现一键安装install_rsync_server.sh
ansible172.16.1.41 -m script -a "/server/scripts/install_rsync_server.sh"
[root@m01/server/scripts]# cat /etc/ansible/hosts
[oldboy]
172.16.1.31
172.16.1.41
172.16.1.7
172.16.1.8
[rsync_client]
172.16.1.31
172.16.1.8
ansiblersync_client -m script -a "/server/scripts/install_rsync_client.sh"
实现从管理机一键完成安装rsync服务端和客户端
3)一键完成nfs服务端。
4)一键完成nfs客户端。
5)一键完成sersync服务端。
6)一键完成sersync客户端。
一个脚本one_key.sh或者一个ansible命令。完成