首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在ansible中循环通过var_files变量

在ansible中循环通过var_files变量
EN

Server Fault用户
提问于 2021-06-11 19:34:09
回答 1查看 4.3K关注 0票数 0

我有一本叫做角色的游戏手册,用来为网站导入apache。问题是,当我查看ports.conf时,我只看到了website1的行。Website2从未被调用过。任何帮助都将不胜感激。

代码语言:javascript
复制
---
- hosts: all
  vars_files:
   - [ "./roles/apache-vhost/vars/website1.yml", "./roles/apache-vhost/vars/website12.yml"]
  roles:
   - apache-vhost

/roles/apache-vhost/vars/website1.yml

代码语言:javascript
复制
site:
  - domain: website1
    http_port: 5000
    https_port: 6000

/角色/apache-vhost/vars/website2.yml

代码语言:javascript
复制
site:
  - domain: website2
    http_port: 5001
    https_port: 6001

游戏手册中的任务是

代码语言:javascript
复制
- 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 }}"

谢谢。

EN

回答 1

Server Fault用户

发布于 2021-06-11 21:00:25

来自第二个文件website2.yml的可变站点重写了来自第一个文件website1.yml的值。

代码语言:javascript
复制
- hosts: localhost
  vars_files:
    - website1.yml
    - website2.yml
  tasks:
    - debug:
        var: site

给出

代码语言:javascript
复制
  site:
  - domain: website2
    http_port: 5001
    https_port: 6001

您将不得不在一个循环中连接(合并)列表。

代码语言:javascript
复制
- 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

给出

代码语言:javascript
复制
  site:
  - domain: website1
    http_port: 5000
    https_port: 6000
  - domain: website2
    http_port: 5001
    https_port: 6001
票数 4
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/1066467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档