Ansible配置:

ansible有两个核心配置文件: ansible.cfg 配置文件和Inventory配置文件

  • Ansible.cfg配置文件
  • Inventory机器列表配置

这里介绍Inventory配置文件

Inventory file:

Inventory file 机器清单,ansible用于管理机器节点的配置文件,类似系统的/etc/hosts文件。

默认的配置文件为:/etc/ansible/hosts (新版本默认的Inventory文件好像是 /home/ansible/ansible_hosts)。

Inventory文件遵循ini文件风格,[]标记分组,方便对机器列表的管理。

#inventory file例子,可在这里添加主机名hostname或者ip地址
#未分组的主机,添加在最前面
122.19.45.201
hostname1
122.19.45.[1:10] #[1:10]表示所有1~10之间的数字,表示一组ip地址45.1、45.2、... #分组管理的主机,以便对不同主机进行不同的管理,同一主机可同时属于不同组
[test0] #组名
122.28.13.100
122.19.61.68:5030 #如果主机ssh端口不是22,可在地址后加:指定 [targets1]
localhost ansible_connection=local
122.28.13.10 ansible_connection=ssh ansible_ssh_user=user #指定连接类型和连接用户名 [targets2] #可配置主机变量
host1 http_port=80
host2 http_port=80 var2=xxx var3=xxx [targets2:var] #添加关键字var,配置组变量,对属于该组的所有主机都适用
var4=xxx
var5=xxx [targets3:children] #添加关键字children,把组作为其他组的子成员
targets1
targets2

ansible ad-hoc 命令的用法为: ansible <host_pattern> [options]

这里先讨论host_pattern部分的用法。 [options]部分用简单的 -m ping来检测网络的连通性

Inventory文件的使用例子:

定义好Inventory文件后,通过:

  1. 命令行ansible <host-pattern> [options]
  2. playbook - hosts: <host-pattern>

<host-pattern>部分指定对哪些机器或分组执行任务。

以ansible命令行命令为例:

#使用默认的inventory文件
ansible 121.28.13.100 -m ping # 检测13.100是否存活(必须在inventory文件中)
ansible all -m ping # 检测所有主机是否存活
ansible targets1 -m ping # 检测组targets1的主机是否存活 #使用指定的inventory文件
ansible all -i my_hosts_list -m ping # -i参数指定inventory文件

ansible不能操作 没有在Inventory中定义过的主机**

主机列表的正则匹配

