inventory针对主机组设置变量 host_vars group_vars playbook执行时传入值 Ansible-register
inventory针对主机组设置变量[rootansible ansible]# vi /etc/ansible/hosts [rootansible ~]# 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 unameyun urlhttp://192.168.92.20:6100 [webservers:vars] yun123456 yun2123456789 # 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 [rootansible ~]# vi an-5.yml [rootansible ~]# cat an-5.yml - hosts: webservers tasks: - name: 输出针对主机组的变量 debug: msg: {{ yun }}, {{ yun2 }} [rootansible ~]# 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 : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0当文件内同时存在针对主机和针对主机组的变量针对主机的变量优先级更高优先级vars files vars inventoryhost_vars[rootansible ~]# mkdir host_vars [rootansible ~]# cd host_vars/ [rootansible host_vars]# ls [rootansible host_vars]# vi 192.168.92.20 [rootansible host_vars]# cat 192.168.92.20 hostname: 20 hostage: 3天 [rootansible host_vars]# cd [rootansible ~]# vi an-6.yml [rootansible ~]# cat an-6.yml - hosts: webservers tasks: - name: 输出host_vars定义的变量 debug: msg: {{ hostname }}, {{ hostage }} [rootansible ~]# 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 : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0你运行的playbook文件内指定的host_vars变量必须和host一vats目录处于相同位置,不然找不到ansible-playbook运行不了优先级host_vars INVENTORYgroup_vars[rootansible ~]# mkdir group_vars [rootansible ~]# vi group_vars/webservers [rootansible ~]# cat group_vars/webservers web: webservers [rootansible ~]# vi an-6.yml [rootansible ~]# cat an-6.yml - hosts: webservers tasks: - name: 输出host_vars定义的变量 debug: msg: {{ hostname }}, {{ hostage }},{{ web }} [rootansible ~]# 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 : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0 [rootansible ~]#你运行的playbook,(yml文件)必须和group_vars目录处于相同位置,不然找不到ansible-playbook运行不了优先级VARS filesvarshost varsINVENTORY GROUP_VARSplaybook执行时传入值[rootansible ~]# vi an-7.yml [rootansible ~]# cat an-7.yml - hosts: webservers tasks: - name: 输出传入的变量 debug: msg: 服务: {{ server }}, 端口: {{ port }} [rootansible ~]# ansible-playbook an-7.yml -e servernginx -e port80 [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 : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0 [rootansible ~]# rm -rf an-7.yml [rootansible ~]# vi an-7.yml [rootansible ~]# cat an-7.yml - hosts: webservers vars_files: - ./bianliang.yml tasks: - name: 输出传入的变量 debug: msg: 服务: {{ server }}, 端口: {{ port }}, 网址: {{ url }} [rootansible ~]# cat bianliang.yml uname: yun uid: 9000 job: projector url: www.baidu.com [rootansible ~]# ansible-playbook an-7.yml -e servernginx -e port80 [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 : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0传值的优先级比vars_file的优先级更高优先级执行时传入值vars filesvarshost_varsinventorygroup_varsAnsible-register[rootansible ~]# vi an-8.yml [rootansible ~]# 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 }} [rootansible ~]# 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 : ok3 changed1 unreachable0 failed0 skipped0 rescued0 ignored0