美文网首页
CentOS7 Ansible自动化运维

CentOS7 Ansible自动化运维

作者: 老哥很稳 | 来源:发表于2020-07-04 16:51 被阅读0次

    企业级自动化运维神器Ansible

    一、介绍

    1.自动化运维工具对比

    1.Puppet:基于 Ruby 开发,采用 C/S 架构,扩展性强,基于 SSL,远程命令执行相对较弱
    2.SaltStack:基于 Python 开发,采用 C/S 架构,相对 puppet 更轻量级,配置语法使用 YAML,使得配置脚本更简单.需要配置客户端以及服务器端。每台被控制节点需要安装agent
    3.Ansible:基于Python开发,分布式,无需客户端,轻量级,配置语法使用YAML语言,更强的远程命令执行操作
    

    2.ansible简介

    ansible是新出现的自动化运维工具,基于Python开发,分布式,无需客户端,轻量级,实现了批量系统配置、批量程序部署、批量运行命令等功能,ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。

    Ansible特性
        1)、no agents:不需要在被管控主机上安装任何客户端,更新时,只需在操作机上进行一次更新即可(不用安装客户端。分布式的)
        2)、no server:无服务器端,使用时直接运行命令即可
        3)、modules in any languages:基于模块工作,可使用任意语言开发模块
        4)、yaml,not code:使用yaml语言定制剧本playbook
        5)、ssh by default:基于SSH工作
        6)、strong multi-tier solution:可实现多级指挥
    
    image.png
     connection plugins:连接插件,负责和被监控端实现通信,默认使用SSH连接
     host inventory:主机清单,是一个配置文件里面定义监控的主机
     modules : 模块,核心模块、command模块、自定义模块等
     plugins : modules功能的补充,包括连接插件,邮件插件等
     playbook:编排,定义 Ansible 多任务配置文件,非必需
    

    二、ansible安装

    1、 准备环境----关闭防护墙和selinux

    环境:
    主机:3台  1个控制节点 2个被控制节点
    解析:本地互相解析(所有机器)
    # vim /etc/hosts
    192.168.91.133 ansible-web1
    192.168.91.132 ansible-web2
    192.168.91.134 ansible-server  (控制节点服务器端)
    配置ssh公钥认证:控制节点需要发送ssh公钥给所有被控制节点
    [root@ansible-server ~]# ssh-keygen
    [root@ansible-server ~]# ssh-copy-id -i 192.168.1.10  #所有被控节点机器
    
    所有机器:
    systemctl stop firewalld && setenforce 0
    

    2、安装

    
    安装:控制节点
     1. 配置EPEL网络yum源
     [root@ansible-server ~]# yum install -y epel-release
     2. 安装ansible
     [root@ansible-server ~]# yum install -y ansible
     3.查看版本
     [root@ansiable-server ~]# ansible --version
    ansible 2.9.10
      config file = /etc/ansible/ansible.cfg
      configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
      ansible python module location = /usr/lib/python2.7/site-packages/ansible
      executable location = /usr/bin/ansible
      python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
    
    4.看帮助
     [root@ansible-server ~]# ansible --help
    

    3、ansible基础----inventory主机清单

    官方文档: http://docs.ansible.com/ansible/intro_inventory.html#>

    inventory文件通常用于定义要管理主机的认证信息,例如ssh登录用户名、密码以及key相关信息。

    查看配置文件:
    [root@ansible-server ~]# rpm  -qc ansible
    /etc/ansible/ansible.cfg
    /etc/ansible/hosts
    -q:---query查询
    1.主配置文件:
    /etc/ansible/ansible.cfg  #主要设置一些ansible初始化的信息,比如日志存放路径、模块、等配置信息
    2.主机清单文件:
    默认位置/etc/ansible/hosts
    
    语法:
    1.添加主机或者主机组:
    [root@ansible-server ~]# vim /etc/ansible/hosts  #在最后追加被管理端的机器
    ansible-web1                      #单独指定主机,可以使用主机名称或IP地址
    2.添加主机组:
    [webservers]        #使用[]标签指定主机组 ----标签自定义
    192.168.91.133        #如果未解析添加ip 这里使用web1作为例子
    ansible-web2      #解析添加主机名
    3.组可以包含其他组:
    [webservers1]     #组一
    ansible-web1
    [webservers2]     #组二
    ansible-web2
    [weball:children]      #children-照写 #weball包括两个子组
    webservers1        #组一
    webservers2        #组二
    4.为一个组指定变量,组内每个主机都可以使用该变量:
    [weball:vars]         #设置变量,vars--照写
    ansible_ssh_port=22     
    ansible_ssh_user=root   
    ansible_ssh_private_key_file=/root/.ssh/id_rsa  
    #ansible_ssh_pass=test      #也可以定义密码,如果没有互传秘钥可以使用密码。
    
    

    查看组内主机列表:

    [root@ansible-server ~]# ansible weball --list-hosts
      hosts (2):
        ansible-web1
        ansbile-web2
    

    自定义主机列表使用密码登录:

    [root@ansible-server ~]# vim /opt/hostlist
    [all:vars]
    ansible_ssh_port=22
    ansible_ssh_user=root
    #ansible_ssh_private_key_file=/root/.ssh/id_rsa
    ansible_ssh_pass=test
    
    [all]
    ansible-web1
    ansible-web2
    使用:
    [root@ansible-server ~]# ansible -i /opt/hostlist all -m ping -o
    小注释:如果不通,手动连接第一次,第一次需要手动输入密码。"第一次"
    -i:指定清单文件
    注意:这里的ping并不是真正意义上的ping而是探测远程主机ssh是否可以连接!判断ssh端口是否存活
    

    4、测试

    语法:

    # ansible  <pattern>  -m  <module_name>  -a  <arguments>
    
    pattern--主机清单里定义的主机组名,主机名,IP,别名等,all表示所有的主机,支持通配符,正则
    -m module_name: 模块名称,默认为command
    -a arguments: 传递给模块的参数
    -o  横着显示(单行显示)
    
    1.使用ping模块检查ansible节点的连通性:

    指定单台机器:

    [root@ansible-server ~]# ansible ansible-web1 -m ping -o
    ansible-web1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
    

    同时指定多台机器:

    [root@ansible-server ~]# ansible ansible-web1,ansible-web2 -m ping -o
    ansible-web1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
    ansible-web2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
    

    指定组名:

    [root@ansible-server ~]# ansible webservers -m ping -o
    ansible-web2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
    192.168.91.133 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
    
    shell命令

    执行shell命令:

    [root@ansible-server ~]# ansible webservers1 -m shell -a 'uptime'
    ansible-web1 | CHANGED | rc=0 >>
     11:28:05 up  1:09,  5 users,  load average: 0.10, 0.12, 0.08
    如果不添加-m 则默认为command
    [root@ansible-server ~]# ansible webservers1 -a 'uptime'
    ansible-web1 | CHANGED | rc=0 >>
     11:28:32 up  1:09,  5 users,  load average: 0.07, 0.11, 0.08
    

    给控制的节点添加用户:

    
    [root@ansible-server ~]# ansible webservers1 -m shell -a 'useradd tom'
    ansible-web1 | CHANGED | rc=0 >>
    
    [root@ansible-server ~]# ansible webservers1 -m shell -a 'grep tom /etc/passwd'
    ansible-web1 | CHANGED | rc=0 >>
    tom:x:1001:1001::/home/tom:/bin/bash
    
    重定向输出到本地文件中:
    [root@ansible-server ~]# ansible webservers1 -m shell -a 'df -Th' > /opt/a.txt
    
    [root@ansible-server ~]# cat /opt/a.txt 
    ansible-web1 | CHANGED | rc=0 >>
    Filesystem     Type      Size  Used Avail Use% Mounted on
    /dev/sda3      xfs        28G  4.6G   23G  17% /
    devtmpfs       devtmpfs  474M     0  474M   0% /dev
    tmpfs          tmpfs     489M     0  489M   0% /dev/shm
    tmpfs          tmpfs     489M  7.4M  481M   2% /run
    tmpfs          tmpfs     489M     0  489M   0% /sys/fs/cgroup
    /dev/sda1      xfs       297M  157M  141M  53% /boot
    tmpfs          tmpfs      98M  4.0K   98M   1% /run/user/42
    tmpfs          tmpfs      98M   28K   98M   1% /run/user/0
    

    Ad-Hoc

    ad hoc其实就是执行简单的命令——一条命令。对于复杂的命令则为 playbook。

    帮助文档:
    列出ansible支持的模块:
    -l:获取列表
    -s module_name:获取指定模块的使用信息
    看所有模块(A10,华为,docker,EC2,aws等等广大厂商设备)
    [root@ansible-server ~]# ansible-doc -l
    查看模块使用信息,了解其功能:
    [root@ansible-server ~]# ansible-doc -s yum
    

    常用模块:

    1.远程复制备份模块:copy
    模块参数详解:  
    src=:指定源文件路径
    dest=:目标地址(拷贝到哪里)
    owner:指定属主
    group:指定属组
    mode:指定权限,可以以数字指定比如0644
    backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no
    
    [root@ansible-server ~]# vim a.txt
    [root@ansible-server ~]# cat a
    anaconda-ks.cfg  a.txt            
    [root@ansible-server ~]# cat a.txt 
    123321
    
    [root@ansible-server ~]# ansible weball -m copy -a 'src=/root/a.txt dest=/opt owner=root group=root mode=644' -o 
    
    将在根目录下的a.txt发送给包含在weball内的所有主机的opt目录下,属组和属主为root,所给权限为644
    
    ansible-web1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "c2dad34b9c4899c4400017e3970429952dae5856", "dest": "/opt/a.txt", "gid": 0, "group": "root", "md5sum": "065fab9f9da81fe06d67c5c279262f83", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:usr_t:s0", "size": 8, "src": "/root/.ansible/tmp/ansible-tmp-1593833682.44-85619-149360861130386/source", "state": "file", "uid": 0}
    ansible-web2 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "c2dad34b9c4899c4400017e3970429952dae5856", "dest": "/opt/a.txt", "gid": 0, "group": "root", "md5sum": "065fab9f9da81fe06d67c5c279262f83", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:usr_t:s0", "size": 8, "src": "/root/.ansible/tmp/ansible-tmp-1593833682.5-85620-67313377801244/source", "state": "file", "uid": 0}
    [root@ansible-server ~]# vim a.txt 
    [root@ansible-server ~]# cat a.txt 
    123321
    123311
    [root@ansible-server ~]# ansible weball -m copy -a 'src=/root/a.txt dest=/opt/ owner=root group=root mode=644 backup=yes' -o
    ansible-web2 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "backup_file": "/opt/a.txt.85851.2020-07-03@20:36:11~", "changed": true, "checksum": "f4c953c39db8bb1f8a5a2c93a3d475fe4219d187", "dest": "/opt/a.txt", "gid": 0, "group": "root", "md5sum": "c9c780a336e86c3bb2b307806e9271f4", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:usr_t:s0", "size": 14, "src": "/root/.ansible/tmp/ansible-tmp-1593833770.84-87442-25034919832242/source", "state": "file", "uid": 0}
    ansible-web1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "backup_file": "/opt/a.txt.85281.2020-07-04@11:36:11~", "changed": true, "checksum": "f4c953c39db8bb1f8a5a2c93a3d475fe4219d187", "dest": "/opt/a.txt", "gid": 0, "group": "root", "md5sum": "c9c780a336e86c3bb2b307806e9271f4", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:usr_t:s0", "size": 14, "src": "/root/.ansible/tmp/ansible-tmp-1593833770.85-87441-60491166064998/source", "state": "file", "uid": 0}
    注释:如果文件没有变化,不会备份。只有文件内容不同,才会做备份。
    
    登录被控制机器其中一台查看
    
    [root@ansible-web1 ~]# cat /opt/a.txt
    a.txt                             a.txt.85281.2020-07-04@11:36:11~
    文件和备份文件均已发送
    
    移动被控制节点的文件
    
    [root@ansible-server ~]# ansible weball -m shell -a 'mv /mnt/user.txt /tmp' -o
    

    2.软件包管理-YUM模块
    安装apache

     ansible webservers1 -m yum -a "name=httpd state=latest" -o
    state=absent     用于remove安装包
    state=latest       表示最新的
    state=removed   表示卸载
    
    [root@ansible-server ~]# ansible webservers1 -m yum -a "name=httpd state=removed" -o
    或者
    [root@ansible-server ~]# ansible webservers1 -m yum -a "name=httpd state=absent" -o
    

    3.服务管理service模块

    state = started 启动
    state = stoped 停止
    state = restarted 重启
    enable = yes 设置开机启动
    enable = no 设置开机不自启
    
    [root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started" #启动
    [root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=stopped" #停止
    [root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=restarted" #重启
    [root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started enabled=yes" #开机启动
    [root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started enabled=no"  #开机关闭
    

    4.文件模块file
    模块参数详解:
    owner:修改属主
    group:修改属组
    mode:修改权限
    path=:要修改文件的路径
    recurse:递归的设置文件的属性,只对目录有效
    yes:表示使用递归设置
    state:
    touch:创建一个新的空文件
    directory:创建一个新的目录,当目录存在时不会进行修改

    [root@ansible-server ~]# ansible webservers1 -m file -a 'path=/tmp/cui.txt mode=777 state=touch'
    ansible-web1 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "dest": "/tmp/cui.txt", 
        "gid": 0, 
        "group": "root", 
        "mode": "0777", 
        "owner": "root", 
        "secontext": "unconfined_u:object_r:user_tmp_t:s0", 
        "size": 0, 
        "state": "file", 
        "uid": 0
    }
    
    [root@ansible-server ~]# ansible ansible-web2 -m file -a 'path=/tmp/cui1.txt mode=777 owner=root state=touch'
    ansible-web2 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "dest": "/tmp/cui1.txt", 
        "gid": 0, 
        "group": "root", 
        "mode": "0777", 
        "owner": "root", 
        "secontext": "unconfined_u:object_r:user_tmp_t:s0", 
        "size": 0, 
        "state": "file", 
        "uid": 0
    }
    [root@ansible-server ~]# ansible webservers1 -m file -a 'path=/tmp/qf mode=777 state=directory'
    ansible-web1 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "gid": 0, 
        "group": "root", 
        "mode": "0777", 
        "owner": "root", 
        "path": "/tmp/qf", 
        "secontext": "unconfined_u:object_r:user_tmp_t:s0", 
        "size": 6, 
        "state": "directory", 
        "uid": 0
    }
    [root@ansible-server ~]# ansible ansible-web2 -m file -a 'path=/opt/haha owner=root group=root state=directory recurse=yes'
    ansible-web2 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "gid": 0, 
        "group": "root", 
        "mode": "0755", 
        "owner": "root", 
        "path": "/opt/haha", 
        "secontext": "unconfined_u:object_r:usr_t:s0", 
        "size": 6, 
        "state": "directory", 
        "uid": 0
    }
    

    5.信息收集模块setup

    [root@ansible-server ~]# ansible webservers1 -m setup  #收集所有信息
    [root@ansible-server ~]# ansible webservers1 -m setup -a 'filter=ansible_all_ipv4_addresses' #只查询ipv4的地址
    filter:过滤
    

    相关文章

      网友评论

          本文标题:CentOS7 Ansible自动化运维

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