w9 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": ,
- "group": "root",
- "md5sum": "55ee21bf1168f9be70abd35bf29d8e4a",
- "mode": "",
- "owner": "lewen",
- "size": ,
- "src": "/root/.ansible/tmp/ansible-tmp-1517744820.18-259504826638509/source",
- "state": "file",
- "uid":
- }
- 172.16.1.31 | SUCCESS => {
- "changed": true,
- "checksum": "bc07bb4d3a780f4fd8cae94ec7bff04edb1a5a4e",
- "dest": "/tmp/hosts",
- "gid": ,
- "group": "root",
- "md5sum": "55ee21bf1168f9be70abd35bf29d8e4a",
- "mode": "",
- "owner": "lewen",
- "size": ,
- "src": "/root/.ansible/tmp/ansible-tmp-1517744820.17-14642605512978/source",
- "state": "file",
- "uid":
- }
-
-
- [root@m01 ~]# ansible lewen -m command -a "ls -l /tmp/hosts"
- 172.16.1.31 | SUCCESS | rc= >>
- -rwxr-xr-x lewen root Feb : /tmp/hosts
- 172.16.1.41 | SUCCESS | rc= >>
- -rwxr-xr-x lewen root Feb : /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= >>
- #time sync by lidao at --
- */ * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null >&
- #check & send result lee at --
- * * * /bin/sh /server/scripts/check.sh >/dev/null >&
- #Ansible: restart network
- * * * /etc/init.d/network restart >/dev/null >&
- 172.16.1.31 | SUCCESS | rc= >>
- #time sync by lidao at --
- */ * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null >&
- #Ansible: restart network
- * * * /etc/init.d/network restart >/dev/null >&
常用模块
- 每个模块就是一个功能
- 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= hour= 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= >>
- #time sync by lidao at --
- */ * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null >&
- #check & send result lee at --
- * * * /bin/sh /server/scripts/check.sh >/dev/null >&
- 172.16.1.31 | SUCCESS | rc= >>
- #time sync by lidao at --
- */ * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null >&
playbook添加定时任务
- 两种书写格式
- (1)
- hosts: oldboy- tasks:
- - name: add restart network cron
- cron: name="restart network" minute= hour= 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:
- hour:
- job: /etc/init.d/network restart >/dev/null >&
- 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' } ] } ]
-
- [root@m01 playbook]# cat hosts.yml
- - hosts: 172.16.1.41
- tasks:
- - name: mkdir
- shell: mkdir -p /oldboy/backup
- - hosts: 172.16.1.31
- tasks:
- - name: find
- shell: find /etc -type f -name "*.conf" >>/tmp/name.log
- ansible安装rsync服务器
- nfs服务器
- 配置sersync数据同步
- 如何使用pssh (pssh pscp prsync)
view
w9 Ansible批量管理与维护的更多相关文章
- ansible批量管理服务 上
1 ansible简介 1.1 ansible批量管理服务概述 (1)是基于python语言开发的自动化软件工具(2)是基于SSH远程管理服务实现远程主机批量管理(3)并行管理,部署简单,应用也简单方 ...
- 使用ansible批量管理远程服务器
使用ansible批量管理远程服务器 背景 本地需要管理远程的一批服务器,主要执行以下任务: 1) 将本地的文件复制到远端所有服务器: 2) 需要在远程服务器中执行一个个命令: 远端服务器路径并非完全 ...
- 六.ansible批量管理服务
期中集群架构-第六章-ansible批量管理服务介绍====================================================================== 01. ...
- Ansible 批量管理Windows Server服务器
Ansible批量管理Windows Server Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具, 它用Python写成,类似于saltstack和Puppe ...
- Linux(11):期中架构(3)--- SSH远程管理服务 & ansible 批量管理服务
SSH远程管理服务 1. 远程管理服务知识介绍 # 1.1 SSH远程登录服务介绍说明 SSH是Secure Shell Protocol的简写,由 IETF 网络工作小组(Network Worki ...
- ansible批量管理常见的配置方法
第7章 ansible的管理 7.1 ansible概念的介绍 ansible-playbook –syntax 检查语法 ansible-playbook -C ...
- ansible批量管理软件部署及剧本
服务器版本信息: Centos6.9 [root@db02 ~]# uname -a Linux db02 -.el6.x86_64 # SMP Tue Mar :: UTC x86_64 x86_6 ...
- Linux中ansible批量管理软件部署及剧本编写
服务器版本信息: Centos6.9 [root@db02 ~]# uname -a Linux db02 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 21 19:29: ...
- Linux系统——Ansible批量管理工具
批量管理工具: (1)ansible 操作简单(适用于500台以下服务器) (2)saltstack 比较复杂(一般适用于1000-4w台服务器) (3)puppet超级复杂 systemctl(统一 ...
随机推荐
- 详解卷积神经网络(CNN)
详解卷积神经网络(CNN) 详解卷积神经网络CNN 概揽 Layers used to build ConvNets 卷积层Convolutional layer 池化层Pooling Layer 全 ...
- Problem B: 取石子
转换成一个数在(0,X + Y)的加减问题 考虑一种使用线段树处理的方法, 维护前缀最大值, 前缀最小值, 前缀和, 然后查询的时候先询问右区间是否会同时碰到上下界, 会的话左区间无用直接递归右区间, ...
- TensorFlow模型加载与保存
我们经常遇到训练时间很长,使用起来就是Weight和Bias.那么如何将训练和测试分开操作呢? TF给出了模型的加载与保存操作,看了网上都是很简单的使用了一下,这里给出一个神经网络的小程序去测试. 本 ...
- android toolbar效果
layout下的layout_main.xml: <?xml version="1.0" encoding="utf-8"?> <Relati ...
- JAVA Aes加解密详解
上篇随笔留了一个问题,两种加密结果不一样? 其实是内部实现方式不一样,具体见注释 /** * 提供密钥和向量进行加密 * * @param sSrc * @param key * @param iv ...
- __module__ 和 __class__
__module__ 查看当前方法来之于那个文件 __class__ 查看当前方法来之于那个类
- oracle数据链接
using System; using System.Collections.Generic; using System.Data; using System.Data.OracleClient; u ...
- vue 全局组件【原】
1.目录 2.内容 -Loading.vue <template> <div class="loading"> loading... </div> ...
- java学习--java源文件
java源文件以“java”为扩展名.源文件的基本组成部分是类(class) 一个源文件中最多只能有一个public类.其他类(如抽象类,default类即省去修饰符的类,接口)的个数不限. 如果源文 ...
- 浅谈MyBatisGenerator的使用
目录 1.概述 2.依赖 3.Maven插件配置 4.配置文件说明 5.运行 6.总结 1.概述 日常中使用MyBatis最为麻烦的就是编写Mapper文件了, 如果数据库增加一张表, 这时通常会复制 ...