Ansible变量介绍 vars变量 inventory针对主机设置变量

Ansible变量介绍

什么是变量

复制代码
一个动态获取数据的对象,在ansible中,提供指定名称定义,然后预存数据当指令或者yml操作
的时候,可以调用里面的数据

变量的定义方式

复制代码
在Ansible内,定义变量有三种方式
    - 通过命令行传递变量参数定义 
    - 在playbook文件定义变量
        - 通过vars:定义变量
        - 通过vars_files 定义变量
    - 通过Inventory在主机组或者单个主机中设置变量
        - 通过hosts_vars:对主机进行定义
        - 通过group_vars:对主机组进行定义

优先级

复制代码
playbook内可以定义多种变量,但是传递和执行的时候是有优先级的关系的

vars变量

复制代码
[root@ansible ~]# vi an-3.yml
[root@ansible ~]# cat an-3.yml
#vars定义变量
#关键字:vars:变量名称
#通过vars定义的变量只能在该文件内使用
#变量名也有要求:变量的首字母必须是英文或者是下划线

#案例:定义变量并且调用变量

- hosts: webservers
  vars:
    id: 666
    age: 21
    uname: yun
  tasks:
    - name: 通过debug模块内的msg参数输出变量
      debug:
        msg: "用户id: {{ id }}, 用户年纪: {{ age }}, 用户名: {{ uname }}"
[root@ansible ~]# ansible-playbook an-3.yml

PLAY [webservers] ***************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************
ok: [192.168.92.20]

TASK [通过debug模块内的msg参数输出变量] *****************************************************************************************************************
ok: [192.168.92.20] => {
    "msg": "用户id: 666, 用户年纪: 21, 用户名: yun"
}

PLAY RECAP **********************************************************************************************************************************************
192.168.92.20              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@ansible ~]#

inventory针对主机设置变量

复制代码
[root@ansible ansible]# vi hosts
[root@ansible ansible]# vi an-5.yml
[root@ansible ansible]# cat hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# 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
[webservers]
192.168.92.20 uname=yun url=http://192.168.92.20:6100
# If you have multiple hosts following a pattern, you can specify
# them like this:

## www[001:006].example.com

# You can also use ranges for multiple hosts:

## db-[99:101]-node.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


# Ex4: Multiple hosts arranged into groups such as 'Debian' and 'openSUSE':

## [Debian]
## alpha.example.org
## beta.example.org

## [openSUSE]
## green.example.com
## blue.example.com

[root@ansible ansible]# cat an-5.yml
#验证inventory变量
- hosts: webservers
  tasks:
    - name: 利用debug输出变量
      debug:
        msg: "用户名: {{ uname }}, 网址: {{ url }}"
[root@ansible ansible]# ansible-playbook an-5.yml

PLAY [webservers] ***************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************
ok: [192.168.92.20]

TASK [利用debug输出变量] ********************************************************************************************************************************
ok: [192.168.92.20] => {
    "msg": "用户名: yun, 网址: http://192.168.92.20:6100"
}

PLAY RECAP **********************************************************************************************************************************************
192.168.92.20              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@ansible ansible]#
相关推荐
见合八方8 分钟前
【滤波器】用于红外微型光谱仪的可调谐MEMS-FP滤光片-综述
自动化·soa·光通信·激光雷达·半导体光放大器
Urbano1 小时前
工装外套全制作流程、工序痛点及自动化设备升级方案
运维·自动化
谢平康2 小时前
解决用 rm 报bash: /usr/bin/rm: Argument list too long错
linux·运维·运维开发
hj2862514 小时前
Linux 网络服务综合笔记(概念 + 命令 + 实操案例)2
linux·运维·网络
what_20184 小时前
Linux 磁盘 (查看、划分、inode)
linux·运维·服务器
海兰4 小时前
【web应用】Excel 项目数据自动化分析系统(AI 驱动分析)详细设计与部署指南(附源代码)
前端·人工智能·自动化·excel
2739920294 小时前
GDB调试(Linux)
linux
凡人叶枫4 小时前
Effective C++ 条款23:宁以 non-member、non-friend 替换 member 函数
linux·开发语言·c++·嵌入式开发
不会C语言的男孩4 小时前
Linux 系统编程 · 第 4 章:文件属性与元数据
linux·c语言·开发语言
小生不才yz5 小时前
Shell脚本精读 · S02-03 | 词拆分、通配符与未加引号的变量
linux