ansible支持主机列表的正则匹配

  • 全量: all/*
  • 逻辑或: :
  • 逻辑非:
  • 逻辑与:
  • 切片: []
  • 正则匹配: 以~开头
ansible all -m ping  #所有默认inventory文件中的机器
ansible "*" -m ping #同上
ansible 121.28.13.* -m ping #所有122.28.13.X机器 ansible web1:web2 -m ping #所有属于组web1或属于web2的机器
ansible web1:!web2 -m ping #属于组web1,但不属于web2的机器
ansible web1&web2 -m ping #属于组web1又属于web2的机器 ansible webserver[0] -m ping #属于组webserver的第1台机器
ansible webserver[0:5] -m ping #属于组webserver的第1到4台机器 ansible "~(beta|web)\.example\.(com|org)" -m ping

inventory file 与hosts patterns (ansible 机器清单 与 主机匹配模式)的更多相关文章

  1. Ansible 小手册系列 六(Patterns 匹配模式)

    Patterns 是定义Ansible要管理的主机.但是在playbook中它指的是对应主机应用特定的配置或IT流程. 命令格式 命令行 ansible <host-pattern> [o ...

  2. sudo提权,ansible批量给所有主机创建系统账户,授权

    sudo(superuser or another do)让普通用户可以以超级管理员或其他人的身份执行命令. sudo基本流程如下: 1.管理员需要先授权(修改/etc/sudoers文件) 2.普通 ...

  3. 16 Go Concurrency Patterns: Timing out, moving on GO并发模式: 超时, 继续前进

    Go Concurrency Patterns: Timing out, moving on  GO并发模式: 超时, 继续前进 23 September 2010 Concurrent progra ...

  4. Swap file "/etc/.hosts.swp" already exists! [O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it,

    非正常关闭vi编辑器时会生成一个.swp文件 非正常关闭vi编辑器时会生成一个.swp文件 关于swp文件 使用vi,经常可以看到swp这个文件,那这个文件是怎么产生的呢,当你打开一个文件,vi就会生 ...

  5. ansible的清单管理与模块应用(三)

  6. 设计模式教程(Design Patterns Tutorial)笔记之三 行为型模式(Behavioral Patterns)

    目录 · Strategy · When to use the Strategy Design Pattern? · Sample Code · Observer · When to use the  ...

  7. 判断机器CPU的大小端模式并将数据转换成小端形式

    首先看一下概念 Little-Endian 就是低位字节排放在内存的低地址端,高位字节排放在内存的高地址端 Big-Endian 就是高位字节排放在内存的低地址端,低位字节排放在内存的高地址端. 第一 ...

  8. 单个机器部署redis集群模式(一键部署脚本)

    一.检查机器是否安装gcc.unzip.wget 二.部署模式 #模式1: 将所有主从节点以及sentinel节点部署在同一台机器上 #模式2: 将一个数据节点和一个sentinel节点部署在一台机器 ...

  9. 设计模式教程(Design Patterns Tutorial)笔记之一 创建型模式(Creational Patterns)

    目录 · 概述 · Factory · What is the Factory Design Pattern? · Sample Code · Abstract Factory · What is t ...

随机推荐

  1. sql 小技巧

    declare @pids varchar(max)='' ),pid)+','+@pids from product where pname like '%red%' select @pids

  2. CentOS 7 NAT模式LVS搭建

    NAT模式下 , 调度器需要有两个IP , 一个公网IP一个内网IP , web服务器只需要内网IP 调度器LB : 192.168.94.11(内网IP)  192.168.29.11 (仅主机模式 ...

  3. sparse-table模板

    预处理: void init(int n) { ;i < n;i++) { dp[i][] = a[i]; } int bitn = (int)(log(n)/log(2.0)); ;j < ...

  4. Flask最强攻略 - 跟DragonFire学Flask - 第十四篇 Flask-SQLAlchemy

    前不久刚刚认识过了SQLAlchemy,点击这里复习一下 当 Flask 与 SQLAlchemy 发生火花会怎么样呢? Flask-SQLAlchemy就这么诞生了 首先要先安装一下Flask-SQ ...

  5. vue移动端金融UI组件库滴滴MandMobile面向金融场景设计附功能思维导图

    vue移动端金融UI组件库滴滴MandMobile面向金融场景设计附功能思维导图 Mand Mobile是面向金融场景设计的移动端组件库,基于Vue.js实现.目前已实际应用于滴滴四大金融业务板块的1 ...

  6. ajax跨域问题及相关解决方案

    1 什么是跨域 所谓的跨域是指浏览器不能执行其他网站的脚本.它是由浏览器的同源策略造成的,是浏览器施加的安全限制. 所谓同源是指,域名,协议,端口均相同: 2 什么时候会存在跨域的问题 页面访问不同源 ...

  7. javascript Template tmpl

    前两天写前端遇到很多table 和 表单需要拼接的问题 , 一堆的字符串 , 页面显得冗长,又不好维护.于是有了下文. 话不多说,上代码: 首先引用一个js文件: <script src=&qu ...

  8. opencv计算两个轮廓之间hu矩相似程度,MatchShapes

    https://blog.csdn.net/jiake_yang/article/details/52589063 [OpenCV3.3]通过透视变换矫正变形图像 https://blog.csdn. ...

  9. shell爬虫--抓取某在线文档所有页面

    在线教程一般像流水线一样,页面有上一页下一页的按钮,因此,可以利用shell写一个爬虫读取下一页链接地址,配合wget将教程所有内容抓取. 以postgresql中文网为例.下面是实例代码 #!/bi ...

  10. Link-Cut-Tree详解

    图片参考YangZhe的论文,FlashHu大佬的博客 Link-Cut-Tree实际靠的是实链剖分,重链剖分和长链剖分珂以参考树链剖分详解 Link-Cut-Tree将某一个儿子的连边划分为实边,而 ...