我想根据
ansible_distribution
变量获取特定的字典并在 with_items
中使用它,但我无法使用查找来检索字典内容。我尝试这样的事情:
- hosts: all
gather_facts: true
become: yes
vars:
distributions:
Centos:
repos:
- repo_name: 'repo1'
baseurl: 'url1'
file: 'file1'
gpgkey: 'gpgkey1'
Rocky:
repos:
- repo_name: 'repo2'
baseurl: 'url2'
file: 'file2
gpgkey: 'gpgkey2'
tasks:
- name: Add Yum Repo
become: yes
yum_repository:
name: "{{ item.repo_name }}"
description: EpelUpdate
baseurl: "{{ item.baseurl }}"
file: " {{ item.file }}"
gpgcheck: 0
gpgkey: "{{ item.gpgkey }}"
with_items:
- "{{ lookup(vars, distributions.{{ ansible_distribution }}.repos) }}" #<-- this line is wrong
我收到错误:
"msg": "template error while templating string: expected name or number. String: {{ lookup(vars, distributions.{{ ansible_distribution }}.repos) }}"
我找到了解决方案。简单
"{{ distributions[ansible_distribution].repos }}"
就够了