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