【原创】运维基础之Ansible(1)简介、安装和使用

官方:https://www.ansible.com/
一 简介
Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.
It uses no agents and no additional custom security infrastructure, so it's easy to deploy - and most importantly, it uses a very simple language (YAML, in the form of Ansible Playbooks) that allow you to describe your automation jobs in a way that approaches plain English.
ansible的好处是快速、简单、易用、不需要安装agent,可以方便的用于配置管理和应用部署等自动化场景;
二 安装
1 安装
yum install -y ansible
2 测试
# ansible --version
ansible 2.7.5
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Jul 13 2018, 13:06:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
3 配置目录结构
# ls /etc/ansible/
ansible.cfg hosts roles
三 使用
首先在/etc/ansible/hosts中根据部署结构将server划分到多个group,然后可以针对某个group执行某些操作;
1 添加server分组配置
vi /etc/ansible/hosts
[all-servers]
192.168.0.54
192.168.0.55
192.168.0.56
以上配置了一个server_group名为all-servers,其中包含3台server;
可以通过--list查看某个server_group下的所有server:
# ansible all-servers --list
hosts (3):
192.168.0.54
192.168.0.55
192.168.0.56
2 执行操作
格式
ansible $server_group -m $module_name -a $module_args
例如ping:
# ansible all-servers -m ping
192.168.0.54 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.0.55 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.0.56 | SUCCESS => {
"changed": false,
"ping": "pong"
}
例如command:
# ansible all-servers -m command -a "date"
192.168.0.54 | CHANGED | rc=0 >>
Sun Jan 13 17:14:06 CST 2019192.168.0.56 | CHANGED | rc=0 >>
Sun Jan 13 17:14:06 CST 2019192.168.0.55 | CHANGED | rc=0 >>
Sun Jan 13 17:14:06 CST 2019
具体的module_name有哪些?可以通过命令查看
# ansible-doc -l
某个module_name的帮助也可以通过命令查看
# ansible-doc $module_name
比如:
# ansible-doc shell
> SHELL (/usr/lib/python2.7/site-packages/ansible/modules/commands/shell.py)
The `shell' module takes the command name followed by a list of space-delimited arguments. It is almost exactly like the [command] module but runs the command
through a shell (`/bin/sh') on the remote node. For Windows targets, use the [win_shell] module instead.
# ansible-doc copy
> COPY (/usr/lib/python2.7/site-packages/ansible/modules/files/copy.py)
The `copy' module copies a file from the local or remote machine to a location on the remote machine. Use the [fetch] module to copy files from remote locations to
the local box. If you need variable interpolation in copied files, use the [template] module. For Windows targets, use the [win_copy] module instead.
常用module:ping,command,shell,copy,file,script,cron等...
比如向某个group的所有server拷贝文件命令:
# ansible all-servers -m copy -a 'src=/tmp/1.log dest=/tmp/'
比如查看某个group的所有server的磁盘占用情况:
# ansible all-servers -m shell -a "df -h"
另外也可以直接指定ip来操作,比如:
# ansible 192.168.0.55 -m ping
其实如果只需要远程执行命令,ssh也可以
ssh 192.168.0.55 "date"
【原创】运维基础之Ansible(1)简介、安装和使用的更多相关文章
- 自动化运维工具之 Ansible 介绍及安装使用
一.初识Ansible 介绍: Absible 使用 模块(Modules)来定义配置任务.模块可以用标准脚本语言(Python,Bash,Ruby,等等)编写,这是一个很好的做法,使每个模块幂等.A ...
- 【Apache运维基础(1)】Apache的安装与使用
安装 yum -y install httpd httpd-devel # 在Ubuntu里面叫做Apache2,输入localhost能打开就算成功了 额...当然专业的运维还是老老实实的去编译吧; ...
- 【原创】运维基础之Ansible(2)离线安装
1 在一个能访问远程repo的服务器上执行,下载ansible及相关依赖的rpm包 # mkdir ansible# yum install --downloadonly --downloaddir= ...
- 【原创】运维基础之yum离线环境安装软件
首先查看系统版本号,然后根据版本号从 CentOS-7-x86_64-DVD-1708.iso 和 CentOS-7-x86_64-Everything-1708.iso 根据需要选择一个下载,我这里 ...
- 自动化运维工具之ansible
自动化运维工具之ansible 一,ansible简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fab ...
- linux运维基础知识
linux运维基础知识大全 一,序言 每一个微不足道的知识,也是未来的铺垫.每一份工作的薪资职位,也是曾经努力的结果. 二,服务器 1,运维人员工作职责: 1)保证数据不丢失:2)保证服务器24小时运 ...
- 自动化运维工具:ansible
自动化运维工具:ansible Ansible(1):简介和基本概念 Ansible(2):安装配置 Ansible(3):ansible资源清单管理 Ansible(4):常用模块
- Linux运维基础
一.服务器硬件 二.Linux的发展史 三.Linux的系统安装和配置 四.Xshell的安装和优化 五.远程连接排错 六.Linux命令初识 七.Linux系统初识与优化 八.Linux目录结构 九 ...
- 第一阶段·Linux运维基础-第1章·Linux基础及入门介绍
01-课程介绍-学习流程 02-服务器硬件-详解 03-服务器核心硬件-服务器型号-电源-CPU 01-课程介绍-学习流程 1.1. 光看不练,等于白干: 1.2 不看光练,思想怠慢: 1.3 即看又 ...
随机推荐
- final和static关键字
1.如果类只有静态方法,可以将构造函数标记为private以避免被初始化: 2.常量同时标记为static和final,常量名全部大写,下划线连接: 3.final修饰一个成员变量(属性),必须要显示 ...
- JEECG 3.8宅男优化版本发布
1024程序员节宅男节日快乐 -- JAVA快速开发平台,JEECG 3.8宅男优化版本发布 - JEECG开源社区 - CSDN博客https://blog.csdn.net/zhangdaisco ...
- 关于学习springboot和springcloud的很不错的教程
近日,逐步开始学习了springboot和springcloud.本以为很简单,但是随着学习的深入,发现其中有很多地方都需要认真揣摩.凡事都需要循序渐进,有一个好的开端就是成功的一半.于是在浩瀚的网络 ...
- golang函数
一.函数语法 func 函数名(形参列表) (返回值列表){ ...... return 返回值 } 例如: package main import "fmt" func test ...
- spring cloud实战与思考(三) 微服务之间通过fiegn上传一组文件(下)
需求场景: 用户调用微服务1的接口上传一组图片和对应的描述信息.微服务1处理后,再将这组图片上传给微服务2进行处理.各个微服务能区分开不同的图片进行不同处理. 上一篇博客已经讨论了在微服务之间传递一组 ...
- Python——Django-settings.py的内容
一.HTML路径设置 #所有和HTML路径相关的设置都在这里 TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTem ...
- 【BZOJ5499】[2019省队联测]春节十二响(贪心)
[BZOJ5499][2019省队联测]春节十二响(贪心) 题面 BZOJ 洛谷 题解 如果是一条折链,显然维护两侧的值,每次两个堆分别弹出一个\(max\)然后合并一下,最后再放回去就可以了. 那么 ...
- CF618G(利用浮点数精度+矩乘优化DP)
这题真的太神辣,%了一发题解,原来还能这么搞QWQ 设\(A_{i,j}\)表示不加任何限制时,第\(i\)个格子会出现权值为\(j\)的史莱姆的概率,则有: \[A_{i,j}=A_{i,j-1}* ...
- 深入理解JVM(5)——垃圾收集和内存分配策略
1.垃圾收集对象 垃圾收集主要是针对堆和方法区进行. 程序计数器.虚拟机栈和本地方法栈这三个区域属于线程私有的,只存在于线程的生命周期内,线程结束之后也会消失,因此不需要对这三个区域进行垃圾回收. 哪 ...
- 内网ntp时间同步配置
选择局域网中的一台机器作为ntp服务器,在ntp server上安装并启动ntpd客户端上要关闭ntpd,安装ntpdateCentOS7上这两个软件都是自带的,只需根据需要打开或者关闭.注意客户端机 ...