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]#
相关推荐
Profile排查笔记3 小时前
指纹浏览器推荐:用一套验收清单筛选 Profile、代理与自动化能力
前端·人工智能·后端·自动化
米糕闯编程3 小时前
鱼香ros2(一)在ubuntu中安装ros2
linux·运维·ubuntu·机器学习
Huangjin007_3 小时前
【Linux 系统篇(十八)】进程(六) :环境变量
linux·运维·服务器
ShineWinsu3 小时前
对于MySQL:数据库的操作的解析
linux·数据库·c++·mysql·面试·笔试·库的操作
cnnews3 小时前
Ubuntu 编译 postmarketOS
linux·运维·arm开发·ubuntu·github·音视频
鹿鹿学长3 小时前
发题前三天,组委会在过三道关:赛题七渠道首发、知网统一收卷、AI 详情 PDF 首进支撑材料
python·自动化
w01_02_033 小时前
linux, 桌面, 自启动。
linux·运维·服务器
康一夏3 小时前
Git Commit 规范实践:Angular Commit 规范 + Commitlint 自动化验证
git·自动化·commit msg·commit规范
云飞云共享云桌面4 小时前
SolidWorks+AutoCAD,多并发。可以多人共享一台设计服务器吗
运维·服务器·3d·自动化·制造
从零开始的嵌入式之旅4 小时前
day36
linux·c语言·经验分享·笔记·嵌入式硬件