我有一个剧本,它调用一个角色,并应该为网站导入 apache 变量。问题是,当我查看 ports.conf 时,我只看到 website1 的行。Website2 从未被调用。任何帮助都将不胜感激。
---
- hosts: all
vars_files:
- [ "./roles/apache-vhost/vars/website1.yml", "./roles/apache-vhost/vars/website12.yml"]
roles:
- apache-vhost
/角色/apache-vhost/vars/website1.yml
site:
- domain: website1
http_port: 5000
https_port: 6000
./roles/apache-vhost/vars/website2.yml
site:
- domain: website2
http_port: 5001
https_port: 6001
剧本中的任务是
- name: add http Listeners to ports.conf
lineinfile:
path: /etc/httpd/conf.d/ports.conf
line: 'Listen {{item.http_port}} #{{ item.domain}}'
loop: "{{ site }}"
- name: add https Listeners to ports.conf
lineinfile:
path: /etc/httpd/conf.d/ports.conf
line: 'Listen {{item.https_port}} #{{ item.domain}}'
loop: "{{ site }}"
谢谢。
答案1
变量地点来自第二个文件 website2.yml 覆盖来自第一个文件 website1.yml 的值,例如
- hosts: localhost
vars_files:
- website1.yml
- website2.yml
tasks:
- debug:
var: site
给出
site:
- domain: website2
http_port: 5001
https_port: 6001
您必须在循环中连接(合并)列表,例如
- hosts: localhost
tasks:
- set_fact:
site: "{{ site|default([]) + x.site }}"
loop:
- website1.yml
- website2.yml
vars:
x: "{{ lookup('file', item)|from_yaml }}"
- debug:
var: site
给出
site:
- domain: website1
http_port: 5000
https_port: 6000
- domain: website2
http_port: 5001
https_port: 6001