• 自动化运维工具----ansible


    ansible是新出现的运维工具是基于Python研发的糅合了众多老牌运维工具的优点实现了批量操作系统配置、批量程序的部署、批量运行命令等功能。

    主要模块以及功能:

    1 command

    2 user

    3 group

    4 cron

    5 copy

    6 file

    7 ping

    8 yum

    9 service

    10 shell

    11 script

    12 setup

    13 playbooks

    14 忽略错误

    15 handler

    ansible一般使用普通用户操作,如需使用root权限,可以设置sudo

    主要有以下特点

    ansible:   yum -y install ansible 
        模块化:调用特定的模块,完成特定的任务
        基于python语言实现,由paramiko,PyYAMAL和Jinja2三个关键模块:
        部署简单,agentless
        支持主从模式
        支持自定义模块
        支持Playbook
    幂等性:  同一个配置文件中执行的操作允许多少次结果都是一样

    配置文件有

        配置文件:
            /etc/ansible/ansible.cfg
            /etc/ansible/hosts  主机清单

    在使用前需要发布ssh密钥来实现自动部署

    ssh-keygen -t rsa -P ''   -P密码为空
    ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.31.5
    ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.31.17
    执行命令测试:
     ssh 192.168.31.5 'ifconfig'
    问题:
    ansible 192.168.31.5 -m command -a 'ifconfig'
     [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
    localhost does not match 'all'
    
     [WARNING]: Could not match supplied host pattern, ignoring: 192.168.31.5
    解决:需要将主机写入/etc/ansible/hosts文件里面

    查看文档说明

    ansible-doc -s  command|user..... 查看文档说明

    模块说明:

    1 command模块

    ansible all -m command -a 'ifconfig'
    
    ansible websrvs  -m command -a 'wget -O /tm/文件名 http://路径/文件'  给websrvs组内的主机下载文件

    2 user模块   -a 'name=  state={present|absent}  system=.....'

    ansible wesrvs -m user -a "name=hacluster state=present" 创建一个非系统用户
    ansible wesrvs -m user -a "name=hacluster state=absent"   删除这个用户

    3 group模块 -a 'name= gid= state=  system='

    cron模块,用来生成计划任务 -a  'name= minute= hour= day= month= weekday= job= user=  state='

    [root@centos7 ansible]# ansible all -m cron -a 'name="sync time form ntpserver" minute="*/10" job="/sbin/ntpdate us.pool.ntp.org &> /dev/null"'
    192.168.31.5 | SUCCESS => {
        "changed": true, 
        "envs": [], 
        "jobs": [
            "sync time form ntpserver"
        ]
    }
    192.168.31.17 | SUCCESS => {
        "changed": true, 
        "envs": [], 
        "jobs": [
            "sync time form ntpserver"
        ]
    }
    [root@centos7 ansible]# crontab -l
    #Ansible: sync time form ntpserver
    */10 * * * * /sbin/ntpdate us.pool.ntp.org &> /dev/null
    
    删除计划任务:
     ansible all -m cron -a 'name="sync time form ntpserver" state=absent' 指明name加上absent

     5  copy模块; 文件复制  -a 'dest= src= mode=  owner= group='

    ansible wesrvs -m copy -a 'src=/etc/fstab dest=/tmp/fstab.tmp mode=600' 复制文件,设置文件权限

    6 file模块 :设置文件的属性

    ansible all -m file -a 'path=/tmp/testdir state=directory'   创建一个目录
    ansible all -m file -a 'path=/tmp/fstab.symlink state=link src=/tmp/fstab.tmp' 创建一个链接文件

    7 ping模块 就时单独的ping,没有参数

    ansible all -m ping

    yum:执行yum的安装命令

    ansible all  -m yum -a 'name=nginx state=latest' 安装包
    ansible all  -m yum -a 'name=nginx state=absent' 卸载

    service模块:设置和服务相关的配置 -a  'name= state={started|stopped|restarted} enabled='

    ansible wesrvs -m service -a 'name=nginx state=started enabled=yes' 设置nginx启动,并开机自启动

    10 shell  :执行命令

    ansible all -m shell -a 'echo 123456 | passwd --stdin limi' 给用户创建密码,此用户必须先存在
    使用 command模块会报错,需要放入一个子shell中所以使用shell模块

     11 script模块:执行脚本

    [root@centos7 ansible]# cat /tmp/test.sh 
    #!/bin/sh
    #
    
    echo "$(hostname) ansible is good." >/tmp/ansible.txt
    
    [root@centos7 ansible]# ll /tmp/test.sh   不给执行权限,远程主机执行 了
    -rw-r--r-- 1 root root 67 Sep 18 12:22 /tmp/test.sh
    [root@centos7 ansible]# ansible all -m script -a '/tmp/test.sh'

    12  setup 获取远程主机的facts,变量,属于查看类别

    ansible all -m setup

    13 playbooks,综合以上所有的模块

    核心元素有:
    tasks:任务 variables:变量 templates:模板 handlers:处理器 roles:角色
    其中一个特点:中途发送错误,所有已经执行的任务都将回滚,需要更正之后重新执行一次

    下面是一个文件例子:hosts,tasks这些冒号之前的文字变成蓝色的就是写正确了,否则就要检查

    - hosts: all
      remote_user: root
      tasks:
       - name: add a group    // 加上 - 就是一个单独的模块以及处理命令:
         group: gid=1008 name=testgroup system=no
       - name: excute a command
         command: /bin/date
    
    运行结果
    [root@centos7 ~]# ansible-playbook test.yaml
    
    PLAY [all] ***********************************************************************************
    
    TASK [Gathering Facts] ***********************************************************************
    ok: [192.168.31.17]
    ok: [192.168.31.5]
    
    TASK [add a group] ***************************************************************************
    changed: [192.168.31.5]
    changed: [192.168.31.17]
    
    TASK [excute a command] **********************************************************************
    changed: [192.168.31.17]
    changed: [192.168.31.5]
    
    PLAY RECAP ***********************************************************************************
    192.168.31.17              : ok=3    changed=2    unreachable=0    failed=0   
    192.168.31.5               : ok=3    changed=2    unreachable=0    failed=0   

    忽略错误的情况
    比如执行mkdir 命令,再建用户的话将报错

    方法一 /bin/true 

    tasks:
     - name:  run this command and ignore the result
        command:  /usr/bin/somecommand || /bin/true

    方法二: ignore_errors

    tasks:
     - name:  run this command and ignore the result
        command:  /usr/bin/somecommand 
        ignore_errors: True

    handler: 用于当关注的资源发生变化时采取一定的操作

       - hosts: all
         remote_user: root
         tasks:
             - name:  ensuse apache laster version
               yum:  state=latest name=httpd
             - name:  apache configure file
               copy: src=/root/httpd.conf  dest=/etc/httpd/conf/httpd.conf  force=yes
               notify:  #激活handlers中的name相同名称的服务
               - restart apache
         handlers:
            - name:  restart apache
              service: name=apache state=restarted

     关于ansible的提权操作:如果免密登陆的远程用户是普通用户,有些命令需要root执行时:远程服务器需要visudo 设置普通用户可以sudo执行命令

    1 命令执行的情况:其中 zabbix_agent是一个服务器组

    [root@peklpcbipo1 ansible]# ansible zabbix_agent -m shell -a 'whoami' -b --become-method=sudo
    10.122.33.250 | CHANGED | rc=0 >>
    root
    [root@peklpcbipo1 ansible]# ansible zabbix_agent -m shell -a 'whoami' 
    10.122.33.250 | CHANGED | rc=0 >>
    cdp

    2 使用playbook的情况

    2.1 切换root执行,下面没有设置become_user,默认是root

    - name: Ensure the httpd service is running
      service:
        name: httpd
        state: started
      become: yes

    2.2 切换其他普通用户执行的情况,下面是切换到用户apache执行某些命令

    - name: Run a command as the apache user
      command: somecommand
      become: yes
      become_user: apache

     不同的主机使用不同的用户问题

    [root@peklpcbipo1 ansible]# cat hosts
    [zabbix_agent_1]
    10.122.33.1 ansible_ssh_user="root"
    10.122.33.2 ansible_ssh_user="root"
    10.122.33.3 ansible_ssh_user="root"
    10.122.33.4 ansible_ssh_user="root"
    10.122.33.5 ansible_ssh_user="root"
    10.122.33.6 ansible_ssh_user="root"
    10.122.33.7 ansible_ssh_user="root"
    [zabbix_agent]   ---》这里默认使用配置文件ansible.cfg里面的remote_user配置
    10.62.101.168
    10.62.101.159

    其他

  • 相关阅读:
    Numpy存字符串
    一个类似于postman的协议测试工具
    freetds设置超时
    学习jQuery
    webpy 使用python3开发
    gdb调试coredump文件
    htop和ncdu
    rqalpha-自动量化交易系统(一)
    perl学习-运算符添加引号
    xss 和 csrf攻击详解
  • 原文地址:https://www.cnblogs.com/mmyy-blog/p/9669435.html
Copyright © 2020-2023  润新知