1. Ansible的架构

Ansible的帮助文档:
http://www.ansible.com.cn/index.html

2. YAML语言简介

  • 基本规则

列表(list, [, , , ...])中的所有成员都开始与相同的缩进级别,并且使用“-”开头。要求-后边必须跟一个空格。

- apple
- banana
- orange
- pear

相应python输出

['apple', 'banana', 'orange', 'pear']

字典(dictionary, {key1:value1, key2:value2, key3:value3, ...})由一组“键:值”构成,且:后边必须跟一个空格。

node_a:
conntimeout: 300
external:
iface: eth0
port: 556
internal:
iface: eth0
port: 778
broadcast:
client: 1000
server: 2000
node_b:
0:
ip: 10.0.0.1
name: b1
1:
ip: 10.0.0.2
name: b2

相应python输出

{
'node_b': { #注意嵌套层次。
0: None, #注意空值。
'ip': '10.0.0.2', #注意key不能重复,重复则覆盖。
'name': 'b2',
1: None
},
'node_a': {
'iface': 'eth0',
'port': 778,
'server': 2000,
'broadcast': None,
'client': 1000,
'external': None,
'conntimeout': 300,
'internal': None
}
}

建议yaml文件以---最为开始行。

  • 说明示例
    test.yaml
---
name: Tom Smith
age: 37
spouse:
name: Jane Smith
age: 35
children:
- name1: Jimmy Smith
age1: 15
- name2: Jenny Smith
age2: 12
  • python的读取代码
#!/usr/bin/python

import yaml

file = open("test.yaml")
x = yaml.load(file)
print x

执行结果

{
'age': 37,
'spouse': {
'age': 25,
'name': 'Jane Smith'
},
'name': 'Tom Smith',
'children': [
{
'age1': 15,
'name1': 'Jimmy Smith'
},
{
'age2': 12,
'name2': 'Jenny Smith'
}
]
}
  • 再一个综合示例
---
name: Example Developer
job: Developer
skill: Elite
employed: True
foods:
- Apple
- Orange
- Strawberry
- Mango
languages:
ruby: Elite
python: Elite
dotnet: Lame

python的显示

{
'name': 'Example Developer',
'job': 'Developer',
'skill': 'Elite',
'employed': True,
'foods': [
'Apple', 'Orange', 'Strawberry', 'Mango'
],
'languages': {
'ruby': 'Elite',
'python': 'Elite',
'dotnet': 'Lame'
}
}

3. Ansible的安装

  • CentOS YUM的安装
先安装EPEL源
在主控端机器上安装
yum install -y ansible
ansible --version
测试安装是否成功
ansible 192.168.12.1 -m ping -k -u beeworkshop
注意:
-k 表示ssh使用密码认证(否则为密钥认证)
-u 指定ssh登陆的用户名
或者通过/etc/ansible/hosts配置
192.168.12.1 ansible_ssh_user=bee
来指定ssh登陆用户。
/etc/ansible/hosts文件中要配置192.168.12.1地址——相当于做白名单,否则不执行命令。
  • Ubuntu的安装
$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible
  • pip安装
$ sudo pip install ansible

4. Ansible的配置文件

  • /etc/ansible/hosts
# Ex 1: Ungrouped hosts, specify before any group headers.

## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10 # Ex 2: A collection of hosts belonging to the 'webservers' group ## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110 # If you have multiple hosts following a pattern you can specify
# them like this: ## www[001:006].example.com # Ex 3: A collection of database servers in the 'dbservers' group ## [dbservers]
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57 # Here's another example of host ranges, this time there are no
# leading 0s: ## db-[99:101]-node.example.com

Ansible通过定义号的主机和组规则Inventory指定了Ansible起作用的主机列表。Ansible默认读取/etc/ansible/hosts文件,以获得控制的主机。
如果不是默认位置的hosts文件需要使用-i选项指明:

ansible -i /home/beeworkshop/hosts bidder -m ping

