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]#
相关推荐
Cat_Rocky10 分钟前
利用Packet Tracer网络实验
linux·运维·服务器
zhensherlock11 分钟前
Protocol Launcher 系列:Trello 看板管理的协议自动化
前端·javascript·typescript·node.js·自动化·github·js
嵌入式×边缘AI:打怪升级日志28 分钟前
Linux 驱动实战:SR501 人体红外传感器驱动开发与调试全记录
linux·运维·驱动开发
正点原子35 分钟前
【正点原子Linux连载】第三章 U-Boot使用 摘自【正点原子】ATK-DLRK3568嵌入式Linux驱动开发指南
linux·运维·驱动开发
Qbw20041 小时前
【Linux】进程地址空间
linux·c++
智慧地球(AI·Earth)2 小时前
用AI重构Python开发:从API调用到本地部署,代码全流程自动化实战
python·自动化·api
jamon_tan2 小时前
linux下lvgl8.3动态库编译
linux
子牙老师4 小时前
软件虚拟化 vs 硬件虚拟化
linux·性能优化·云计算
旦莫4 小时前
测试工程师如何用AI生成测试用例?我的提示词模板分享
人工智能·python·测试开发·自动化·测试用例·ai测试
rayyy95 小时前
Linux 下标准的 libX.so 软链接生成
linux