inventory针对主机组设置变量 host_vars group_vars playbook执行时传入值 Ansible-register

inventory针对主机组设置变量

复制代码
[root@ansible ansible]# vi /etc/ansible/hosts
[root@ansible ~]# cat /etc/ansible/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
[webservers:vars]
yun=123456
yun2=123456789
# 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 ~]# vi an-5.yml 
[root@ansible ~]# cat an-5.yml
- hosts: webservers
  tasks:
    - name: 输出针对主机组的变量
      debug:
        msg: "{{ yun }}, {{ yun2 }}" 
[root@ansible ~]# ansible-playbook an-5.yml

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

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

TASK [输出针对主机组的变量] *****************************************************************************************************************************
ok: [192.168.92.20] => {
    "msg": "123456, 123456789"
}

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

当文件内同时存在针对主机和针对主机组的变量,针对主机的变量优先级更高

优先级:vars files> vars > inventory

host_vars

复制代码
[root@ansible ~]# mkdir host_vars
[root@ansible ~]# cd host_vars/
[root@ansible host_vars]# ls
[root@ansible host_vars]# vi 192.168.92.20 
[root@ansible host_vars]# cat 192.168.92.20
hostname: 20
hostage: 3天 
[root@ansible host_vars]# cd
[root@ansible ~]# vi an-6.yml 
[root@ansible ~]# cat an-6.yml
- hosts: webservers
  tasks:
    - name: 输出host_vars定义的变量
      debug:
        msg: "{{ hostname }}, {{ hostage }}"
[root@ansible ~]# ansible-playbook an-6.yml

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

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

TASK [输出host_vars定义的变量] **************************************************************************************************************************
ok: [192.168.92.20] => {
    "msg": "20, 3天"
}

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

你运行的playbook文件内指定的host_vars变量必须和host一vats目录处于相同位置,不然找不到,ansible-playbook运行不了

优先级:host_vars > INVENTORY

group_vars

复制代码
[root@ansible ~]# mkdir group_vars
[root@ansible ~]# vi group_vars/webservers 
[root@ansible ~]# cat group_vars/webservers
web: webservers 
[root@ansible ~]# vi an-6.yml
[root@ansible ~]# cat an-6.yml
- hosts: webservers
  tasks:
    - name: 输出host_vars定义的变量
      debug:
        msg: "{{ hostname }}, {{ hostage }},{{ web }}" 
[root@ansible ~]# ansible-playbook an-6.yml

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

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

TASK [输出host_vars定义的变量] **************************************************************************************************************************
ok: [192.168.92.20] => {
    "msg": "20, 3天,webservers"
}

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

[root@ansible ~]#

你运行的playbook,(yml文件)必须和group_vars目录处于相同位置,不然找不到,ansible-playbook运行不了

优先级:VARS files>vars>host vars>INVENTORY >GROUP_VARS

playbook执行时传入值

复制代码
[root@ansible ~]# vi an-7.yml
[root@ansible ~]# cat an-7.yml
- hosts: webservers
  tasks:
    - name: 输出传入的变量
      debug:
        msg: "服务: {{ server }}, 端口: {{ port }}"
[root@ansible ~]# ansible-playbook an-7.yml -e "server=nginx" -e "port=80"
[WARNING]: Found variable using reserved name: port

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

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

TASK [输出传入的变量] ***********************************************************************************************************************************
ok: [192.168.92.20] => {
    "msg": "服务: nginx, 端口: 80"
}

PLAY RECAP **********************************************************************************************************************************************
192.168.92.20              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[root@ansible ~]# rm -rf an-7.yml
[root@ansible ~]# vi an-7.yml 
[root@ansible ~]# cat an-7.yml
- hosts: webservers
  vars_files:
    - ./bianliang.yml
  tasks:
    - name: 输出传入的变量
      debug:
        msg: "服务: {{ server }}, 端口: {{ port }}, 网址: {{ url }}"
[root@ansible ~]# cat bianliang.yml
uname: yun
uid: 9000
job: projector
url: www.baidu.com
[root@ansible ~]# ansible-playbook an-7.yml -e "server=nginx" -e "port=80"
[WARNING]: Found variable using reserved name: port

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

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