localhost会被默认地添加到Inventory中。
一台主机可以属于多个组,但需使用优先级来避免冲突。
如果不是使用SSH的默认端口22,需要指定端口

bee.example.com:5555

别名的使用

jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50

指定连接类型和用户名

localhost ansible_connection=local
abc.exam.com ansible_connection=ssh ansible_ssh_user=abc
def.exam.com ansible_connection=ssh ansible_ssh_user=def
  • 主机变量
    供playbook配置使用。
[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909
  • 组变量
    组变量的作用是覆盖组中的所有成员。
[atlanta]
host1
host2
[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
  • 组嵌套
[atlanta]
host1
host2
[raleigh]
host2
host3
[southeast:children]
atlanta
raleigh
[usa:children]
southeast
northeast
southwest
northwest
  • 分离主机和组变量
    为了更好的规范定义主机和组变量,Ansible支持将hosts文件定义的主机名与组变量单独分离出来,并用YAML文件存储。其中Inventory目录和playbook目录均可以存放group_vars,host_vars,但playbook优先级更高。

5. Ansible常用模块

  • 帮助
ansible-doc -l
ansible-doc -s <module>
ansible 操作目标 -m 模块名 -a 模块参数
  • 常用模块的使用
(1) setup
获取客户机详细信息
ansible webserver -m setup (2) copy
向客户机发送文件
关闭客户机的SELinux
ansible webserver -m command -a "yum install -y install libselinux-python"
ansible webserver -m copy -a "src=/usr/local/src/test.py dest=/tmp owner=root group=root mode 0755 force=yes" force:
yes 覆盖
no 不存在时才复制文件 backup:
yes 覆盖之前备份原文件,备份文件包含时间
no 不备份 路径包含/ 复制不包含该目录,只涉及目录中的内容
路径不包含/ 复制包含该目录 (3) synchronize
需提前安装rsync
复制文件及目录至客户机
ansible 192.168.1.21 -m synchronize -a "src=/usr/local/src/ dest=/usr/local/src/ delete=yes compress=yes"
delete=yes 使两边内容一样——客户端不存在的新建,客户端不同的删除。
compress=yes 开启压缩
路径包含/ 复制不包含该目录,只涉及目录中的内容
路径不包含/ 复制包含该目录 (4) file
设置文件目录属性
group 定义文件目录的组
mode 定义文件目录的权限
owner 定义文件目录的属主
path 必选项,路径
recurse 递归设置文件属性,只对目录有效
src 链接原文件,只用于state=link
dest 链接目标,只用于state=link
force yes:覆盖,no:不覆盖
state 链接文件状态
link 创建软链接
directory 目录不存在则创建目录
file 即使文件不存在也不创建
absent 删除目录,文件,链接文件
touch 同touch命令 ansible 192.168.2.1 -m file -a "src=/usr/local/src/test.py dest=/tmp/test.py state=link"
ansible 192.168.2.1 -m command -a 'll /tmp/test.py'
ansible 192.168.2.1 -m file -a "path=/tmp/test.py state=absent"
ansible 192.168.2.1 -m file -a 'path=/tmp/test.py state=touch owner=root group=root mode=0755'
ansible webserver -m file -a 'path=/tmp/test state=directory owner=root group=root mode=0755'

Ansible自动化运维工具(1)的更多相关文章

  1. Ansible自动化运维工具-上

    [Ansible特点] 1)Ansible与saltstack均是基于Python语言开发的 2)安装使用简单,基于不同插件和模块实现各种软件,平台,版本的管理以及支持虚拟容器多层级的部署 3)不需要 ...

  2. Ansible 自动化运维工具

    Ansible 自动化运维工具 Ansible是什么? Ansible是一个"配置管理工具"也是一个"自动化运维工具" Ansible 作用: Ansible是 ...

  3. Ansible自动化运维工具的使用

                                 Ansible自动化运维工具的使用       host lnventory 管理主机 ip  root账号密码 ssh端口 core mod ...

  4. Ansible自动化运维工具及其常用模块

    Ansible自动化运维工具及其常用模块 目录 Ansible自动化运维工具及其常用模块 一.Ansible简介 1. Ansible概述 2. Ansible作用 3. Ansible的工作模块 4 ...

  5. Ansible自动化运维工具使用

    概述本文描述自动化运维工具 Ansible 的安装及基础使用方法,包含: Centos 下的安装主机配置Ad-Hoc command(命令行执行)Playbook (任务剧本)Ansible 和 Sa ...

  6. ansible自动化运维工具使用详解

    一. ansible 简介 1. ansible ansible是新出现的 自动化 运维工具 , 基于Python研发 . 糅合了众多老牌运维工具的优点实现了批量操作系统配置.批量程序的部署.批量运行 ...

  7. Ansible自动化运维工具

    ansible软件介绍 python语言是运维人员必会的语言!  ansible是一个基于Python开发的自动化运维工具!(saltstack)  其功能实现基于SSH远程连接服务!  ans ...

  8. [Linux]Ansible自动化运维② - 工具与模块

    目录 一.Ansible的工具 1.1 Ansible的工作前提 1.2 Ansible的安装文件 1.3 Ansible的配置文件 1.4 Ansible的相关工具 1.4.1 [帮助工具]Ansi ...

  9. 三石之道之Ansible自动化运维工具部署

    centos6默认python版本为2.6 centos7默认python版本为2.7 ansible需要最低python2.7的支持 总结:centos6要部署ansible工具,需要先升级pyth ...

  10. ansible自动化运维工具的安装与使用

    运行环境 centOS6.6 ansible ansible的功能还是比较多的,博主只用它在集群上进行批量部署软件和维护的功能,其他不多做研究,有需要的话这篇文章会慢慢补充. ansible特点 轻量 ...

