Ansible部署和常见模块使用方法

ansible批量管理服务介绍

1、ansible批量管理服务意义
    01. 提高工作效率
    02. 提高工作准确度
    03. 减少维护的成本
    04. 减少重复性工作
    
2、ansible批量管理服务功能
    01. 可以实现批量系统操作配置
    02. 可以实现批量软件服务部署
    03. 可以实现批量文件数据分发
    04. 可以实现批量系统信息收集
    
3、ansible批量管理服务部署

    一、用脚本将控制端秘钥分发到被控端主机

   分发公钥脚本:

    将要分发的ip写入ip.txt中
    #!/bin/bash
    for ip in `cat ip.txt`
    do
        echo "=========host $ip pub-key is start=========="
        sshpass -pqweasd ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.16.1.$ip "-o StrictHostKeyChecking=no " &> /dev/null
        if [ $? -eq 0 ];then
            echo "host $ip is successed"
        fi
        echo "=========host $ip pub-key is end==========="
    done
    二、安装部署软件
    yum -y install ansible            --需要依赖epel的yum源
    
    /etc/ansible/ansible.cfg        --ansible服务配置文件
    /etc/ansible/hosts                --主机清单文件
    /etc/ansible/roles                --角色目录
    
    三、需要编写主机清单文件
    vi /etc/ansible/hosts
    
    四、测试是否可以管理多个主机
    脚本 hostname
    ansible all -a "command" 
    [root@m01 scripts]# ansible all -a "hostname"
    172.16.1.41 | CHANGED | rc=0 >>
    backup01
    172.16.1.8 | CHANGED | rc=0 >>
    web02
    172.16.1.9 | CHANGED | rc=0 >>
    web03
    172.16.1.31 | CHANGED | rc=0 >>
    nfs01
    172.16.1.7 | CHANGED | rc=0 >>
    web01
    
4、ansible服务架构信息
    1)主机清单配置
    2)软件模块信息
    3)基于秘钥连接主机
    4)主机需要关闭selinux
    5)软件剧本功能
    
5、ansible软件模块应用
    ansible官网:https://docs.ansible.com/
    模块的应用:
    ansible 主机名称/主机组名称/主机地址信息/all -m(指定应用的模块信息) 模块名称 -a(指定动作信息) "执行的命令"
    
第一个模块: command(默认)
    command — Executes a command on a remote node
            在一个远程主机上执行一个命令
    简单用法:
    [root@m01 scripts]# ansible 172.16.1.31 -m command -a "hostname"
    172.16.1.31 | CHANGED | rc=0 >>
    nfs01
    
    扩展应用:
    1)chdir - Change into this directory before running the command.
                在执行命令之前对目录进行切换
    ansible 172.16.1.31 -m command -a "chdir=/tmp touch file1.txt"
    
    2)creates - If it already exists,this step wom't be run.
                如果文件已存在,不执行命令操作
    [root@m01 scripts]# ansible 172.16.1.31 -m command -a "creates=/tmp/file1.txt touch file1.txt"
    172.16.1.31 | SUCCESS | rc=0 >>
    skipped, since /tmp/file1.txt exists
    
    3)removes - If it already exists,this setp will be run.
                如果文件已存在,这个步骤将执行
    [root@m01 scripts]# ansible 172.16.1.31 -m command -a "removes=/tmp/file1.txt touch file1.txt"
    172.16.1.31 | CHANGED | rc=0 >>
    
    4)free_form(required)
    The command module takes a free form command to run.
    There is no parameter actually nam ed 'free form'. See the examples!
    使用command模块的时候,-a参数后面必须写上一个合法的linux命令信息
 
第二个模块:shell(万能模块)
    shell    - Execute commands in nodes
              在节点上执行操作
    简单用法:
    [root@m01 ~]# ansible 172.16.1.31 -m shell -a "hostname"
    172.16.1.31 | CHANGED | rc=0 >>
    nfs01
    
    实践应用:利用shell执行脚本
    1、编写一个脚本
    2、将脚本发送的远程主机
    3、将脚本权限进行修改(增加执行权限)
    4、运行ansible命令执行脚本
    
第三个模块:script(脚本模块)
    1、编写一个脚本 
    2、运行ansible命令执行脚本
    
    PS:script模块参数功能和commadn模块类似
    
