管理配置好:
1、创建分发秘钥
2、安装ansible工具。
3、一键执行各服务脚本
具体服务一键实现的几个步骤:
1、计划要做。
2、单机安装好,步骤抽出来。
3、写成脚本,一键安装。
4、拿到管理机安装
1)一键完成rsync服务端安装。
剧本:
#1)安装
#yum install rsync -y
#2)配置配置文件/etc/rsyncd.conf
cp /etc/rsyncd.conf{,.ori}
cat>/etc/rsyncd.conf< #rsync_config_______________start #created by oldboy #site: http://www.oldboyedu.com uid = rsync gid = rsync use chroot = no fake super = yes max connections = 200 timeout = 600 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log ignore errors read only = false list = false hosts allow = 172.16.1.0/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password [backup] comment = welcome to oldboyedu backup! path = /backup/ EOF #3)创建用户和备份目录 useradd rsync id rsync mkdir -p /backup chown -R rsync.rsync /backup/ ls -ld /backup/ #4)启动和检查 systemctl start rsyncd systemctl enable rsyncd systemctl status rsyncd ps -ef|grep sync|grep -v grep #检查进程 netstat -lntup|grep 873 #检查端口 #5)配置密码文件 echo "rsync_backup:oldboy" > /etc/rsync.password chmod 600 /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 chmod 600 /etc/rsync.password cat /etc/rsync.password ls -l /etc/rsync.password rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password #最终脚本路径/server/scripts/install_rsync_client.sh,需提前测试成功。 3)配置管理机61-m01: 1)实现批量分发秘钥,免秘钥管理 #!/bin/bash yum install ansible -y #含sshpass [ ~/.ssh/id_rsa ]&& rm -fr ~/.ssh ssh-keygen -f ~/.ssh/id_rsa -P '' -q for ip 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 localfile remotedir" exit 1 fi for n 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 for n in 7 31 41 do scp -P 22 -rp $1 root@$n:$2 &>/dev/null done [root@m01 /server/scripts]# cat cmd.sh for n in 31 41 7 do echo "=====172.16.1.$n======" ssh 172.16.1.$n "$1" done 方法2:使用ansible工具 yum install 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 ####Start by oldboy#2020-04-26### PermitEmptyPasswords no UseDNS no GSSAPIAuthentication no #ListenAddress 172.16.1.7:22 ####End by oldboy#2018-04-26### 方法1:脚本分发 [root@m01 /server/scripts]# sh fenfa.sh /etc/ssh/sshd_config /etc/ssh/ 7 successful [ 确定 ] 31 successful [ 确定 ] 41 successful [ 确定 ] [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分发 ansible oldboy -m copy -a "src=/etc/ssh/sshd_config dest=/etc/ssh/sshd_config backup=yes" ansible oldboy -m shell -a "systemctl restart sshd" 从管理机实现一键安装install_rsync_server.sh ansible 172.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 ansible rsync_client -m script -a "/server/scripts/install_rsync_client.sh" 实现从管理机一键完成安装rsync服务端和客户端 3)一键完成nfs服务端。 4)一键完成nfs客户端。 5)一键完成sersync服务端。 6)一键完成sersync客户端。 一个脚本one_key.sh或者一个ansible命令。完成 项目实践作业: rsync服务端写成脚本 r1.sh rsync客户端写成脚本 r2.sh nfs服务端写成脚本 n1.sh nfs客户端写成脚本 n2.sh sersync服务端写成脚本 s1.sh sersync客户端写成脚本 s2.sh /server/scripts/one_key_gaoding.sh ansible r1 -m copy -a "src=/server/scripts/r1.sh dest=/server/scripts/ mode=ugo+x" ansible r1 -m shell -a "sh /server/scripts/r1.sh" ansible r1 -m copy -a "src=/server/scripts/r2.sh dest=/server/scripts/ mode=ugo+x" ansible r2 -m shell -a "sh /server/scripts/r2.sh" ansible n1 -m shell -a "sh /server/scripts/n1.sh" ansible n2 -m shell -a "sh /server/scripts/n2.sh" ansible s1 -m shell -a "sh /server/scripts/s1.sh" ansible s2 -m shell -a "sh /server/scripts/s2.sh" /bin/sh /server/scripts/one_key_gaoding.sh 也可以使用script模块,替代copy+shell模块 12.8 playbook 12.8.1 什么是playbook? 把所有操作按照ansible编程语法,放在文件里执行就是playbook。 12.8.2 ansible剧本编写格式说明 ansible剧本遵循PYyaml语法规则进行编写,yaml文件基本编写规则如下说明: 规则一:缩进 yaml使用一个固定的缩进风格表示数据层结构关系,需要每个缩进级别由两个空格组成。切记一定不能使用tab键进行缩进。 规则二:冒号 每个冒号后面一定要有一个空格(以冒号结尾不需要空格,表示文件路径的模版可以不需要空格) 规则三:短横线 想要表示列表项,使用一个短横杠加一个空格。多个项使用同样的缩进级别作为同一个列表的一部分 1.YAML介绍 YAML是一个可读性高的用来表达资料序列的格式。YAML参考了其他多种语言,包括:XML、C语言、Python、Perl以及电子邮件格式RFC2822等。Clark Evans在2001年在首次发表了这种语言,另外Ingy dt Net与Oren Ben-Kiki也是这语言的共同设计者。 YAML Ain't Markup Language,即YAML不是XML。不过,在开发的这种语言时,YAML的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)。其特性: YAML的可读性好 YAML和脚本语言的交互性好 YAML使用实现语言的数据类型 YAML有一个一致的信息模型 YAML易于实现 YAML可以基于流来处理 YAML表达能力强,扩展性好 更多的内容及规范参见http://www.yaml.org。 12.8.3 playbook替代方案 playbook替代方案1:不同样 [root@m01 ~]# cat ansible.sh #判断 循环,可以脚本实现。 ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch" ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch owner=oldboy group=oldboy mode=ugo=rwx" ansible oldboy -m yum -a "name=nginx state=installed" ansible oldboy -m service -a "name=crond state=started enabled=yes" ansible oldboy -m cron -a "name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'" playbook替代方案2:通用 [root@m01 ~]# cat ~/set.sh touch /tmp/oldboy_file chown oldboy.oldboy /tmp/oldboy_file yum install nginx -y /etc/init.d/crond start chkconfig cornd on echo '#sync time oldboy' >>/var/spool/cron/root echo '00 00 * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' >>/var/spool/cron/root 执行: ansible oldboy -m script -a "~/set.sh" 12.8.4实践 ansible oldboy -m shell -a "echo oldboy linux. >/tmp/oldboy.log" 把这行命令写成剧本: 准备: mkdir -p /etc/ansible/yaml cd /etc/ansible/yaml vim p1.yml 开始编写: [root@m01 /etc/ansible/yaml]# cat /etc/ansible/yaml/p1.yml - hosts: oldboy tasks: - name: Create a log file shell: echo oldboy linux. >/tmp/oldboy.log [root@m01 /etc/ansible/yaml]# cat p2.yml - hosts: oldboy tasks: - shell: echo oldboy linux. >/tmp/oldboy.log [root@m01 /etc/ansible/yaml]# ansible-playbook -C /etc/ansible/yaml/p1.yml 说明:利用ansibl-playbook命令执行剧本,-C参数表示测试剧本任务执行,类似话剧的彩排一样。 PLAY [oldboy] *********************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************** ok: [172.16.1.41] ok: [172.16.1.31] TASK [Create a log file] ************************************************************************************************ skipping: [172.16.1.41] skipping: [172.16.1.31] PLAY RECAP ************************************************************************************************************** 172.16.1.31 : ok=1 changed=0 unreachable=0 failed=0 172.16.1.41 : ok=1 changed=0 unreachable=0 failed=0 执行: [root@m01 /etc/ansible/yaml]# ansible-playbook /etc/ansible/yaml/p1.yml PLAY [oldboy] *********************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************** ok: [172.16.1.31] ok: [172.16.1.41] TASK [Create a log file] ************************************************************************************************ changed: [172.16.1.31] changed: [172.16.1.41] PLAY RECAP ************************************************************************************************************** 172.16.1.31 : ok=2 changed=1 unreachable=0 failed=0 172.16.1.41 : ok=2 changed=1 unreachable=0 failed=0 ansible oldboy -m file -a "dest=/tmp/oldboy1 state=touch" 编写剧本: [root@m01 /etc/ansible/yaml]# cat p3.yml - hosts: oldboy remote_user: root tasks: - name: Create New File file: name=/tmp/oldboy1 state=touch 定时任务: ansible oldboy -m cron -a "name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'" #crond-id-001:time sync by oldboy */5 * * * * /usr/sbin/ntpdate ntp3.aliyun.com >/dev/null 2>&1 编写剧本: [root@m01 /etc/ansible/yaml]# cat p4.yml - hosts: oldboy tasks: - name: Cron time sync cron: name='sync time' minute=*/10 job='/usr/sbin/ntpdate ntp3.aliyun.com >/dev/null 2>&1' 练习题:每周一1-5 上午8:30 /server/scripts/class.sh 编写剧本: [root@m01 /etc/ansible/yaml]# cat p5.yml - hosts: oldboy tasks: - name: Cron 每周1-5,8:30 老男孩脱产上课 cron: name='oldboy class' minute=30 hour=08 weekday=1-5 job='/bin/sh /server/scripts/class.sh>/dev/null 2>&1' 结果: [root@nfs01 ~]# crontab -l|tail -2 #Ansible: oldboy class 30 08 * * 1-5 /bin/sh /server/scripts/class.sh>/dev/null 2>&1 ansible oldboy -m copy -a "src=/etc/rsyncd.conf dest=/etc/rsyncd.conf backup=yes" ansible oldboy -m copy -a "content='rsync_backup:oldboy' dest=/etc/rsync.password backup=yes mode=0600" 编写剧本: [root@m01 /etc/ansible/yaml]# cat p6.yml - hosts: backup tasks: - name: copy rsyncd.conf copy: src=/data/rsyncd.conf.template dest=/etc/rsyncd.conf mode=0600 backup=yes - name: create rsync.password copy: content='rsync_backup:oldboy' dest=/etc/rsync.password mode=0600 3个步骤 1、Linux命令行操作 #3)创建用户和备份目录 useradd rsync id rsync mkdir -p /backup chown -R rsync.rsync /backup/ ls -ld /backup/ #4)启动和检查 systemctl start rsyncd systemctl enable rsyncd systemctl status rsyncd 2、ansible命令 ansible oldboy -m systemd -a "name=crond.service enabled=no state=stopped " ansible oldboy -m command -a "systemctl status crond" ansible oldboy -m systemd -a "name=crond.service enabled=yes state=started" 3、编写剧本: [root@m01 ~]# cat /etc/ansible/yaml/p6.yml - hosts: backup tasks: - name: copy rsyncd.conf copy: src=/data/rsyncd.conf.template dest=/etc/rsyncd.conf mode=0600 backup=yes - name: create rsync.password copy: content='rsync_backup:oldboy' dest=/etc/rsync.password mode=0600 - name: create user 用user模块 替代 command: useradd rsync -s /sbin/nologin -M - name: create dir 用file模块替代 command: mkdir -p /backup - name: shouquan 用file模块替代 command: chown -R rsync.rsync /backup/ - name: startup rsyncd systemd: name='rsyncd.service' state=restarted enabled=yes 执行三部曲: ansible-playbook --syntax-check /etc/ansible/yaml/p6.yml ansible-playbook -C /etc/ansible/yaml/p6.yml ansible-playbook /etc/ansible/yaml/p6.yml 检测结果: rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password