Ansible是一款简单的运维自动化工具,只需要使用ssh协议连接就可以来进行系统管理,自动化执行命令,部署等任务。

Ansible的特点

1、ansible轻量级无客户端agentless,只需要双方支持ssh

2、ansible playbook 采用yaml配置,对于自动化任务执行统一了脚本格式

3、学习成本低,快速上手

4、完整的模块化扩展,支持开发能容丰富

Ansible组成结构

Ansible
Ansible的命令工具,核心执行工具;一次性或临时执行的操作都是通过该命令执行。

Ansible Playbook
任务剧本(又称任务集),编排定义Ansible任务集的配置文件,由Ansible顺序依次执行,yaml格式。

Inventory
Ansible管理主机的清单,默认是/etc/ansible/hosts文件。

Modules
Ansible执行命令的功能模块,Ansible2.3版本为止,共有1039个模块。还可以自定义模块。

Plugins
插件,模块功能的补充,常有连接类型插件,循环插件,变量插件,过滤插件,插件功能用的较少。

API
提供给第三方程序调用的应用程序编程接口。



安装与快速使用 

curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo  && yum clean all && yum makecache

yum -y install ansible

ansible --version

————————————————————————————————————————

配置主机Inventory     /etc/ansible/hosts

临时指定inventory   /etc/ansible-web

[root@C7-local ~]# cat /etc/ansible/hosts
# This is the default ansible 'hosts' file.
[web]
192.168.9.243
192.168.9.242
192.168.9.241

[db]
192.168.17.214
192.168.17.220
192.168.17.248

————————————————————————————————————————

查看主机列表  
ansible all --list-hosts
ansible web --list
ansible all -m ping -i /etc/ansible-web

查看主机存活状态
ansible web -m ping -o
匹配apache组nginx组两个组所有的机器(并集)
ansible 'web:db' -m ping -o 
执行非内置命令
ansible web -m shell -a 'yum clean all ; yum makecache' -o

Ansible Playbooks常用模块

file模块  在目标主机创建文件或目录,并赋予其系统权限

- name:create a file
file: 'path=/root/test.txt state=touch mode=0755
owner=user group=user'
 
copy模块  实现Ansible服务端到目标主机的文件传送
- name:copy a file
copy:'remote_src=no src=roles/testbox/files/test.sh
dest=/root/test.sh mode=0644 force=yes'
 
Stat模块    获取远程文件状态信息
- name:check if test.sh exists
stat: 'path=/root/test.sh'
register:script_stat
 
Debug模块    打印语句到Ansible执行输出
-debug:msg=test.sh exists
when:script_stat.stat.exists
 
Command/Shell模块    用来执行Linux目标主机命令行 
- name:run the script
command:"sh/root/test.sh"
-name:run the script
shell:"echo'test'>/root/test.txt"
 
Template模块    实现Ansible服务端到目标主机的jinja2模板传送
- name:write the nginx config file
   template:src=roles/testbox/templates/nginx.confj2
dest=/etc/nginx/nginx.conf
 
Packaging模块    调用目标主机系统包管理工具(yum,apt)进行安装
- name:ensure nginx is at the latest version 
yum:pkg=nginx state=latest
 
Service模块    管理目标主机系统服务
- name:start nginx service
service:name=nginx state=started
 
ansible-playbook -i inventory/testenv ./deploy.yml
 
 

Ansible-playbook执行慢

 gather_facts: no


PS:

Ansible快速入门

Ansible文献资料

Ansible中文权威指南

Ansible批量执行命令慢的解决思路

Ansible执行速度优化指北

