zoukankan      html  css  js  c++  java
  • Ansible简介及常用模块

    1.Ansible核心组件

    • Ansible:核心
    • Host inventory:用来所定义远程管理的主机(端口 密码 IP 等)
    • Core Modules:核心模块(所进行的工作都是通过调用模块来进行的)
    • playbook(yaml):剧本执行多个任务时,非必需可以让节点一次性运行多个任务。
    • 借助于插件完成记录日志邮件等功能

    2.Ansible的特性

    基于Python语言实现,由Paramiko,PyYAML和jinjia2三个关键模块;部署简单;默认使用ssh协议;基于“模块”完成各种“任务”

    3.安装Ansible和使用

    1.安装
    [root@ansible-server ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    
    [root@ansible-server ~]# yum -y install epel-release
    
    [root@ansible-server ~]# yum -y install ansible
    
    2.生成密钥对;将公钥传给被管理的主机
    [root@ansible-server ~]# ssh-keygen -t rsa
    
    [root@ansible-server ~]# ssh-copy-id root@192.168.100.103
    

    3.1生成的配置文件

    • /etc/ansible/ansible.cfg

    主配置文件

    • /etc/ansible/hosts

    定义主机清单的配置文件

    3.2定义主机清单

    • 定义格式(常用)

      [webservers] #定义的组名;此组下的主机都生效
      alpha.example.org #可以定义主机名
      beta.example.org
      192.168.1.100 #也可以定义IP地址
      192.168.1.110

      [webservers]
      192.168.100.101
      192.168.100.102
      192.168.100.103

    • 使用通配符定义多个主机

      db-[99:101]-node.example.com #表示:db-99-node.example.com db-100-node.example.com db-101-node.example.com

    • 例子

      [root@ansible-server ~]# cat /etc/ansible/hosts
      [webserve]
      192.168.100.103
      [dbserver]
      192.168.100.104

    4.使用前准备

    • 在ansible服务器中建立密钥对;并将公钥传送到被管理主机的节点上

    • 编辑主机清单

    • 进行管理使用

    • 查看模块的帮助

      ansible-doc -l
      ansible-doc -s

    4.1ansible命令应用基础语法

    ansible <IP;或者是主机清单的组名;all代表所有组> [-m 模块] [-a args]
    	-f forks:启动的并发线程
    	-m 要使用的模块
    	-a agrs:模块的参数
    
    • 例子
        1.all 选项 代表所有的主机组
        [root@ansible-server ~]# ansible all -m ping
        192.168.100.104 | SUCCESS => {
            "changed": false, 
            "ping": "pong"
        }
        192.168.100.103 | SUCCESS => {
            "changed": false, 
            "ping": "pong"
        }
        
        2.指定IP
        [root@ansible-server ~]# ansible 192.168.100.103 -m ping
        192.168.100.103 | SUCCESS => {
            "changed": false, 
            "ping": "pong"
        }
        
        3.指定主机组
        [root@ansible-server ~]# ansible webserver -m ping
        192.168.100.103 | SUCCESS => {
            "changed": false, 
            "ping": "pong"
        }
    
    

    5.模块的使用

    5.1 commend

    commend命令模块;是默认的模块;在被管理的主机上运行命令

    1.使用-m 选项 指定commend模块
    [root@ansible-server ~]# ansible all -m command -a 'date'
    192.168.100.103 | CHANGED | rc=0 >>
    2019年 04月 23日 星期二 15:58:29 CST
    
    192.168.100.104 | CHANGED | rc=0 >>
    2019年 04月 23日 星期二 15:58:29 CST
    
    2.默认就是commend模块 不用指定 直接用 -a选项也可以
    [root@ansible-server ~]# ansible all  -a 'date'
    192.168.100.103 | CHANGED | rc=0 >>
    2019年 04月 23日 星期二 15:58:34 CST
    
    192.168.100.104 | CHANGED | rc=0 >>
    2019年 04月 23日 星期二 15:58:34 CST
    

    5.2 cron

    计划任务模块;将执行计划任务添加到被管理的主机中

    参数

    • day=tian hour=小时
    • minute=分钟
    • name=名字
    • job=执行什么事
    • state=安装或者是移除(absent移除;默认就是安装)
        [root@ansible-server ~]# ansible 192.168.100.103 -m cron -a 'minute="*/2" job="/usr/bin/echo Hello Ansible" name="Test Ansible Cron"'
        
        移除:
        
        [root@ansible-server ~]# ansible 192.168.100.103 -m cron -a 'name="Test Ansible Cron" state=absent' 
        指定添加时的任务名字;并使用“state=absent”参数移除
    

    5.3 user

    创建用户模块

    参数

    • name 指定用户名
    • password 指定密码
    • home 指定家目录
    • state 指定删除(absent)
    
        1.创建
        [root@ansible-server ~]# ansible all -m user -a 'name="user1"'
    
        2.删除
        [root@ansible-server ~]# ansible all -m user -a 'name="user1" state=absent'
    

    5.3 group

    创建组的模块
    参数

    • name 指定组名
    • gid 指定gid号
    • system 指定是否为系统组
        1.创建
        [root@ansible-server ~]# ansible all -m group -a 'name="mysql" gid=3306 system=yes'
        
        2.删除
        [root@ansible-server ~]# ansible all -m group -a 'name="mysql" state=absent'
    

    5.4 copy

    可以复制本机到远程主机的文件,或者是可以直接写内容

    参数

    • src 本地文件的路径
    • dest 远程主机的路径
    • mode 复制到被管理主机;文件所属的权限
    • owner 复制到被管理主机 属主
    • group 复制到被管理主机 属组
    • content 要写的内容
        
        1.复制文件;属主和属组是zs;权限是640
        [root@ansible-server ~]# ansible all -m copy -a 'src=/etc/fstab dest=/test/ owner=zs group=zs mode=640'
    

    5.5 file

    文件设置属性模块

    参数

    • owner 属主
    • group 属组
    • mode 权限
    • dest 被远程主机的文件
        [root@ansible-server ~]# ansible all -m file -a 'owner=root dest=/test/fstab'
    

    5.6ping

    用来ping测试的模块

    [root@ansible-server ~]# ansible all -m ping
    192.168.100.103 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    192.168.100.104 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    

    5.7 service

    服务管理启动模块

    参数

    • name 服务名字

    • runlevel 运行级别( 3 5 )

    • enabled 开机启动状态(on off)

    • state 是否启动(reloaded, restarted, started, stopped)

      [root@ansible-server ~]# ansible all -m service -a 'name=httpd state=started enabled=on runlevel=35'

    5.8 shell

    运行命令模块;如果命令中有管道符;就要用此模块

    ansible all -m shell -a 'echo 123.com | passwd --stdin u01'
    

    5.9 script

    将本地脚本复制到远程主机并执行

    [root@ansible-server ~]# ansible all -m script -a 'test.sh '
    192.168.100.104 | CHANGED => {
        "changed": true, 
        "rc": 0, 
        "stderr": "Shared connection to 192.168.100.104 closed.
    ", 
        "stderr_lines": [
            "Shared connection to 192.168.100.104 closed."
        ], 
        "stdout": "Hello Ansible
    ", 
        "stdout_lines": [
            "Hello Ansible"
        ]
    }
    192.168.100.103 | CHANGED => {
        "changed": true, 
        "rc": 0, 
        "stderr": "Shared connection to 192.168.100.103 closed.
    ", 
        "stderr_lines": [
            "Shared connection to 192.168.100.103 closed."
        ], 
        "stdout": "Hello Ansible
    ", 
        "stdout_lines": [
            "Hello Ansible"
        ]
    }
    
    脚本一点要在当前目录下
    

    5.10 yum

    安装程序和卸载程序模块
    
    **参数**
    - name 程序名
    - state (absent=卸载;默认就是安装;installed安装)
    - update_cache	 安装软件包前更新缓存
    - enablerepo 指定yum源名
    
    1.安装
        [root@ansible-server ~]# ansible all -m yum -a 'name=lftp state=installed'
        
        2.卸载
        [root@ansible-server ~]# ansible all -m yum -a 'name=lftp state=absent'
        192.168.100.104 | CHANGED => {
            "ansible_facts": {
                "pkg_mgr": "yum"
            }, 
            "changed": true, 
            "msg": "", 
            "rc": 0, 
            "results": [
                "已加载插件:fastestmirror
    正在解决依赖关系
    --> 正在检查事务
    ---> 软件包 lftp.x86_64.0.4.4.8-8.el7_3.2 将被 删除
    --> 解决依赖关系完成
    
    依赖关系解决
    
    ================================================================================
     Package       架构            版本                       源               大小
    ================================================================================
    正在删除:
     lftp          x86_64          4.4.8-8.el7_3.2            @local          2.4 M
    
    事务概要
    ================================================================================
    移除  1 软件包
    
    安装大小:2.4 M
    Downloading packages:
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      正在删除    : lftp-4.4.8-8.el7_3.2.x86_64                                 1/1 
      验证中      : lftp-4.4.8-8.el7_3.2.x86_64                                 1/1 
    
    删除:
      lftp.x86_64 0:4.4.8-8.el7_3.2                                                 
    
    完毕!
    "
            ]
        }
        192.168.100.103 | CHANGED => {
            "ansible_facts": {
                "pkg_mgr": "yum"
            }, 
            "changed": true, 
            "msg": "", 
            "rc": 0, 
            "results": [
                "已加载插件:fastestmirror
    正在解决依赖关系
    --> 正在检查事务
    ---> 软件包 lftp.x86_64.0.4.4.8-8.el7_3.2 将被 删除
    --> 解决依赖关系完成
    
    依赖关系解决
    
    ================================================================================
     Package       架构            版本                       源               大小
    ================================================================================
    正在删除:
     lftp          x86_64          4.4.8-8.el7_3.2            @local          2.4 M
    
    事务概要
    ================================================================================
    移除  1 软件包
    
    安装大小:2.4 M
    Downloading packages:
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      正在删除    : lftp-4.4.8-8.el7_3.2.x86_64                                 1/1 
      验证中      : lftp-4.4.8-8.el7_3.2.x86_64                                 1/1 
    
    删除:
      lftp.x86_64 0:4.4.8-8.el7_3.2                                                 
    
    完毕!
    "
            ]
    
    

    5.11 setup

    [root@ansible-server ~]# ansible all -m setup
    收集被管理主机的信息 ip地址 cup核心数 版本

    网站:

    https://mp.weixin.qq.com/s?__biz=MzAwNTM5Njk3Mw==&mid=2247484668&idx=1&sn=3b8f2ef4febc6c27f5ed33bc449564c9&chksm=9b1c047eac6b8d68f912bd93110ca54796cd42c7b7acd6ebfa0d9439420c3f341a1c136ca3f1&mpshare=1&scene=23&srcid=1224fYCGJvnBjEgMaino3YkP#rd==
    

    5.12 cron

    功能:计划任务
    支持时间:minute,hour,day,month,weekday

    
    #备份数据库脚本
    [root@centos8 ~]#cat mysql_backup.sh
    mysqldump -A -F --single-transaction --master-data=2 -q -uroot |gzip >
    /data/mysql_`date +%F_%T`.sql.gz
    #创建任务
    ansible 10.0.0.8 -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql"
    job=/root/mysql_backup.sh'
    ansible websrvs   -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1
    &>/dev/null' name=Synctime"
    #禁用计划任务
    ansible websrvs   -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1
    &>/dev/null' name=Synctime disabled=yes"
    #启用计划任务
    ansible websrvs   -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1
    &>/dev/null' name=Synctime disabled=no"
    #删除任务
    ansible websrvs -m cron -a "name='backup mysql' state=absent"
    ansible websrvs -m cron -a 'state=absent name=Synctime'
    
    

    5.13 Lineinfile模块

    ansible在使用sed进行替换时,经常会遇到需要转义的问题,而且ansible在遇到特殊符号进行替换时,
    存在问题,无法正常进行替换 。其实在ansible自身提供了两个模块:lineinfile模块和replace模块,可
    以方便的进行替换

    功能:相当于sed,可以修改文件内容

    
    ansible all -m   lineinfile -a "path=/etc/selinux/config regexp='^SELINUX='
    line='SELINUX=disabled'"
    ansible all -m lineinfile  -a 'dest=/etc/fstab state=absent regexp="^#"'
    
    

    5.14 Replace模块

    该模块有点类似于sed命令,主要也是基于正则进行匹配和替换,建议使用

    ansible all -m replace -a "path=/etc/fstab regexp='^(UUID.*)' replace='#1'"  
    ansible all -m replace -a "path=/etc/fstab regexp='^#(.*)' replace='1'"
    
  • 相关阅读:
    01_Linux基础篇
    Docker
    Day02_IP地址详解&进制转换&DOS基本命令与批处理
    Day01_虚拟化架构与系统部署
    重学TCP/IP协议和三次握手四次挥手
    作为一个程序员,CPU的这些硬核知识你必须会!
    通过docker-compose制作dubbo-admin和zookeeper组合服务
    双主master-master复制Err 1677故障分析
    唐宇迪-人工智能学习路线(上篇)
    DNS访问原理只需9个步骤
  • 原文地址:https://www.cnblogs.com/precipitation/p/15098158.html
Copyright © 2011-2022 走看看