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 >&
# 取消定时任务
[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= 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(统一 ...
随机推荐
- 学习 MeteoInfo二次开发教程(三)
1.breakList的问题 ((PolygonBreak) aLS.breakList[0]).DrawFill=false; 新的类库将LegendScheme的breakList属性改为了Leg ...
- 通过spark sql 将 hdfs上文件导入到mongodb
功能:通过spark sql 将hdfs 中文件导入到mongdo 所需jar包有:mongo-spark-connector_2.11-2.1.2.jar.mongo-java-driver-3.8 ...
- Spark+Scalar+Mysql
包:mysql-connector-java-5.1.39-bin.jar 平台:Win8.1 环境:MyEclipse2015 hadoop-2.7.3.tar.gz + winutils.exe ...
- ADO.NET链接数据库封装方法
/// <summary> /// 获取一个表 /// </summary> /// <param name="sql ...
- python3文件的读写操作
open函数:对文件进行读写操作前,先打开文件,获取文件的句柄: open(file, mode, encoding, buffering) 参数说明 file_name:一个包含了你要访问的文件路径 ...
- 1. Spring基于xml加载和读取properties文件配置
在src目录下,新建test.properties配置文件,内容如下 name=root password=123456 logArchiveCron=0/5 * * * * ? 一种是使用sprin ...
- leetcode139
class Solution { public: bool wordBreak(string s, vector<string> wordDict) { vector<, false ...
- VS2017打包设置
本文为网络贴文,引用于:http://www.cnblogs.com/overstep/p/6942423.html 一. 安装打包插件: 安装打包插件:Microsoft Visual Studi ...
- tensorflow实战系列(四)基于TensorFlow构建AlexNet代码解析
整体流程介绍: 我们从main函数走,在train函数中,首先new了一个network;然后初始化后开始训练,训练时设定设备和迭代的次数,训练完后关闭流程图. 下面看network这个类,这个类有许 ...
- Spring再接触 注入类型
共有三种注入类型 一种是set注入 一种是构造注入 一种是接口注入 最常用的还是set 现在看一下construct 构造注入 在userservice中加入 package com.bjsxt.se ...