期中集群架构-第六章-ansible批量管理服务介绍
期中集群架构-第六章-ansible批量管理服务介绍
======================================================================
01.批量管理服务知识介绍
   a ansible是一个基于Python开发的自动化运维工具
   b ansible是一个基于SSH协议实现远程管理的工具
   c ansible软件可以实现多种批量管理操作 (批量系统配置、批量软件部署、批量文件拷贝、批量运行命令)
   
02.特征介绍
   a ansible软件服务端(管理端):不需要启动任何服务 默认服务端不需要任何的配置修改
   b ansible软件客户端(受控端):没有任何软件安装
03.ansible软件安装部署
   a ansible软件自动化环境架构规划
     管理主机一台:
     10.0.0.61 m01
     受控主机3台:
     10.0.0.41 backup
     10.0.0.31 nfs01
     10.0.0.7  web01
     linux系统6.9	
   b ansible软件自动化部署条件
     建议基于ssh秘钥方式建立远程连接
     a ssh密钥对创建(管理主机)
	 ssh-keygen -t dsa
	 影响免交互密钥对创建因素:
	 1)需要指定私钥存放路径
	    -f /root/.ssh/id_dsa
	 2)需要进行秘钥文件密码设置
	    -N "" 或 -P ""
    
	免交互创建秘钥对方法:
	ssh-keygen -t dsa -f /root/.ssh/id_dsa  -N ""
	
	 b 分发公钥文件(管理主机分发给受控主机)
	   ssh-copy-id -i /root/.ssh/id_dsa.pub  root@172.16.1.41
	   影响面交互批量分发秘钥因素
	   1)需要有确认连接过程,需要输入 yes/no
	      -o StrictHostKeyChecking=no
		  ssh-copy-id -i /root/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no root@172.16.1.41"
		  
		2)需要解决输入密码问题
		   sshpass -p123456     《--“-p”参数后面输入密码
		   [root@m01 .ssh]# sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa "-o StrictHostKeyChecking=no root@172.16.1.41"
           Now try logging into the machine, with "ssh '-o StrictHostKeyChecking=no root@172.16.1.41'", and check in:
           
             .ssh/authorized_keys
           to make sure we haven't added extra keys that you weren't expecting.
		   
		   
		 面交互批量分发公钥脚本:
		 #!/bin/bash
         rm /root/.ssh/id_dsa* -f                                <-先删除本地密钥对文件
         ssh-keygen -t dsa -f /root/.ssh/id_dsa  -N ""          《-用面交互方式创建新的密钥对文件       
         for ip in 31 41 7     
         do
         sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa "-o StrictHostKeyChecking=no root@172.16.1.$ip"
         done
         
		 
	 c   检查是否可以进行基于秘钥远程管理
	     ssh 172.16.1.31
		 面交互批量检查测试脚本
		 #!/bin/bash
         if [ $# -ne 1  ]   
         then
            echo "sh $0 agrs"
            exit 1
         fi
         
         for ip in 31 41 7
         do
         echo =======172.16.1.$ip===========
         ssh root@172.16.1.$ip $1
         echo ""
         done
		 
		 
		 
		基于ssh口令方式建立远程连接  (也可以)
		 vim /etc/ansible/hosts
	     [oldboy]
         172.16.1.7
         172.16.1.31 ansible_user=root ansible_password=123456
         172.16.1.41
	     
	     ansible 172.16.1.31 -m command -a "hostname" -k     --- 实现口令交互式远程管理
         SSH password: 
         172.16.1.31 | SUCCESS | rc=0 >>
         nfs01
	     
		 
		 
	   c ansible软件下载安装
	     ansible管理主机软件安装:
		 yum install ansible -y
		 ansible受控主机软件安装:(可选,一般针对被策略控制的受控主机需要安装)
		 yum install -y libselinux-python
		 
	   d ansible软件管理主机添加配置
	     [root@m01 ~]# cat /etc/ansible/hosts 
         [oldboy]                   《---分组
         172.16.1.41  
         172.16.1.31
         172.16.1.7
		 
		 
		实践练习:[root@m01 ~]# ansible oldboy -m command -a "hostname"
                  172.16.1.31 | SUCCESS | rc=0 >>
                  nfs01
                  
                  172.16.1.7 | SUCCESS | rc=0 >>
                  web01
                  
                  172.16.1.41 | SUCCESS | rc=0 >>
                  backup
				  
				  
				  
04. ansible软件应用过程
    ansible软件模块::
	[root@m01 ~]# ansible-doc -l |wc -l
    1852                  《---代表ansible一共有1852个模块
	
	ansible语法
	ansible 受控主机信息或受控主机组信息 -m 模块名称 -a 相关模块参数
    
    受控主机信息:远程主机IP地址 远程主机组名称 远程所有主机all
    -m:          指定模块
    -a:          利用模块中的某些参数功能
 
    命令类型模块:
    第一个模块:command	
	官方参考链接https://docs.ansible.com/ansible/latest/modules/command_module.html
	参数:chdir---在执行某个命令之前,先切换目录
	[root@m01 ~]# ansible 172.16.1.41 -m command -a "chdir=/backup/ pwd"
    172.16.1.41 | SUCCESS | rc=0 >>
    /backup
	
	参数:creates--判断一个文件是否存在,如果存在,后面的命令就不会执行
	[root@m01 ~]# ansible 172.16.1.41 -m command -a "creates=/etc/rsyncd.conf ls"
    172.16.1.41 | SUCCESS | rc=0 >>
    skipped, since /etc/rsyncd.conf exists
    
    [root@m01 ~]# ansible 172.16.1.41 -m command -a "creates=/etc/rsyncd.123 pwd"
    172.16.1.41 | SUCCESS | rc=0 >>
    /root
	
	参数:removes--判断一个文件是否存在,如果不存在,后面的命令就不会执行
    [root@m01 ~]# ansible 172.16.1.41 -m command -a "removes=/etc/rsyncd.123 pwd"
    172.16.1.41 | SUCCESS | rc=0 >>
    skipped, since /etc/rsyncd.123 does not exist
    
    [root@m01 ~]# ansible 172.16.1.41 -m command -a "removes=/etc/rsyncd.conf pwd"
    172.16.1.41 | SUCCESS | rc=0 >>
    /root
	
	
	第二个模块:shell模块(万能模块)
	参数:chdir---在执行某个命令之前,先切换目录
	参数:creates--判断一个文件是否存在,如果存在,后面的命令就不会执行
    参数:removes--判断一个文件是否存在,如果不存在,后面的命令就不会执行
    说明:shell模块可以满足command所有的功能,并且可以支持识别特殊字符信息< > | ;
	
	
    第三个模script --专门运行脚本的模块
    参数:chdir---在执行莫个命令前,先切换目录
	参数:creates---判断一个文件是否存在,如果已经存在了,后面的命令就不会执行
	参数:removes---判断一个文件是否存在,如果不存在,后面的命令就不会执行
	参数(必须要有的):free_form---表示执行command模块时,必须要有linux合法命令信息	
    文件类型模块:
	第一个模块:copy---复制模块
	参数:backup---对数据信息进行备份
	[root@m01 ~]# ansible oldboy -m copy -a "src=/tmp/file_01.txt  dest=/tmp/file_01.txt backup=yes"
    参数:src---定义要推送的数据信息
	参数:dest---定义要讲数据推送到远程主机的什么目录中
	[root@m01 ~]# touch /tmp/file_02.txt 
    [root@m01 ~]# ansible 172.16.1.31 -m copy -a "src=/tmp/file_02.txt dest=/root/file_cs.txt "
    172.16.1.31 | SUCCESS => {
        "changed": true, 
        "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
        "dest": "/root/file_cs.txt", 
        "gid": 0, 
        "group": "root", 
        "md5sum": "d41d8cd98f00b204e9800998ecf8427e", 
        "mode": "0644", 
        "owner": "root", 
        "size": 0, 
        "src": "/root/.ansible/tmp/ansible-tmp-1589614685.56-160656782822804/source", 
        "state": "file", 
        "uid": 0
    }
    [root@m01 ~]# ansible 172.16.1.31 -m shell -a "ls -l /root/file*"
    172.16.1.31 | SUCCESS | rc=0 >>
    -rw-r--r-- 1 root root 0 May 16 15:38 /root/file_cs.txt
    
    参数:owner---设置设置复制后文件的属主权限
	参数:group---设置复制后文件的属组权限
	参数:mode---设置复制后的文件权限
    [root@m01 ~]# ansible 172.16.1.31 -m copy -a "src=/tmp/file_02.txt dest=/root/file_cs.txt  owner=oldboy group=oldboy mode=600"
    172.16.1.31 | SUCCESS => {
    "changed": true, 
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
    "dest": "/root/file_cs.txt", 
    "gid": 500, 
    "group": "oldboy", 
    "mode": "0600", 
    "owner": "oldboy", 
    "path": "/root/file_cs.txt", 
    "size": 0, 
    "state": "file", 
    "uid": 500
    }
    [root@m01 ~]# ansible 172.16.1.31 -m shell -a "ls -l /root/file*"     《--验证结果
    172.16.1.31 | SUCCESS | rc=0 >>
    -rw------- 1 oldboy oldboy 0 May 16 15:38 /root/file_cs.txt
    
    第二个模块:file---文件属性修改 目录创建 文件创建
	参数:owner---设置设置复制后文件的属主权限
	参数:group---设置复制后文件的属组权限
	参数:mode---设置复制后的文件权限
	[root@m01 ~]# ansible 172.16.1.31 -m file -a "dest=/tmp/file_cs.txt owner=root group=root mode=666"
    172.16.1.31 | SUCCESS => {
        "changed": true, 
        "gid": 0, 
        "group": "root", 
        "mode": "0666", 
        "owner": "root", 
        "path": "/tmp/file_cs.txt", 
        "size": 0, 
        "state": "file", 
        "uid": 0
    }
    [root@m01 ~]# ansible 172.16.1.31 -m shell -a "ls -l /tmp/file*"
    172.16.1.31 | SUCCESS | rc=0 >>
    -rw-rw-rw- 1 root root 0 May 16 15:51 /tmp/file_cs.txt
    参数:state----用于指定创建目录或文件
	创建文件
	[root@m01 ~]# ansible 172.16.1.31 -m file -a "dest=/tmp/file01.txt state=touch"
    172.16.1.31 | SUCCESS => {
        "changed": true, 
        "dest": "/tmp/file01.txt", 
        "gid": 0, 
        "group": "root", 
        "mode": "0644", 
        "owner": "root", 
        "size": 0, 
        "state": "file", 
        "uid": 0
    }
    [root@m01 ~]# ansible 172.16.1.31 -m shell -a "ls -l /tmp/file*"
    172.16.1.31 | SUCCESS | rc=0 >>
    -rw-r--r-- 1 root root 0 May 16 16:01 /tmp/file01.txt
	
	创建目录
	[root@m01 ~]# ansible 172.16.1.31 -m file -a "dest=/tmp/file_dir state=directory"
    172.16.1.31 | SUCCESS => {
        "changed": true, 
        "gid": 0, 
        "group": "root", 
        "mode": "0755", 
        "owner": "root", 
        "path": "/tmp/file_dir", 
        "size": 4096, 
        "state": "directory", 
        "uid": 0
    }
    [root@m01 ~]# ansible 172.16.1.31 -m shell -a "ls -l /tmp/"
    172.16.1.31 | SUCCESS | rc=0 >>
    total 8
    drwxr-xr-x 2 root root 4096 May 16 16:03 file_dir
	
	
	包管理模块类型
	模块:yum---安装软件包模块
	参数:name----指定要安装软件的名称,已经软件的版本
	参数:state---installed安装   absent卸载
    [root@m01 ~]# ansible 172.16.1.31 -m yum -a "name=iftop state=installed"
    [root@m01 ~]# ansible 172.16.1.31 -m yum -a "name=iftop state=absent"
    参数:list---指定软件名称,查看软件是否可以安装,以及是否安装过了
	[root@m01 ~]# ansible 172.16.1.31 -m yum -a "list=iftop"
	
	
	系统模块类型
	模块:service---管理服务状态模块
	name:指定要管理的服务名称(管理的服务一定要在chkconfig中可以看到的))
	state:stoped(关闭) started(启动) restarted(重启) reloaded(平滑重启)
    enabled:yes 表示服务开机自启动 no表示服务开机不要自动启动
    ansible 172.16.1.31 -m service -a "name=nfs state=started enabled=yes"
    ansible 172.16.1.31 -m service -a "name=nfs state=stopped enabled=no"
	
   cron---定时任务模块
	* * * * *  /bin/sh /server/scripts/test.sh &>/dev/null
	
	minute=0-59 * */n , -   hour  day  month weekday  job='/bin/sh /server/scripts/test.sh &>/dev/null'
	
	添加定时任务
	ansible 172.16.1.41 -m cron -a "minute=0 hour=0 job='/bin/sh /server/scripts/test.sh &>/dev/null'"
    ansible 172.16.1.41 -m cron -a "name=oldboy02 minute=0 hour=0 job='/bin/sh /server/scripts/test.sh &>/dev/null'"
	
	删除定时任务
	ansible 172.16.1.41 -m cron -a "name=oldboy02 minute=0 hour=0 job='/bin/sh /server/scripts/test.sh &>/dev/null' state=absent"
	ansible 172.16.1.41 -m cron -a "name=oldboy01 state=absent"
	注释定时任务
	ansible 172.16.1.41 -m cron -a "name=oldboy01 minute=0 hour=0 job='/bin/sh /server/scripts/test.sh &>/dev/null' disabled=yes"
	ansible 172.16.1.41 -m cron -a "name=oldboy01 job='/bin/sh /server/scripts/test.sh &>/dev/null' disabled=no"
	总结ansible颜色信息:
	绿色:查看远程主机信息,不会对远程主机系统做任何修改
	红色:执行操作出现异常错误
	黄色:对远程主机系统进行修改操作
	粉色:警告或者忠告信息
	
	ansible软件剧本
	编写剧本规范:
	http://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
	遵循pyyaml
	①. - 用法说明,表示列表显示的内容
	    水果信息:
		  - 苹果
		  - 香蕉
		  - 西瓜
    ②. : 用法说明:
	    姓名: 张三
		性别: 男
		人员信息:
		- 运维人员: sa
		- 开发人员: dev
		- 存储人员: dba
	③. 空格 用法说明:
	    对内容进行分级时,需要有两个空格表示分级
		软件安装步骤:
		  - 服务端安装步骤:
		    第一个里程碑: 检查软件是否安装
			第二个里程碑: 编写配置文件内容
		  - 客户端安装步骤:
	    补充:必须使用空格分隔ansible剧本级别,一定不要使用tab键进行分割
    执行脚本方法:
    ansible-playbook /etc/ansible/ansible-playbook/test.yaml
    ansible-playbook -C /etc/ansible/ansible-playbook/test.yaml
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号