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

  1. wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

  2. #ansible 管理端
  3. yum install ansible -y
  4. yum install libselinux-python -y

  5. #backup nfs01 被管理端
  6. yum install libselinux-python -y
  7. [root@m01 ~]# tree /etc/ansible/
  8. /etc/ansible/
  9. ├── ansible.cfg   # ansible的配置文件
  10. ├── hosts       # ansible管理了 哪些服务器 服务器列表
  11. └── roles      # 角色
  12.  
  13. [root@m01 ~]# cat /etc/ansible/hosts
  14. [lewen]
  15. 172.16.1.31
  16. 172.16.1.41

  17. ansible lewen -m command -a "hostname"
  18. 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在所有客户端必须存在。

  1. [root@m01 ~]# ansible lewen -m copy -a "src=/etc/hosts dest=/tmp owner=lewen mode=0755" # backup=yes 已存在的文件就复制备份
  2. 172.16.1.41 | SUCCESS => {
  3. "changed": true,
  4. "checksum": "bc07bb4d3a780f4fd8cae94ec7bff04edb1a5a4e",
  5. "dest": "/tmp/hosts",
  6. "gid": ,
  7. "group": "root",
  8. "md5sum": "55ee21bf1168f9be70abd35bf29d8e4a",
  9. "mode": "",
  10. "owner": "lewen",
  11. "size": ,
  12. "src": "/root/.ansible/tmp/ansible-tmp-1517744820.18-259504826638509/source",
  13. "state": "file",
  14. "uid":
  15. }
  16. 172.16.1.31 | SUCCESS => {
  17. "changed": true,
  18. "checksum": "bc07bb4d3a780f4fd8cae94ec7bff04edb1a5a4e",
  19. "dest": "/tmp/hosts",
  20. "gid": ,
  21. "group": "root",
  22. "md5sum": "55ee21bf1168f9be70abd35bf29d8e4a",
  23. "mode": "",
  24. "owner": "lewen",
  25. "size": ,
  26. "src": "/root/.ansible/tmp/ansible-tmp-1517744820.17-14642605512978/source",
  27. "state": "file",
  28. "uid":
  29. }


  30. [root@m01 ~]# ansible lewen -m command -a "ls -l /tmp/hosts"
  31. 172.16.1.31 | SUCCESS | rc= >>
  32. -rwxr-xr-x lewen root Feb : /tmp/hosts
  33. 172.16.1.41 | SUCCESS | rc= >>
  34. -rwxr-xr-x lewen root Feb : /tmp/hosts
  1. ansible lewen -m copy -a "src=/etc/hosts dest=/tmp backup=yes"
  2.  
  3. ansible-doc -l|wc -l
  4. ansible-doc -s copy # 查看文档
  5.  
  6. 其他常用模块命令
  7. ansible lewen -m copy -a "src=/server/scripts/yum-htop.sh dest=/server/scripts/ "
  8. ansible lewen -m shell -a "/bin/sh /server/scripts/yum-htop.sh"
  9. ansible lewen -m script -a "/server/scripts/yum.sh"