文件类型模块:

    copy    - Copies files to remote locations
              将数据信息进行批量分发
              
    基本用法:
    ansible 172.16.1.31 -m copy -a "src=/etc/hosts dest=/etc/hosts_bak"
    
    扩展用法:
    1、传输文件时修改属主和属组的信息
    ansible 172.16.1.31 -m copy -a "src=/etc/hosts dest=/etc/hosts_bak owner=oldboy group=oldboy"
    2、在传输文件时修改文件权限信息
    ansible 172.16.1.31 -m copy -a "src=/etc/hosts dest=/etc/hosts_bak mode=777"    
    3、在传输文件时对文件先进行备份
    ansible 172.16.1.31 -m copy -a "src=/etc/hosts dest=/etc/hosts_bak backup=yes"
    4、传输一个文件并编辑文件的信息
    ansible 172.16.1.31 -m copy -a "content='oldboy123' dest=/etc/rsync.password"
    5、remote_src
    If no, it will search for src at originating/master machine.
            src参数指定文件信息,会在本地管理端服务进行查找
    if yes, it will go to the remote/target machine for the src. Default is no.
            src参数指定文件信息,会从远程主机上进行查找
            
    PS: ansible软件copy模块复制目录信息
    ansible 172.16.1.31 -m copy -a "src=/oldboy /dest/=/oldboy"
    src后面目录没有/: 将目录本身以及目录下面的内容都进行远程传输复制
    ansible 172.16.1.31 -m copy -a "src=/oldboy/ /dest/=/oldboy"
    src后面目录有/:   只将目录下面的内容都进行远程传输复制
    
    file - Sets attributes of files
           设置文件属性信息
    基本用法:
    ansible 172.16.1.31 -m file -a "dest=/etc/hosts owner=oldboy group=oldboy mode=666"
            
    扩展用法:
    1、可以利用模块创建数据信息( 文件 目录 连接文件 )
    state 参数
    =absent        ---缺席/删除数据信息
        ansible 172.16.1.31 -m file -a "dest=/oldboy/oldboy.txt state=absent"
        ansible 172.16.1.31 -m file -a "dest=/oldboy/ state=absent"
        
    =directory    ---创建一个目录信息
        ansible 172.16.1.31 -m file -a "dest=/oldboy state=directory"
        ansible 172.16.1.31 -m file -a "dest=/oldboy/oldboy01/oldboy02 state=directory"

    =file        ---检测创建的数据信息是否存在

    =hard        ---创建一个硬链接文件
        ansible 172.16.1.31 -m file -a "src=/oldboy/oldboy.txt dest=/oldboy_hard.txt state=hard"
    
    =link        ---创建一个软连接文件
        ansible 172.16.1.31 -m file -a "src=/oldboy/oldboy.txt dest=/oldboy_hard.txt state=link"
    
    =touch        ---创建一个文件信息
        ansible 172.16.1.31 -m file -a "dest=/oldboy/oldboy.txt state=touch"

    =recurse    ---递归修改目录下的属组和属主信息
        ansible 172.16.1.31 -m file -a "dest=/oldboy  owner=oldboy recurse=yes"
        
    fetch - 将数据从被控端拉取过来控制端
    
    基本用法:
    ansible 172.16.1.31 -m fetch -a "src=/etc/hosts dest=/tmp"
    
yum模块
    name    ---指定安装软件名称
    state    ---是否安装软件
                installed        ---安装软件
                present            
                latest
                absent            ---卸载软件
                removed
    ansible 172.16.1.31 -m yum -a "name=iotop state=installed"
    
    service模块:管理服务器的运行状态    停止    开启    重启
    name:        ---指定管理的服务名称
    state:        ---指定服务状态
                    started        启动
                    restarted    重启
                    stopped        停止
    enabled        ---指定服务是否开机自启动
    ansible 172.16.1.31 -m service -a "name=nfs state=started enabled=yes"

cron模块:批量设置多个主机的定时任务信息
    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:        用于定义定时任务需要做的事情
    
    基本用法:
    ansible 172.16.1.31 -m cron -a "minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'"
    
    扩展用法:
    01.给定时任务设置注释信息
    ansible 172.16.1.31 -m cron -a "name='time sync' minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'"
    
    02.删除指定定时任务
    ansible 172.16.1.31 -m cron -a "name='time sync' state=absent"
    PS:ansible可以删除的定时任务,只能是ansible设置好的定时任务
    
    03.批量注释定时任务
    ansible 172.16.1.31 -m cron -a "name='time sync' job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1' disabled=yes"
    
mount模块:批量进行挂载操作
        src:需要挂载的存储设备或文件信息
        path:指定目标挂载点目录
        fstype:指定挂载时的文件系统类型
        state:
            present/mounted        ---进行挂载
            present:不会实现立即挂载,实现开机自动挂载
            mounted:会实现立即挂载,并且会修改fstab文件,实际开机自动挂载    *****
            
            absent/unmounted    ---进行卸载
            absent:会实现立即卸载,并且会删除fstab文件,禁止开机自动挂载
            unmounted:会实现立即卸载,但是不会删除fstab文件                *****
            
user模块:实现批量创建用户
    基本用法:
    ansible 172.16.1.31 -m user -a "name=oldboy01"
    
    扩展用法:
    1)指定用户UID
    ansible 172.16.1.31 -m user -a "name=oldboy01 uid=666" 
    
    2)指定用户组信息
    ansible 172.16.1.31 -m user -a "name=oldboy03 group=oldboy3"
    ansible 172.16.1.31 -m user -a "name=oldboy04 groups=oldboy2"
    
    3)创建虚拟用户
    ansible 172.16.1.31 -m user -a "name=rsync create_home=no shell=/sbin/nologin"
    
    4)给指定用户创建密码
    PS:利用ansible程序user模块设置用户密码信息,需要将密码明文转换文密文进行设置
    一、生成密文密码信息:
    ansible all -i localhost, -m debug -a "msg={{ '密码qweasd' | password_hash('sha512','加密校验信息123') }}"
    二、创建用户并给予密码
    ansible 172.16.1.31 -m user -a 'name=oldboy08 password=$6$123$90hLUJ33QF7tP7CByH96s4RzRgV6f2KIUQYpKE.Ku9x0uhJV/CTt7isTYS9rR79Or4L3PJulhz9I3adDQCIkk0'
    
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值