美文网首页运维相关自我总结
备份文档——ansible批量管理测试

备份文档——ansible批量管理测试

作者: 如歌丨如歌 | 来源:发表于2020-02-12 20:26 被阅读0次
实现ansible批量管理,管理机需要先给被管理机分发公钥

1..原理
    基于密钥方式连接过程(原理)
    1) 客户端(管理端)     执行命令创建密钥对
    2) 客户端(管理端)     建立远程连接(口令方式),发送公钥信息
    3) 客户端(管理端)     在此建立远程连接
    4) 服务端(被管理端)        发送公钥质询信息
    5) 客户端(管理端)     处理公钥质询信息,将结果返回服务端
    6) 服务端(被管理端)        接受质询结果后,匹配,最终建立远程连接
    
2..需要安装的小工具
    yum install sshpass ----- 解决传输时,交互输入密码信息的缺陷
    
    命令结尾加上:
    "-o StrictHostKeyChecking=no" --- 解决传输时,yes/no确认的交互信息
    
3..创建密钥对与分发公钥
    [root@m01 ~]# ssh-keygen -t dsa 
    Generating public/private dsa key pair.
    Enter file in which to save the key (/root/.ssh/id_dsa):            --- 密钥对保存位置,默认
    Created directory '/root/.ssh'.
    Enter passphrase (empty for no passphrase):                         --- 不需要给私钥加密码了,为空
    Enter same passphrase again: 
    Your identification has been saved in /root/.ssh/id_dsa.
    Your public key has been saved in /root/.ssh/id_dsa.pub.
    The key fingerprint is:
    SHA256:PNX3NhrxSiUcSLR0u2Yzzj0j4V+P/2GVp3IeBpjM0gI root@m01
    The key's randomart image is:
    +---[DSA 1024]----+
    |          o+.o   |
    |          .o+ o  |
    |      E   ...=.. |
    |       o = o .*..|
    |        S * .X +=|
    |         +  B.Oo+|
    |            .B=*.|
    |             =+o*|
    |              oo=|
    +----[SHA256]-----+
    
    
    分发公钥:
    sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub root@172.16.1.41 (-p ssh端口号) "-o StrictHostKeyChecking=no"
    
4..完善分发 -- 循环命令编辑脚本
    
    #!/bin/bash
    #send_pub_key
    #lyl -- 17858282850
    #2020/1/12
    Port=22
    Password=123456
    IPh=172.16.1.
    for IPe in {101,5,6,7,8,9,31,41,51,61,72}
    do
    echo "======================================start_send: $IPh$IPe============================================"
    sshpass -p$Password ssh-copy-id -i /root/.ssh/id_dsa.pub root@$IPh$IPe -p $Port "-o StrictHostKeyChecking=no" &>/dev/null
    if [ $? -eq 0 ]
    then
    echo "success" 
    else
    echo "faile"
    fi
    done