定时任务

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

  1. # 创建定时任务
    [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'"
  2. 172.16.1.31 | SUCCESS => {
  3. "changed": true,
  4. "envs": [],
  5. "jobs": [
  6. "restart network"
  7. ]
  8. }
  9. 172.16.1.41 | SUCCESS => {
  10. "changed": true,
  11. "envs": [],
  12. "jobs": [
  13. "restart network"
  14. ]
  15. }
  16. ​# 查看定时任务
  17. [root@m01 scripts]# ansible lewen -a "crontab -l"
  18. 172.16.1.41 | SUCCESS | rc= >>
  19. #time sync by lidao at --
  20. */ * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null >&
  21. #check & send result lee at --
  22. * * * /bin/sh /server/scripts/check.sh >/dev/null >&
  23. #Ansible: restart network
  24. * * * /etc/init.d/network restart >/dev/null >&
  25. 172.16.1.31 | SUCCESS | rc= >>
  26. #time sync by lidao at --
  27. */ * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null >&
  28. #Ansible: restart network
  29. * * * /etc/init.d/network restart >/dev/null >&​
  1. ​# 取消定时任务
  2. [root@m01 ~]# ansible oldboy -m cron -a "name='restart network' state=absent "
  3. 172.16.1.31 | SUCCESS => {
  4. "changed": true,
  5. "envs": [],
  6. "jobs": []
  7. }
  8. 172.16.1.41 | SUCCESS => {
  9. "changed": true,
  10. "envs": [],
  11. "jobs": []
  12. } 

常用模块

  1. 每个模块就是一个功能
  2. command(默认的模块)  #执行命令模块****
  3. shell          #执行shell 脚本模块****。 # 支持shell 管道更多的功能
  4. script         #把脚本发到客户端,然后执行。****。
  5. copy          #把本地文件发送到远端
  6. file          # 设定文件属性模块。
  7. service        #系统服务管理模块。
  8. cron          #计划任务管理模块
  9. yum          #yum软件包安装管理模块
  10. synchronize     #使用rsync同步文件模块。

  11. eg:
      ansible lewen -m service -a "name=crond state=started enabled=yes"

  12. ssh 认证模块
  13. authorized_key   #-Adds or removes an SSH authorized key

playbook

ansible 剧本

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

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

  1. mkdir -p /server/playbook

  2. [root@m01 playbook]# cat ifconfig.yml
  3. - hosts: lewen
  4. tasks:
  5. - command: ifconfig
  6. - shell: ifconfig >/tmp/ip.log


  7. ansible-playbook -C ifconfig.yml # 检查剧本
  8. ansible-playbook ifconfig.yml
  9.  
  10. [root@m01 playbook]# cat print-ip.yml
  11. - hosts: all
  12. tasks:
  13. - name: get ip address
  14. shell: ifconfig eth0 |awk -F "[ :]+" 'NR==2{print $4}' >>/tmp/ip.log
  15.  
  16. ansible-playbook -C print-ip.yml
  17. ansible-playbook print-ip.yml
  18. ansible all -a "tail -1 /tmp/ip.log"
  19.  
  20. ansible oldboy -m cron -a 'name="restart network" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present'

  21. ​# playbook添加定时任务
  22. [root@m01 playbook]# cat add-cron.yml
  23. - hosts: oldboy
  24. tasks:
  25. - name: add restart network cron
  26. cron: name="restart network" minute= hour= job="/etc/init.d/network restart >/dev/null 2>&1" state=present

  27.  
  28. 查看
  29. [root@m01 playbook]# ansible oldboy -a "crontab -l"
  30. 172.16.1.41 | SUCCESS | rc= >>
  31. #time sync by lidao at --
  32. */ * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null >&
  33. #check & send result lee at --
  34. * * * /bin/sh /server/scripts/check.sh >/dev/null >&
  35. 172.16.1.31 | SUCCESS | rc= >>
  36. #time sync by lidao at --
  37. */ * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null >&

playbook添加定时任务

  1. 两种书写格式
  1. (1)
    - hosts: oldboy
  2. tasks:
  3. - name: add restart network cron
  4. cron: name="restart network" minute= hour= job="/etc/init.d/network restart >/dev/null 2>&1" state=present
    (2)
  5. - hosts: oldboy
  6. tasks:
  7. - name: add restart network cron
  8. cron:
  9. name: restart network
  10. minute:
  11. hour:
  12. job: /etc/init.d/network restart >/dev/null >&
  13. state: present

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

重启网络 service

安装软件 yum

显示时间信息到文件 date

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

-

  1. [root@m01 playbook]# cat hosts.yml
  2. - hosts: 172.16.1.41
  3. tasks:
  4. - name: mkdir
  5. shell: mkdir -p /oldboy/backup
  6. - hosts: 172.16.1.31
  7. tasks:
  8. - name: find
  9. shell: find /etc -type f -name "*.conf" >>/tmp/name.log
  10.  
  11. ansible安装rsync服务器
  12.  
  13. nfs服务器
  14.  
  15. 配置sersync数据同步
  16.  
  17. 如何使用pssh (pssh pscp prsync)

view

w9 Ansible批量管理与维护的更多相关文章

  1. ansible批量管理服务 上

    1 ansible简介 1.1 ansible批量管理服务概述 (1)是基于python语言开发的自动化软件工具(2)是基于SSH远程管理服务实现远程主机批量管理(3)并行管理,部署简单,应用也简单方 ...

  2. 使用ansible批量管理远程服务器

    使用ansible批量管理远程服务器 背景 本地需要管理远程的一批服务器,主要执行以下任务: 1) 将本地的文件复制到远端所有服务器: 2) 需要在远程服务器中执行一个个命令: 远端服务器路径并非完全 ...

  3. 六.ansible批量管理服务

    期中集群架构-第六章-ansible批量管理服务介绍====================================================================== 01. ...

  4. Ansible 批量管理Windows Server服务器

    Ansible批量管理Windows Server         Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具,  它用Python写成,类似于saltstack和Puppe ...

  5. Linux(11):期中架构(3)--- SSH远程管理服务 & ansible 批量管理服务

    SSH远程管理服务 1. 远程管理服务知识介绍 # 1.1 SSH远程登录服务介绍说明 SSH是Secure Shell Protocol的简写,由 IETF 网络工作小组(Network Worki ...

  6. ansible批量管理常见的配置方法

    第7章 ansible的管理 7.1 ansible概念的介绍 ansible-playbook –syntax            检查语法 ansible-playbook -C         ...

  7. ansible批量管理软件部署及剧本

    服务器版本信息: Centos6.9 [root@db02 ~]# uname -a Linux db02 -.el6.x86_64 # SMP Tue Mar :: UTC x86_64 x86_6 ...

  8. Linux中ansible批量管理软件部署及剧本编写

    服务器版本信息: Centos6.9 [root@db02 ~]# uname -a Linux db02 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 21 19:29: ...

  9. Linux系统——Ansible批量管理工具

    批量管理工具: (1)ansible 操作简单(适用于500台以下服务器) (2)saltstack 比较复杂(一般适用于1000-4w台服务器) (3)puppet超级复杂 systemctl(统一 ...

随机推荐

  1. 详解卷积神经网络(CNN)

    详解卷积神经网络(CNN) 详解卷积神经网络CNN 概揽 Layers used to build ConvNets 卷积层Convolutional layer 池化层Pooling Layer 全 ...

  2. Problem B: 取石子

    转换成一个数在(0,X + Y)的加减问题 考虑一种使用线段树处理的方法, 维护前缀最大值, 前缀最小值, 前缀和, 然后查询的时候先询问右区间是否会同时碰到上下界, 会的话左区间无用直接递归右区间, ...

  3. TensorFlow模型加载与保存

    我们经常遇到训练时间很长,使用起来就是Weight和Bias.那么如何将训练和测试分开操作呢? TF给出了模型的加载与保存操作,看了网上都是很简单的使用了一下,这里给出一个神经网络的小程序去测试. 本 ...

  4. android toolbar效果

    layout下的layout_main.xml: <?xml version="1.0" encoding="utf-8"?> <Relati ...

  5. JAVA Aes加解密详解

    上篇随笔留了一个问题,两种加密结果不一样? 其实是内部实现方式不一样,具体见注释 /** * 提供密钥和向量进行加密 * * @param sSrc * @param key * @param iv ...

  6. __module__ 和 __class__

    __module__  查看当前方法来之于那个文件 __class__  查看当前方法来之于那个类

  7. oracle数据链接

    using System; using System.Collections.Generic; using System.Data; using System.Data.OracleClient; u ...

  8. vue 全局组件【原】

    1.目录 2.内容 -Loading.vue <template> <div class="loading"> loading... </div> ...

  9. java学习--java源文件

    java源文件以“java”为扩展名.源文件的基本组成部分是类(class) 一个源文件中最多只能有一个public类.其他类(如抽象类,default类即省去修饰符的类,接口)的个数不限. 如果源文 ...

  10. 浅谈MyBatisGenerator的使用

    目录 1.概述 2.依赖 3.Maven插件配置 4.配置文件说明 5.运行 6.总结 1.概述 日常中使用MyBatis最为麻烦的就是编写Mapper文件了, 如果数据库增加一张表, 这时通常会复制 ...