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]#
相关推荐
结衣结衣.3 小时前
【Linux】命名管道的妙用:实现进程控制与实时字符交互
linux·运维·开发语言·学习·操作系统·交互
IMPYLH3 小时前
Linux 的 groups 命令
linux·运维·服务器·bash
Lugas Luo3 小时前
SATA 协商流程深度分析 (基于 libata 与 AHCI 控制器)
linux·嵌入式硬件
RisunJan3 小时前
Linux命令-mysqladmin(MySQL服务器管理客户端)
linux·服务器·mysql
不才小强4 小时前
Linux网络调试工具:curl与抓包实战
linux·运维·网络
我还为发觉4 小时前
Linux 监控可视化|Prometheus+Node Exporter 一键部署教程
linux·运维·prometheus
骆驼爱记录4 小时前
Word双语目录制作全攻略
自动化·word·新人首发
zylyehuo4 小时前
在 Ubuntu 下进行磁盘分卷
linux
sdm0704275 小时前
Linux-库制作与原理
linux·c++·操作系统