是否可以在Ansible中组合单独的Vars文件中提到的相同变量名称的值

发布于 2025-02-13 22:07:40 字数 626 浏览 1 评论 0原文

app2.yml具有相同的变量名称,

cat app1.yml
dbconn:
  - host1 port1

cat app2.yml
dbconn:
  - host4 port4
  - host5 port5

我有两个变量文件app1.yml

   - name: Load Variable Files
     include_vars: "{{ playbook_dir }}/{{ item }}.yml"
     loop: 
       - app1
       - app2
     run_once: yes

   - debug:
       msg: "{{ dbconn }}"

即代码> dbconn 具有来自两个变量文件的值,即

  - host1 port1
  - host4 port4
  - host5 port5

,它仅打印最后加载的变量文件值IE -OSTOR4 port4 port4 and - host5 port5

您可以建议吗?

I have two variable files app1.yml and app2.yml having the same variable name viz dbconn

cat app1.yml
dbconn:
  - host1 port1

cat app2.yml
dbconn:
  - host4 port4
  - host5 port5

cat main.yml

   - name: Load Variable Files
     include_vars: "{{ playbook_dir }}/{{ item }}.yml"
     loop: 
       - app1
       - app2
     run_once: yes

   - debug:
       msg: "{{ dbconn }}"

My expectation is that variable dbconn to have values from both the variable files i.e

  - host1 port1
  - host4 port4
  - host5 port5

However, it prints only the last loaded variable file values i.e - host4 port4 and - host5 port5

Can you please suggest?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悍妇囚夫 2025-02-20 22:07:43

加入循环中的列表。例如,

    - set_fact:
        dbconn: "{{ dbconn|d([]) + (lookup('file', item)|from_yaml).dbconn }}"
      loop:
        - app1.yml
        - app2.yml

给予

dbconn:
  - host1 port1
  - host4 port4
  - host5 port5

Concatenate the lists in the loop. For example,

    - set_fact:
        dbconn: "{{ dbconn|d([]) + (lookup('file', item)|from_yaml).dbconn }}"
      loop:
        - app1.yml
        - app2.yml

gives

dbconn:
  - host1 port1
  - host4 port4
  - host5 port5
香草可樂 2025-02-20 22:07:43

您有一个特定的问题,因此本剧本是解决您的问题的解决方案:

- name: "tips3"
  hosts: localhost
  vars:
    appx: [app1, app2]
  tasks:
    - name: Load Variable Files
      include_vars: 
        file: "{{ playbook_dir }}/{{ item }}.yml"
        name: "{{ item }}"
      loop: "{{ appx }}"
      run_once: yes
      
    - name: group var
      set_fact:
        dbconn: "{{dbconn | d([]) + lookup('vars', item )['dbconn'] }}"
      loop: "{{ appx }}"

    - debug:
        msg: "{{ dbconn }}"

第一个任务给出了变量:

app1:
  dbconn:
    - host1 port1
app2:
  dbconn:
    - host4 port4
    - host5 port5

第二个任务concats一个列表中的所有列表dbConn:

最终结果:

ok: [localhost] => {
    "msg": [
        "host1 port1",
        "host4 port4",
        "host5 port5"
    ]
}

you have a specific problem so this playbook is a solution to resolve your problem:

- name: "tips3"
  hosts: localhost
  vars:
    appx: [app1, app2]
  tasks:
    - name: Load Variable Files
      include_vars: 
        file: "{{ playbook_dir }}/{{ item }}.yml"
        name: "{{ item }}"
      loop: "{{ appx }}"
      run_once: yes
      
    - name: group var
      set_fact:
        dbconn: "{{dbconn | d([]) + lookup('vars', item )['dbconn'] }}"
      loop: "{{ appx }}"

    - debug:
        msg: "{{ dbconn }}"

the first task gives as variables:

app1:
  dbconn:
    - host1 port1
app2:
  dbconn:
    - host4 port4
    - host5 port5

and the second task concats all lists in one list dbconn:

final result:

ok: [localhost] => {
    "msg": [
        "host1 port1",
        "host4 port4",
        "host5 port5"
    ]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文