Ansible-快速启动的更多相关文章

  1. ansible 快速入门

    安装 $ sudo apt-get install software-properties-common $ sudo apt-add-repository ppa:ansible/ansible $ ...

  2. 8. 利用Ansible快速构建MGR | 深入浅出MGR

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 目录 1. 安装ansbile 2. 配置ansible 3. 建立ssh信任 4. 测试ansible 5. 使用ans ...

  3. Win7快速启动栏

    http://jingyan.baidu.com/article/456c463bbc1d140a583144cf.html 1. 在任务栏上右键 -> 工具栏 -> 新建工具栏.   在 ...

  4. Atitit 控制中心快速启动面板quick launcher

    Atitit 控制中心快速启动面板quick launcher contralPanel.bat aaaControlPanel.contrlx /AtiPlatf_auto/src_atibrow/ ...

  5. 使用Yeoman快速启动AngularJS项目开发

    本博客停止更新,请访问新个人博客:owenchen.net 前言 博客迁移到了BAE上,http://owenchen.net/,以后的文章会首发在自己的博客上,随后在博客园发布. 很久没有写文章了, ...

  6. 神逸之作:国产快速启动软件神品ALTRun

    http://xbeta.info/altrun.htm 作者: ET民工和塞壬 日期: 2010-09-15 分类: windows 标签: quick-launch <神逸之作:国产快速启动 ...

  7. win7和win8如何设置快速启动栏

    a.在任务栏上右键 -> 工具栏 -> 新建工具栏 -> 跳出选择文件夹对话框,在文件夹里面(光标山洞处)输入这个路径,然后按回车: %userprofile%\AppData\Ro ...

  8. Linux系统快速启动方案

    =========================        基本常识       ========================= Linux系统基本启动流程: 1. CPU从ROM(如果有的 ...

  9. win7 系统如何设置快速启动栏

    a.在任务栏上右键 -> 工具栏 -> 新建工具栏 -> 跳出选择文件夹对话框,在文件夹里面(光标山洞处)输入这个路径,然后按回车: %userprofile%\AppData\Ro ...

  10. win+r 快速启动应用程序

    如何使用WIN+R快捷键快速启动应用程序呢?其实很简单 首先随便在一个盘上建一个新文件夹,随便取什么名字,最好是英文格式,小编以D盘为例,如图 2. 打开文件夹,单击右键,选择“新建”,单击“快捷方式 ...

随机推荐

  1. Android Activity间跳转与传递数据

    1 概述 Activity之间的跳转主要使用 startActivity(Intent intent); startActivityForResult(Intent intent,int reques ...

  2. Mybatis3源码笔记(一)环境搭建

    1. 源码下载 地址:https://github.com/mybatis/mybatis-3.git. 国内访问有时确实有点慢,像我就直接先fork.然后从git上同步到国内的gitte上,然后在i ...

  3. C - 抽屉 POJ - 3370 (容斥原理)

    Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain ...

  4. 功能:SpringBoot日志配置详情

    SpringBoot日志配置详情 一.介绍 在所有的项目中,日志是必不可少的,为了高效清晰的查找日志,可以配置日志输出的等级和格式. 在配置后,可以自定义输出日志到指定目录,可以按照天数来分割日志,可 ...

  5. POJ 1961 KMP(当前重复次数)

    题意:       前缀重复次数,举个例子,aaa 2的位置2个a,3的位置3个a abcabcabc 6的位置两个abcabc,9的位置三个abcabc.... 思路:      KMP基础题目之一 ...

  6. hdu4515 小模拟

    题意:       给你当期日期,问前D天和后D天的年月日. 思路:      直接模拟就行了水题,对了别忘了题目2013,3,5要输出这样的2013/03/05. #include<stdio ...

  7. java.lang.ClassNotFoundException的解决方案

    举一个特定的例子 java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource 到Maven中央仓库下载 当我们看 ...

  8. 在服务器上使用 smart http 搭建 Git 服务器

    前言 最近一直在写 django 网页的代码,随着代码的量越来越大,管理起来也有点复杂(当然,有在使用 git 进行代码管理).同时由于有不同的工作环境,有些工作环境对 ssh 的访问有限制,所以想到 ...

  9. x265编码命令

    CQP: #/bin/bash ./x265 --input FourPeople_1280x720_60.yuv --input-res 1280x720 --fps 60 --qp 40 --fr ...

  10. vue3.0API详解

    Vue 3.0 于 2020-09-18 发布了,使用了 Typescript 进行了大规模的重构,带来了 Composition API RFC 版本,类似 React Hook 一样的写 Vue, ...