TASK [输出传入的变量] ***********************************************************************************************************************************
ok: [192.168.92.20] => {
    "msg": "服务: nginx, 端口: 80, 网址: www.baidu.com"
}

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

传值的优先级比vars_file的优先级更高

优先级:执行时传入值>vars files>vars>host_vars>inventory>group_vars

Ansible-register

复制代码
[root@ansible ~]# vi an-8.yml
[root@ansible ~]# cat an-8.yml
# 1. 写一个简单的任务
# 2. 将任务的结果输出给到某一个变量
# 3. 再调用变量进行输出
- hosts: webservers
  tasks:
    - name: 查询操作
      shell: yum -y install net-tools && netstat -anpt
      register: status

    - name: 调用status变量输出
      debug:
        msg: "{{ status }}" 
[root@ansible ~]# ansible-playbook an-8.yml

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

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

TASK [查询操作] *****************************************************************************************************************************************
changed: [192.168.92.20]

TASK [调用status变量输出] *******************************************************************************************************************************
ok: [192.168.92.20] => {
    "msg": {
        "changed": true,
        "cmd": "yum -y install net-tools && netstat -anpt",
        "delta": "0:00:00.601279",
        "end": "2026-03-28 17:30:30.401668",
        "failed": false,
        "msg": "",
        "rc": 0,
        "start": "2026-03-28 17:30:29.800389",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "Last metadata expiration check: 1:44:47 ago on Sat Mar 28 15:45:43 2026.\nPackage net-tools-2.0-0.64.20160912git.el9.x86_64 is already installed.\nDependencies resolved.\nNothing to do.\nComplete!\nActive Internet connections (servers and established)\nProto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    \ntcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      725/sshd: /usr/sbin \ntcp        0      0 192.168.92.20:22        192.168.92.19:46040     ESTABLISHED 4998/sshd: root [pr \ntcp6       0      0 :::22                   :::*                    LISTEN      725/sshd: /usr/sbin \ntcp6       0      0 :::80                   :::*                    LISTEN      723/httpd           ",
        "stdout_lines": [
            "Last metadata expiration check: 1:44:47 ago on Sat Mar 28 15:45:43 2026.",
            "Package net-tools-2.0-0.64.20160912git.el9.x86_64 is already installed.",
            "Dependencies resolved.",
            "Nothing to do.",
            "Complete!",
            "Active Internet connections (servers and established)",
            "Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    ",
            "tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      725/sshd: /usr/sbin ",
            "tcp        0      0 192.168.92.20:22        192.168.92.19:46040     ESTABLISHED 4998/sshd: root [pr ",
            "tcp6       0      0 :::22                   :::*                    LISTEN      725/sshd: /usr/sbin ",
            "tcp6       0      0 :::80                   :::*                    LISTEN      723/httpd           "
        ]
    }
}

PLAY RECAP **********************************************************************************************************************************************
192.168.92.20              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
相关推荐
JZC_xiaozhong10 小时前
数据不互通、审批慢?企业多系统智能协同与流程自动化解决方案
运维·自动化·流程管理·流程自动化·数据集成与应用集成·流程监控·流程可视化设计
爱学习的小囧10 小时前
ESXi 8.0 原生支持 NVMe 固态硬盘吗?VMD 配置详解教程
linux·运维·服务器·esxi·esxi8.0
坚持就完事了10 小时前
Linux中的变量
linux·运维·服务器
hERS EOUS11 小时前
nginx 代理 redis
运维·redis·nginx
Cat_Rocky11 小时前
利用Packet Tracer网络实验
linux·运维·服务器
zhensherlock11 小时前
Protocol Launcher 系列:Trello 看板管理的协议自动化
前端·javascript·typescript·node.js·自动化·github·js
嵌入式×边缘AI:打怪升级日志11 小时前
Linux 驱动实战:SR501 人体红外传感器驱动开发与调试全记录
linux·运维·驱动开发
正点原子11 小时前
【正点原子Linux连载】第三章 U-Boot使用 摘自【正点原子】ATK-DLRK3568嵌入式Linux驱动开发指南
linux·运维·驱动开发
忍冬行者12 小时前
MongoDB 三节点副本集离线部署运维手册
运维·数据库·mongodb
爱学习的小囧12 小时前
ESXi VMkernel 端口 MTU 最佳设置详解
运维·服务器·网络·php·虚拟化