1.定义变量规范

  先说说怎样定义变量,变量名应该由字母、数字、下划线组成,变量名需要以字母开头,ansible内置的关键字不能作为变量名。

2.定义变量,使用关键字:vars

  定义变量用vars,调用变量用{{ 变量名 }}

---
- hosts: 192.168.10.2
remote_user: root
vars:
testvar1: testfile
tasks:
- name: test for var
file:
path: /test/{{testvar2}}
state: touch

  定义多个变量,如下操作:

    vars:
testvar1: testfile1
testvar2: testfile2

  或者使用YAML的块序列语法定义,等效,如下操作:

     vars:
- testvar1: testfile1
- testvar2: testfile2  

关于nginx的一个案例:  

  使用类似“属性”的方法来定义变量,这样可以更加清晰地分辨出所设置的变量是属于谁的,也方便在使用的时候调用相应的变量。

---
- hosts: 192.168.10.2
remote_user: root
vars:
nginx:
conf80: /etc/nginx/conf.d/80.conf
conf8080: /etc/nginx/conf.d/8080.conf
tasks:
- name: nginx configuration
file:
path: "{{nginx.conf80}}"
state: touch
- name: nginx configuration
file:
path: "{{nginx.conf8080}}"
state: touch

 调用变量时可用的两种语法:

语法一:   "{{nginx.conf80}}"

语法二:   "{{nginx['conf8080']}}"

 注意:关于是否在调用变量的时候使用双引号(" ")的问题:当调用变量直接被放在开头位置时候需要使用双引号,例如:path: "{{nginx.conf80}}"

    当调用变量时不在开头,则不需要使用双引号,例如:path: /test/{{testvar2}}

  前文中有描述过,当在playbook中为模块的参数赋值时,可以使用“冒号”,也可以使用“等号”,当使用“等号”为模块的参数赋值时,则不用考虑引用变量时是否使用“引号”的问题,示例如下:

---
- hosts: 192.168.10.2
remote_user: root
vars:
nginx:
conf80: /etc/nginx/conf.d/80.conf
conf8080: /etc/nginx/conf.d/8080.conf
tasks:
- name: nginx configuration
file:
path={{nginx.conf80}}
state=touch
- name: nginx configuration
file:
path={{nginx.conf8080}}
state=touch

  

3.变量文件分离,使用vars_file参数

  我们不在剧本中写入并调用变量,采用单独将变量写入一个文档里,实现变量文件分离,当我们需要调用某个变量文件中的变量时,只需要使用vars_file参数进行调用,被调用变量文件的路径前面要加上"-",横杠。

---
- hosts: 192.168.10.2
remote_user: root
vars_files:
- /playbook/nginx_vars.yml
tasks:
- name: nginx configuration
file:
path: "{{nginx.conf80}}"
state: touch
- name: nginx configuration
file:
path: "{{nginx.conf8080}}"
state: touch  

注意:vars关键字和vars_file关键字可以同时使用,示例如下:

    vars:
conf90: /etc/nginx/conf.d/9090.conf
vars_files:
- /playbook/nginx_vars.yml

  

ansible笔记(12):变量(一)的更多相关文章

  1. python3学习笔记12(变量作用域)

    变量作用域 参考http://www.runoob.com/python3/python3-function.html Python 中,程序的变量并不是在哪个位置都可以访问的,访问权限决定于这个变量 ...

  2. ansible笔记(12):handlers的用法

    ansible笔记():handlers的用法 这篇文章会介绍playbook中handlers的用法. 在开始介绍之前,我们先来描述一个工作场景: 当我们修改了某些程序的配置文件以后,有可能需要重启 ...

  3. ansible笔记(10):初识ansible playbook

    ansible笔记():初识ansible playbook 假设,我们想要在test70主机上安装nginx并启动,我们可以在ansible主机中执行如下3条命令 ansible test70 -m ...

  4. ansible笔记(8):常用模块之系统类模块(二)

    ansible笔记():常用模块之系统类模块(二) user模块 user模块可以帮助我们管理远程主机上的用户,比如创建用户.修改用户.删除用户.为用户创建密钥对等操作. 此处我们介绍一些user模块 ...

  5. JAVA自学笔记12

    JAVA自学笔记12 1.Scanner 1)JDK5后用于获取用户的键盘输入 2)构造方法:public Scanner(InputStream source) 3)System.in 标准的输入流 ...

  6. matlab学习笔记12单元数组和元胞数组 cell,celldisp,iscell,isa,deal,cellfun,num2cell,size

    一起来学matlab-matlab学习笔记12 12_1 单元数组和元胞数组 cell array --cell,celldisp,iscell,isa,deal,cellfun,num2cell,s ...

  7. SpringMVC:学习笔记(12)——ThreadLocal实现会话共享

    SpringMVC:学习笔记(12)——ThreadLocal实现会话共享 ThreadLocal ThreadLocal,被称为线程局部变量.在并发编程的情况下,使用ThreadLocal创建的变量 ...

  8. Python基础笔记_变量类型

     下面是W3C学习笔记 , , ) :] ]) :]) :]) :-]) :-]) ]) :]) :]) ) , , ]) :]) :]) ) , , , ]) :]) :]) ) ] = , ])) ...

  9. ES6学习笔记之变量的解构赋值

    变量的解构赋值 ES6允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构. 数组的解构赋值 以前,为变量赋值,只能直接指定值: 1 2 3 var a = 1; var b = 2; ...

  10. Spring源码学习笔记12——总结篇,IOC,Bean的生命周期,三大扩展点

    Spring源码学习笔记12--总结篇,IOC,Bean的生命周期,三大扩展点 参考了Spring 官网文档 https://docs.spring.io/spring-framework/docs/ ...

随机推荐

  1. 【WCF Restful】Post传参示范

    1.传多个参数 接口定义:(ResponseFormat与RequestFormat分别将相应参数序列化.请求参数反序列化) [OperationContract] [WebInvoke(UriTem ...

  2. python学习随笔2:python判断和循环

    1.if-else _username = 'heyue' _password = ' username = input("username:") password = input ...

  3. Python学习笔记--协程asyncio

    协程的主要功能是单线程并发运行 假设有3个耗时不一样的任务.看看协程的效果. 先来看没有使用协程情况: #!/usr/bin/python3 # -*- coding:utf-8 -*- import ...

  4. sqlserver with 递归用法

    DECLARE @companyid TABLE ( [Id] [int] ); with cte as( union all select a.Id from [base].[Company] a, ...

  5. css样式-区域内文字不会被选中

    要注意浏览器的兼容性: -webkit-user-select:none; -moz-user-select:none; -ms-user-select:none; user-select:none;

  6. ImportError: libzmq.so.5 报错

    https://pkgs.org/download/libzmq.so.5()(64bit) # rpm -ivh zeromq-4.1.4-6.el7.x86_64.rpm

  7. JavaScript实现动态打字效果

    废话不多说,上代码~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  8. sqlmap注入基本教程

    附上一个别人总结的:https://www.cnblogs.com/ichunqiu/p/5805108.html 一套基础的sqlmap语句: python sqlmap.py -u "h ...

  9. jQuery中校验时间格式的正则表达式小结

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  10. Hadoop学习之路(5)Mapreduce程序完成wordcount

    程序使用的测试文本数据: Dear River Dear River Bear Spark Car Dear Car Bear Car Dear Car River Car Spark Spark D ...