zoukankan      html  css  js  c++  java
  • Ansible批量管理与维护

      Ansible是2013年推出的一种通用自动化工具,可用于配置管理或工作流程自动化。配置管理是一种“基础架构代码”实践,它将事物编码,例如应该在系统上安装什么包和版本,或者应该运行什么守护进程。工作流自动化可能是从配置基础架构到部署软件的任何事情。Ansible在2015年时被Redhat公司收购。
      Ansible是用Python编写的,它使用SSH在不同的机器上执行命令。Ansible是无代理的,这使得入手更容易。您只需要在相关机器上安装SSH访问和Python。Ansible使用声明式YML"playbook"
    将一组主机(从“hosts”)映射到定义明确的角色。声明性用于指示Ansible如何设置或更改事物,Ansible才进行必要的更改。

      200-500台服务器,用ansible。更多的则使用saltstack

      ansible 无需安装客户端,依赖ssh服务。 -->ssh 认证        

    ansible 部署

    安装ansible

    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
    ​
    #ansible 管理端
    yum install ansible -y
    yum install libselinux-python -y
    ​
    #backup nfs01  被管理端
    yum install libselinux-python -y
    [root@m01 ~]# tree /etc/ansible/
    /etc/ansible/
    ├── ansible.cfg   # ansible的配置文件
    ├── hosts       # ansible管理了 哪些服务器 服务器列表
    └── roles      # 角色
    
    [root@m01 ~]# cat /etc/ansible/hosts
    [lewen]
    172.16.1.31
    172.16.1.41
    ​
    ansible lewen -m command -a "hostname"
    ansible lewen -m command -a "yum install cowsay -y"

    -m 后边是模块的名字 -m MODULE_NAME,--module-name=MODULE_NAME module name to execute(default=command). -a 后面是要执行的命令  -a MODULE_ARGS,-args=MODULE_ARGS. module arguments.

    复制文件

    利用ansible远程批量拷贝文件或目录。
    语法:
    
     ansible lewen -m copy -a "sre=/etc/passwd dest=/tap/oldgirl.txt owner=lewen group=lewen sode=0755" 
    注意:
    1)如果指定的目标目录不存在,系统会自动创建,否则源目录会放到目标目录下面去:
    2)如果copy的是文件,dest指定的名字和源如果不同,并且它不是已经存在的目录,相当于copy过去后再重命名:
    3)若果dest是目标机器上已经存在的目录,则会直接把文件copy 到该目录下面。
    4)设定的用户和组lewen在所有客户端必须存在。
    [root@m01 ~]# ansible lewen -m copy -a "src=/etc/hosts dest=/tmp owner=lewen mode=0755"  # backup=yes 已存在的文件就复制备份
    172.16.1.41 | SUCCESS => {
    "changed": true,
    "checksum": "bc07bb4d3a780f4fd8cae94ec7bff04edb1a5a4e",
    "dest": "/tmp/hosts",
    "gid": 0,
    "group": "root",
    "md5sum": "55ee21bf1168f9be70abd35bf29d8e4a",
    "mode": "0755",
    "owner": "lewen",
    "size": 364,
    "src": "/root/.ansible/tmp/ansible-tmp-1517744820.18-259504826638509/source",
    "state": "file",
    "uid": 500
    }
    172.16.1.31 | SUCCESS => {
    "changed": true,
    "checksum": "bc07bb4d3a780f4fd8cae94ec7bff04edb1a5a4e",
    "dest": "/tmp/hosts",
    "gid": 0,
    "group": "root",
    "md5sum": "55ee21bf1168f9be70abd35bf29d8e4a",
    "mode": "0755",
    "owner": "lewen",
    "size": 364,
    "src": "/root/.ansible/tmp/ansible-tmp-1517744820.17-14642605512978/source",
    "state": "file",
    "uid": 500
    }
    ​
    ​
    [root@m01 ~]# ansible lewen -m command -a "ls -l /tmp/hosts"
    172.16.1.31 | SUCCESS | rc=0 >>
    -rwxr-xr-x 1 lewen root 364 Feb 4 19:47 /tmp/hosts
    172.16.1.41 | SUCCESS | rc=0 >>
    -rwxr-xr-x 1 lewen root 364 Feb 4 19:47 /tmp/hosts
    
    ansible lewen -m copy -a "src=/etc/hosts dest=/tmp backup=yes"
    
    ansible-doc -l|wc -l
    ansible-doc -s copy # 查看文档

    其他常用模块命令 ansible lewen -m copy -a "src=/server/scripts/yum-htop.sh dest=/server/scripts/ " ansible lewen -m shell -a "/bin/sh /server/scripts/yum-htop.sh" ansible lewen -m script -a "/server/scripts/yum.sh"

    定时任务

    linux 定时任务。
    分,时,日,月,周   执行的命令。

    # 创建定时任务
    [root@m01 scripts]# ansible lewen -m cron -a "name='restart network' minute=00 hour=00 job=' /etc/init.d/network restart >/dev/null 2>&1'" 172.16.1.31 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "restart network" ] } 172.16.1.41 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "restart network" ] } ​# 查看定时任务 [root@m01 scripts]# ansible lewen -a "crontab -l"
    172.16.1.41 | SUCCESS | rc=0 >> #time sync by lidao at 2017-03-08 */5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1 #check & send result lee at 2017-01-01 03 * * * /bin/sh /server/scripts/check.sh >/dev/null 2>&1 #Ansible: restart network 00 * * * /etc/init.d/network restart >/dev/null 2>&1 172.16.1.31 | SUCCESS | rc=0 >> #time sync by lidao at 2017-03-08 */5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1 #Ansible: restart network 00 * * * /etc/init.d/network restart >/dev/null 2>&1​
    ​# 取消定时任务
    [root@m01 ~]# ansible oldboy -m cron -a "name='restart network' state=absent "
    172.16.1.31 | SUCCESS => {
        "changed": true,
        "envs": [],
        "jobs": []
    }
    172.16.1.41 | SUCCESS => {
        "changed": true,
        "envs": [],
        "jobs": []
    } 

    常用模块

    每个模块就是一个功能
    command(默认的模块)  #执行命令模块****
    shell          #执行shell 脚本模块****。           # 支持shell 管道更多的功能
    script         #把脚本发到客户端,然后执行。****。
    copy          #把本地文件发送到远端
    file          # 设定文件属性模块。
    service        #系统服务管理模块。
    cron          #计划任务管理模块
    yum          #yum软件包安装管理模块
    synchronize     #使用rsync同步文件模块。
    
    eg:
      ansible lewen -m service -a "name=crond state=started enabled=yes"
    ssh 认证模块 authorized_key   #-Adds or removes an SSH authorized key

    playbook 

    ansible 剧本

    核心功能
    1.PyYAML-剧本的语言。

    2.paramiko-远程连接与数据传输。
    3.Jinjia2

    mkdir -p /server/playbook
    ​
    [root@m01 playbook]# cat ifconfig.yml
    - hosts: lewen
      tasks:
        - command: ifconfig
        - shell: ifconfig >/tmp/ip.log
    ​
    ​
    ansible-playbook -C ifconfig.yml       # 检查剧本
    ansible-playbook ifconfig.yml          
    
    
    [root@m01 playbook]# cat print-ip.yml          
    - hosts: all
      tasks:
        - name: get ip address
          shell: ifconfig eth0 |awk -F "[ :]+" 'NR==2{print $4}' >>/tmp/ip.log
    
    
    ansible-playbook -C print-ip.yml
    ansible-playbook print-ip.yml
    ansible all -a "tail -1 /tmp/ip.log"
    ​
    
    ansible oldboy -m cron -a 'name="restart network" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present'
    ​
    ​# playbook添加定时任务
    [root@m01 playbook]# cat add-cron.yml
    - hosts: oldboy
      tasks:
        - name: add restart network cron
          cron: name="restart network" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present
    ​
    ​
    
    查看
    [root@m01 playbook]# ansible oldboy -a "crontab -l"
    172.16.1.41 | SUCCESS | rc=0 >>
    #time sync by lidao at 2017-03-08
    */5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
    #check & send result lee at 2017-01-01
    03 * * * /bin/sh /server/scripts/check.sh >/dev/null 2>&1
    172.16.1.31 | SUCCESS | rc=0 >>
    #time sync by lidao at 2017-03-08
    */5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1

    playbook添加定时任务

    两种书写格式
    (1)
    - hosts: oldboy tasks: - name: add restart network cron cron: name="restart network" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present
    (2) - hosts: oldboy tasks: - name: add restart network cron cron: name: restart network minute: 00 hour: 00 job: /etc/init.d/network restart >/dev/null 2>&1 state: present

    例3:对同一台机器配置多个任务

    重启网络 service

    安装软件 yum

    显示时间信息到文件 date

    [root@m01 playbook]# cat manage.yml
    - hosts: all
      tasks:
        - name: restart network
          service:                    #服务
            name: network               #服务器名
            state: restarted            #状态
        - name: install tree nmap lrzsz iftop htop iotop nc
          shell: yum install -y tree nmap lrzsz iftop htop iotop nc
        - name: print date to file
          shell: date +%F >>/tmp/date.log
    yml 转化后的格式:
    [ { hosts: 'all', tasks: [ { name: 'restart network', service: { name: 'network', state: 'restarted' } }, { name: 'install tree nmap lrzsz iftop htop iotop nc', shell: 'yum install -y tree nmap lrzsz iftop htop iotop nc' }, { name: 'print date to file', shell: 'date +%F >>/tmp/date.log' } ] } ]

    -

    view
  • 相关阅读:
    双11享Go了吗?2017阿里双11在线峰会续写科技盛宴!
    python django 原生sql 获取数据
    python 操作mysql
    django post 请求传参
    Django post 报错
    django get请求传参
    windows8.1下装kali linux 2017.3双系统(最新版)
    Django 速成
    Galera将死——MySQL Group Replication正式发布
    Galera将死——MySQL Group Replication正式发布
  • 原文地址:https://www.cnblogs.com/wenyule/p/13472624.html
Copyright © 2011-2022 走看看