随机推荐

  1. matplotlib中中文字体配置

    解决方式1:利用matplotlib的字体管理工具font_manager---->缺点:每次必须要进行设置 import matplotlib.pyplot as plt from matpl ...

  2. 在ubuntu12.4上安装minigui3.0.12

      在ubuntu12.4上安装minigui3.0.12 一下载源文件 移植所需的文件可以从minigui官网下载:http://www.minigui.org/en/download/ 主要文件有 ...

  3. 蒟蒻的splay 1---------洛谷板子题普通平衡树

    前言部分 splay是个什么东西呢? 它就是个平衡树,支持以下操作 这些操作还可以用treap,替罪羊树,红黑树,multiset balabala(好像混进去什么奇怪的东西) 这里就只说一下spla ...

  4. Django学习之模板

    一.常用语法 1.变量 2.Filters 3.自定义filter 4.Tags 5.csrf_token 6.注释 7.注意事项 二.母板 2.继承母板 3.块(block) 4.组件 5.静态文件 ...

  5. java设置RabbitMQ的消费处理出现:ConditionalRejectingErrorHandler : Execution of Rabbit message listener failed.

    WARN 7868 --- [cTaskExecutor-1] s.a.r.l.ConditionalRejectingErrorHandler : Execution of Rabbit messa ...

  6. JSON基础,简单介绍

    JSON(JavaScript Object Notation(记号.标记)) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - ...

  7. android hidl

    1.定义.hal接口文件,如: 在vendor/sprd/interface中新建目录hello,其中定义好hidl接口,如: package vendor.sprd.hardware.hello@1 ...

  8. 负载均衡环境搭建(nginx和tomcat)

    偶然看到博客上一篇负载均衡的文章,学习了一下,此处做下记录 目录 1.环境准备 2.tomcat配置 3.nginx配置 1.环境准备 第一步:java环境 第二步:nginx和pcre源码包 下载链 ...

  9. 如何在sql server数据库中建立主从表

    建立关联是通过外键引用实现的 例如建立一个学生表和班级表的关联,可以如下: create table class ( classid char(4) primary key not null, cla ...

  10. sql中unique和distinct

    在SQL语法里面,有unique和distinct两个关键字, unique是distinct的同义词,功能完全相同.distinct是标准语法,其他数据库 sql server,db2,oracle ...