5..安装部署ansible批量管理服务
    yum install ansible  (需要依赖epel yum源)
    
    1) ansible重要配置文件
        /etc/ansible/ansible.cfg    --- ansible服务配置文件
        /etc/ansible/hosts          --- 主机清单文件
        /etc/ansible/roles          --- 角色目录

    2) ansible主机清单配置
        vim /etc/ansible/hosts
        [web]
        172.16.1.[7:9]

        [lb]
        172.16.1.[5:6]

        [backup]
        172.16.1.41

        [nfs]
        172.16.1.31

        [zabbix]
        172.16.1.72

        [db]
        172.16.1.51

        172.16.1.101


    3) 测试
        ansible all -a "hostname"
    4) ansible常用模块
        https://docs.ansible.com/   官方网站
        
        命令行调用格式:
        ansible 主机地址/主机清单类别/all -m 模块名 -a "执行的动作/命令"
        
        (1 默认模块: commend 
            除了可以某些简单执行命令
            一些常用的命令参数:
            (1) chdir   --- 切换操作路径
            (2) creates --- 创建文件(使用file模块创建文件夹)(如果几个数据信息存在,则之后的动作不会执行)
            (3) removes --- 删除(如果文件存在,则之后的动作将执行) 
            
            注意事项: 有些符号信息无法识别
                  "<", ">", "|", ";" and "&"
            使用shell模块可以使用   -m shell
        
        (2 万能模块: shell
            扩展命令参数同command,但是各异识别 "<", ">", "|", ";" and "&" 等符号信息
        
        (3 脚本模块: scripts
            script – Runs a local script on a remote node after transferring i
            第一步: 编写一个脚本
            第二步: 运行ansible模块,执行脚本,注意!!此文件不需要执行权限!!
            
            PS: scripts模块参数类似command模块!
        
        (4 copy模块
            基本用法: 
            ansible 172.16.1.31 -m copy -a "src=/etc/hosts dest=/etc/host_bak"  ////可以用于重命名
            扩展用法: 
                01..修改属主,属组
                [root@m01 rsync]# ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ owner=oldboy group=oldboy"
                02..修改文件权限
                    "...mode=666"
                03..再传输替换文件时,对远程主机的源文件进行备份
                    "...backup=yes"
                04..创建一个文件并直接编写文件的信息
                [root@m01 rsync]# ansible 172.16.1.31 -m copy -a "content='123456' dest=/etc/rsync.password"
                
            remote_src=yes/no
            If no, it will search for src at originating/master machine.
                在本地主机,按照路径找源文件----传输                            默认为no,即本地主机的文件传输到远程主机上
            If yes it will go to the remote/target machine for the src.
                在远程主机按照路径找源文件-----移动
        
            利用copy模块移动目录: 
                [root@m01 ~]# ansible 172.16.1.31 -m copy -a "src=/tmp/oldboy dest=/tmp"
                src后面的目录没有/: 将目录本身进行传输,包括目录下的文件             #####
                [root@m01 ~]# ansible 172.16.1.31 -m copy -a "src=/tmp/oldboy/ dest=/tmp"
                src后面的目录有/ : 将目录下的所有内容进行传输,不传输目录本身
        (5 file模块   
           file – Manage files and file properties
                管理文件和文件属性
            1..基本用法: 修改属性,创建文件
                ansible 172.16.1.31 -m file -a "dest=/tmp/oldboy.txt owner=oldboy group=oldboy mode=000"
                
                扩展用法: 利用模块创建数据信息(文件 目录 链接文件)
                state参数
                =absent    : 缺席/删除数据信息
                =directory : ansible 172.16.1.31 -m file -a "dest=/tmp/oldboy01/oldboy02/oldboy03 state=directory # 可以创建多级
                =touch     : ansible 172.16.1.31 -m file -a "dest=/tmp/oldboy01/oldboy.txt state=touch
                =hard      : ansible 172.16.1.31 -m file -a "src=/tmp/oldboy01/oldboy.txt dest=/tmp/oldboy01/oldboy02/ state=hard
                =link      : 创建软连接一定要给个名字!!!
                =file      : 检测创建的数据信息是否存在
            
            2..删除数据
                ansible 172.16.1.31 -m file -a "dest=/tmp/oldboy01/oldboy02/oldboy03 state=absent" ##可删除目录
                
            3..递归参数:recures
                recurse     Recursively set the specified file attributes on directory contents.
                            This applies only when is set to .statedirectory
        (6 fetch    (批量拉取数据)    被管理端 ---> 管理端
            ansible all -m fetch -a "src=/etc/sysconfig/network-scripts/ifcfg-eth1 dest=/tmp/oldboy" 与copy重复
        (7 yum模块
            ansible 172.16.1.31 -m yum -a "name=iotop state=installed"
            安装多个软件---name=iotop,htop    用,隔开
            
            详细说明:
                state --- 指定yum模块动作
                        installed 
                        present     安装
                        latest
                        absent,removed  卸载软件
        
        (8 service模块: 管理服务器的运行状态
               name --- 指定管理的服务名称
               state    --- 指定服务状态:started restarted stopped
               enabled  --- 指定服务是否开机自启动
        (9 cron模块: 批量设置多个主机的定时任务信息     
               crontab -e :
               * * * * * 定时任务的动作信息(命令)
               
               minute:                # Minute when the job should run ( 0-59, *, */2, etc )
                                    设置分信息
               hour:                  # Hour when the job should run ( 0-23, *, */2, etc )
                                    设置时信息
               day:                   # Day of the month the job should run ( 1-31, *, */2, etc )
                                    设置天信息
               month:                 # Month of the year the job should run ( 1-12, *, */2, etc )
                                    设置月信息
               weekday:               # Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )
                                    设置周信息
               job:                   # The command to execute or, if env is set, the value of environment variable. The command should not
                                       contain line breaks. Required if `state=present'.
                                    用于定义定时任务需要干的事
               
               命令格式:
               基本用法: 
               ansible 172.16.1.31 -m cron -a "minute=0 hour=2 job='/usr/bin/ntpdate ntp1.aliyun.com &>/dev/null'"
               PS: 默认的每天每月可以不写!!!
            
            扩展用法: 
            1) 避免反复操作出现多个相同定时任务  并添加描述信息
                ansible 172.16.1.31 -m cron -a "name='time sync with aliyun' minute=0 hour=2 job='/usr/bin/ntpdate ntp1.aliyun.com &>/dev/null'"
            
            2) 如何删除指定定时任务
                ansible 172.16.1.31 -m cron -a "name='time sync with aliyun' state=absent"
                注意事项: ansible可以删除的定时任务,只能是ansible设置好的定时任务,手动设置的ansible删除不了
                
            3) 如何批量注释定时任务
                ansible 172.16.1.31 -m cron -a "name='time sync with aliyun' disabled=yes"
                : 报错,提示需要加上job信息
                
                修改后:
                    ansible 172.16.1.31 -m cron -a "name='time sync with aliyun' minute=0 hour=2 job='/usr/bin/ntpdate ntp1.aliyun.com &>/dev/null' enabled=yes"
                    
                取消注释:
                    ansible 172.16.1.31 -m cron -a "name='time sync with aliyun' minute=0 hour=2 job='/usr/bin/ntpdate ntp1.aliyun.com &>/dev/null' disabled=no"
        (10 mount模块 --- 批量挂载操作          
                mount – Control active and configured mount points
                
                常用参数:
                    src                 ---- 需要挂载的存储设备或文件信息
                    path                ---- 需要挂载的目录
                    fstype              ---- 指定挂在时的系统文件类型
                    state: 
                        参数选项:
                            present/mounted     ---- 进行挂载
                                present: 不会实现立即挂载,修改fstab文件,实现开机自动挂载!!!
                                
                                mounted: 立即挂载,并写入fstab文件
                            
                            absent/unmounted    ---- 进行卸载
                                absent:     会立即卸载,并删除fstab文件中的信息
                                unmounted:  立即卸载,但不会删除文件中的信息
        (11 user模块: 批量创建用户模块
            基本用法: ansible 172.16.1.31 -m user -a "name=oldboy"
            
            扩展用法: 指定用户uid信息
                        name=oldboy01 uid=6666
             
                        
                      指定用户组和从属组信息
                        group=  groups=
                        
                      
                      批量创建虚拟用户
                        是否创建家目录参数: create_home=   true & false 默认true
                        指定shell信息:      shell=/sbin/nologin
                        
                      设置用户密码
                        使用password参数设置密码,password=123456----此时为明文密码信息
                        
                        生成密文密码信息
                            1..ansible all -i localhost, -m debug -a "msg={{ 'mypassword' | password_hash('sha512', 'mysecretsalt') }}"
                                mysecretsalt --- 加密参考值(加密校验信息)
                        
                            2..利用python功能实现
                                python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
                        
                      
                        注意: 使用此方法生产密文密码时,需要将"",改为'',因为密文密码信息里面有$符号
                        
6..ansible剧本编写
    1) 剧本的编写规范: pyyaml格式 --- 三点要求
                    1..合理的信息缩进: 缩进两个空格
                        标题一
                          标题二
                            标题三
                PS: 不能用tab键进行缩进
                
                    2..冒号的使用方法 :
                        hosts: 172.16.1.41
                        tasks:
                        yum: name=xxx
                        
                PS: 冒号要有空格,但是出现在注释说明和结尾中(一行的尾部),不需要加空格
                
                    3..短横线的应用 - 代表列表功能
                        - 张三
                          男
                            打游戏
                              湖北
                        - 李四
                          女
                            逛街
                              湖南
                        - 王五
                          男
                            看电影
                              深圳
       
                PS: 使用短横线生产列表信息,短横线后面要加空格
    (2 剧本编写实例
        - hosts: 主机名
          tasks:
            yum: name=rsync state=installed
            copy: src=/tmp/rsyncd.conf dest=/etc/
            
        如何执行剧本:
        ansible-playbook rsync_server.yaml
            第一步: 检查剧本的语法格式对不对
            ansible-playbook --syntax-check rsync_server.yaml
                             --syntax-check        perform a syntax check on the playbook, but do not
                            execute it
                            检查语法格式
            第二步: 彩排,模拟执行剧本
            ansible-playbook -C rsync_server.yaml
                
            第三步: 运行剧本
            ansible-playbook rsync_server.yaml
            
            完善剧本:
            1..添加注释信息
                - hosts: 172.16.1.41
                  tasks:
                    - name: 01-install rsync                    ###添加注释信息
                      yum: name=rsync state=installed
                    - name: 02-push conf file                   ###添加注释信息
                      copy: src=/tmp/rsyncd.conf dest=/etc/

相关文章

网友评论

    本文标题:备份文档——ansible批量管理测试

    本文链接:https://www.haomeiwen.com/subject/xmzcfhtx.html