内容:
1、ansible的作用以及工作结构
2、ansible的安装以及使用
3、ansible的playbook使用

一、ansible的作用以及工作结构
        1、ansible简介:
        ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
        (1)、连接插件connection plugins:负责和被监控端实现通信;
        (2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
        (3)、各种模块核心模块、command模块、自定义模块;
        (4)、借助于插件完成记录日志邮件等功能;
        (5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。
        2、ansible的架构:连接其他主机默认使用ssh协议

二、ansible的安装以及常用模块使用
        1、ansible无服务器端,使用时直接运行命令即可,同时不需要在被管控主机上安装任何客户端,因此ansible是一个十分轻量级的工具,可以在epel源进行安装,ansible已经被红帽收购,相信不久会被收入base源
        配置好epel源后直接yum安装ansible

  •  [root@php ~]# yum info ansible
    Loaded plugins: fastestmirror, refresh-packagekit, security
    Loading mirror speeds from cached hostfile
    base | 4.0 kB 00:00 ...
    epel | 4.3 kB 00:00
    epel/primary_db | 5.7 MB 00:00
    Available Packages
    Name : ansible
    Arch : noarch
    Version : 1.9.2
    Release : 1.el6
    Size : 1.7 M
    Repo : epel
    Summary : SSH-based configuration management, deployment, and task execution system
    URL : http://ansible.com
    License : GPLv3
    Description :
    : Ansible is a radically simple model-driven configuration management,
    : multi-node deployment, and remote task execution system. Ansible works
    : over SSH and does not require any software or daemons to be installed
    : on remote nodes. Extension modules can be written in any language and
    : are transferred to managed machines automatically.
    [root@php ~]# yum install ansible

查看生成的主要文件:

 /etc/ansible
/etc/ansible/ansible.cfg #配置文件
/etc/ansible/hosts #主机库(host inventory)
/usr/bin/ansible #主程序
/usr/bin/ansible-doc #文档
/usr/bin/ansible-playbook #剧本

ansible命令的使用方法也比较简单:
        语法:
        ansible <host-pattern> [-f forks] [-m module_name] [-a args]
        host-pattern:host inventory文件的一个组名,可以为all
            -f forks:并行处理的个数,默认为5
            -m module_name:模块名,默认为command
            -a args:参数
        ansible-doc:
            -l:查看模块列表
            -s:查看相关模块参数
        我们可以看到ansible支持非常多的模块:

 [21:20 root@centos6.8/var/ftp/pub/files]# ansible-doc -l
less 436
Copyright (C) 1984-2009 Mark Nudelman
less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less
a10_server Manage A10 Networks AX/SoftAX/Thunder/vThunder devices
a10_service_group Manage A10 Networks AX/SoftAX/Thunder/vThunder devices
a10_virtual_server Manage A10 Networks AX/SoftAX/Thunder/vThunder devices
acl Sets and retrieves file ACL information.
add_host add a host (and alternatively a group) to the ansible-playbook in-memory inventory
airbrake_deployment Notify airbrake about app deployments
alternatives Manages alternative programs for common commands
apache2_module enables/disables a module of the Apache2 webserver
apt Manages apt-packages
apt_key Add or remove an apt key
apt_repository Add and remove APT repositories
apt_rpm apt_rpm package manager
assemble Assembles a configuration file from fragments
assert Fail with custom message
at Schedule the execution of a command or script file via the at command.
authorized_key Adds or removes an SSH authorized key
azure create or terminate a virtual machine in azure
bigip_facts Collect facts from F5 BIG-IP devices
bigip_monitor_http Manages F5 BIG-IP LTM http monitors
bigip_monitor_tcp Manages F5 BIG-IP LTM tcp monitors
bigip_node Manages F5 BIG-IP LTM nodes
bigip_pool Manages F5 BIG-IP LTM pools
bigip_pool_member Manages F5 BIG-IP LTM pool members
bigpanda Notify BigPanda about deployments
boundary_meter Manage boundary meters

注意:使用ansible-doc -s查看帮助是,一般有=号的参数都是必要的参数
        Ansible默认安装好后有一个配置文件/etc/ansible/ansible.cfg,该配置文件中定义了ansible的主机的默认配置部分,如默认是否需要输入密码、是否开启sudo认证、action_plugins插件的位置、hosts主机组的位置、是否开启log功能、默认端口、key文件位置等等。
        具体如下:

 [defaults]
# some basic default values...
hostfile = /etc/ansible/hosts \\指定默认hosts配置的位置
# library_path = /usr/share/my_modules/
remote_tmp = $HOME/.ansible/tmp
pattern = *
forks = 5
poll_interval = 15
sudo_user = root \\远程sudo用户
#ask_sudo_pass = True \\每次执行ansible命令是否询问ssh密码
#ask_pass = True \\每次执行ansible命令时是否询问sudo密码
transport = smart
remote_port = 22
module_lang = C
gathering = implicit
host_key_checking = False \\关闭第一次使用ansible连接客户端是输入命令提示
log_path = /var/log/ansible.log \\需要时可以自行添加。chown -R root:root ansible.log
system_warnings = False \\关闭运行ansible时系统的提示信息,一般为提示升级
# set plugin path directories here, separate with colons
action_plugins = /usr/share/ansible_plugins/action_plugins
callback_plugins = /usr/share/ansible_plugins/callback_plugins
connection_plugins = /usr/share/ansible_plugins/connection_plugins
lookup_plugins = /usr/share/ansible_plugins/lookup_plugins
vars_plugins = /usr/share/ansible_plugins/vars_plugins
filter_plugins = /usr/share/ansible_plugins/filter_plugins
fact_caching = memory
[accelerate]
accelerate_port = 5099
accelerate_timeout = 30
accelerate_connect_timeout = 5.0
# The daemon timeout is measured in minutes. This time is measured
# from the last activity to the accelerate daemon.
accelerate_daemon_timeout = 30

免密登陆

因为ansible是基于ssh工作,所以在使用ansible之前要先给各个服务器制作ssh免密登陆

ssh免密登陆教程

用法

 ansible users1 -m command -a 'ls /etc/rc.local'
# | | | | | |
# | | | | | |_________________要执行的命令
# | | | | |
# | | | | |____________________________接命令
# | | | |
# | | | |__________________________________模块
# | | |
# | | |_______________________________________接模块
# | |
# | |____________________________________________组/IP
# |
# |_____________________________________________________ansible

远程执行命令模块

shell模块

 # 在/tmp/1.txt写入hello
ansible users1 -m shell -a 'echo "hello" > /tmp/1.txt'
 # 查看/tmp/1.txt文件内容
ansible users1 -m shell -a 'cat /tmp/1.txt'

command模块

 ansible users1 -m command -a 'ls /etc/rc.local'

其他模块

copy模块(将本地文件拷贝到服务器)

 ansible users1 -m copy -a 'src=/root/passwd dest=/tmp/passwd mode=0777 ownes=user group=youboy'

备注:src本地文件;dest客户端目录;修改权限mode=0777 ;用户ownes=user ;用户组group=youboy

// 指定内容写入到文件

 ansible users1 -m copy -a 'content="hello word" dest=/tmp/test.txt mode=0777'

fetch模块(将服务器上的文件拷贝到本地)

 ansible users1 -m fetch -a 'src=/etc/passwd dest=/tmp/passwd'

file模块

 //删除文件
ansible users1 -m file -a 'past=/tmp/passwd state=adsent'
//创建软连接
ansible users1 -m file -a 'src=/etc/passwd path=/tmp/passwd.link state=link'
//修改用户权限
ansible users1 -m file -a 'path=/tmp/passwd mode=0777 ownes=user group=youboy'

疑问?
///服务器上的文件拷贝到其他目录

 ansible users1 -m copy -a 'path=/etc/passwd dest=/tmp/passwd'

cron模块(计划任务)

 ansible users1 -m cron -a 'minute=10 hour=02 day=15 moneth=12 weekday=7 name="test" job="date > /tmp/date.txt"'
//使用shell模块验证计划任务
ansible users1 -m shell -a 'crontab -l'
//清除计划任务(使用ansible users1 -m cron -a name="test" state=absent''可能无效,使用全命令清除即可)
ansible users1 -m cron -a 'minute=10 hour=02 day=15 moneth=12 weekday=7 name="test" job="date > /tmp/date.txt" state=absent'
//使用shell模块验证清除的计划任务

hostname模块(临时修改主机名)

 ansible 192.168.1.2 -m hostname -a 'name=jiahui.com'

yum模块

 ansible users1 -m yum -a 'name=httpd state=installed'

present 查看安装
installed 安装
latest 升级安装
absent 卸载

service模块(操作服务)

 //启动服务
ansible users1 -m service -a 'name=httpd state=started'

started 启动服务
stopped 关闭服务

 /开机自启
ansible users1 -m service -a 'name=httpd enabled=yes runlevel=2345'

备注:runlevel 运行级别(0123456 7个级别,如下)

 chkconfig --list | grep httpd
httpd 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭

ansible 工作原理以及使用详解的更多相关文章

  1. NFS工作原理及配置文件详解

    nfs工作原理流程       如上图所示,当访问程序通过NFS客户端向NFS服务端存取文件时,其请求数据流程如下几点:     1.首先用户访问网站程序,由程序在NFS客户端上发出NFS文件存取功能 ...

  2. 以太坊工作原理之txpool详解

    txpool详解 交易池txpool作为区块链系统的重要组成部分,对系统的安全性和稳定性具有重要作用.功能可归纳为:交易缓存.交易验证和交易过滤. 基本介绍 交易分类和缓存 txpool主要包含两个重 ...

  3. 场效应管种类-场效应管N、P沟道与增强、耗尽型工作原理等知识详解 如何选用晶体三极管与场效应管的技巧

    http://www.kiaic.com/article/detail/1308.html 场效应管种类场效应管 场效应晶体管(Field Effect Transistor缩写(FET))简称场效应 ...

  4. vmware三种网络模式的工作原理及配置详解

    vmware为我们提供了三种网络工作模式,它们分别是:Bridged(桥接模式).NAT(网络地址转换模式).Host-Only(仅主机模式). 打开vmware虚拟机,我们可以在选项栏的“编辑”下的 ...

  5. Go语言备忘录:反射的原理与使用详解

    目录: 预备知识 reflect.Typeof.reflect.ValueOf Value.Type 动态调用 通过反射可以修改原对象 实现类似“泛型”的功能   1.预备知识: Go的变量都是静态类 ...

  6. Spring学习 6- Spring MVC (Spring MVC原理及配置详解)

    百度的面试官问:Web容器,Servlet容器,SpringMVC容器的区别: 我还写了个文章,说明web容器与servlet容器的联系,参考:servlet单实例多线程模式 这个文章有web容器与s ...

  7. Go语言备忘录(2):反射的原理与使用详解

    本文内容是本人对Go语言的反射原理与使用的备忘录,记录了关键的相关知识点,以供翻查. 文中如有错误的地方请大家指出,以免误导!转摘本文也请注明出处:Go语言备忘录(2):反射的原理与使用详解,多谢! ...

  8. 基础 | batchnorm原理及代码详解

    https://blog.csdn.net/qq_25737169/article/details/79048516 https://www.cnblogs.com/bonelee/p/8528722 ...

  9. Oracle中的SQL分页查询原理和方法详解

    Oracle中的SQL分页查询原理和方法详解 分析得不错! http://blog.csdn.net/anxpp/article/details/51534006

随机推荐

  1. Code VS 1002 搭桥

    题目描述 Description 有一矩形区域的城市中建筑了若干建筑物,如果某两个单元格有一个点相联系,则它们属于同一座建筑物.现在想在这些建筑物之间搭建一些桥梁,其中桥梁只能沿着矩形的方格的边沿搭建 ...

  2. Python智能提示--提示对象内涵成员

    1. demo展示 2. 提示效果

  3. Oracle查看哪些表被锁住了

    --查看哪些表被锁住了select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_object ...

  4. Mysql学习总结(36)——Mysql查询优化

    从事前端开发的都知道,页面显示的数据一定要及时的呈现,否则会影响用户体现.那么导致页面加载数据慢或者显示滞后的原因又是什么呢? 拿自己之前做项目经历给大家讲讲吧,之前做后台,当时的项目实时性都非常高, ...

  5. MySQL主要命令(2)

    创建表 : create table if not exists employee( //格式:变量名 数据类型, id int, name varchar(30), sex varchar(2), ...

  6. rails create方法ActiveModel::ForbiddenAttribute的问题

    rails create方法ActiveModel::ForbiddenAttribute的问题 def create @ad = Ad.new(ad_params) @ad.save end pri ...

  7. UVa11183 - Teen Girl Squad(最小树形图-裸)

    Problem I Teen Girl Squad  Input: Standard Input Output: Standard Output -- 3 spring rolls please. - ...

  8. 在不同的浏览器下FORM及它的小伙伴们默认样式的CSS属性值是不全然一致

    我们一般在定义CSS样式的时候都须要定义去掉HTML标签默认样式的CSS,原因是在不同的浏览器以下它们的表现出来的默认样式不全然一致,我们要保证在不同的浏览器下它们能正常显示出我们想要达到的预期效果, ...

  9. 【Android 应用开发】 ActionBar 样式具体解释 -- 样式 主题 简单介绍 Actionbar 的 icon logo 标题 菜单样式改动

    作者 : 万境绝尘 (octopus_truth@163.com) 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/3926916 ...

  10. 程序猿的量化交易之路(18)--Cointrader之Event实体(6)

    转载需注明: 事件,是Esper的重要概念. 这里我们定义个事件类.它是Temporal实体的派生类. 不过对Temporal简单的包装.其代码例如以下: package org.cryptocoin ...