使用 pattern/wildcard 匹配读取 ansible 中的字典键值
Read dictionary key value in ansible using pattern/wildcard match
我得到的变量文件如下:
猫myvar.yml
dbname: ser1
url_ser1: url1
url_ser2: url2
dbname: ser2
my_ser1_port: 12207
my_ser2_port: 23332
为了在第一次调试中开始,我希望显示 dbname
的两个键(也可以超过 2 个键)值,即 ser1
和 ser2
然后我希望使用第一个键的值,即 ser1
来提取其键包含 ser1
的所有值,即 url1
和 12207
然后ser2
值得到url2
和23332
因此,我需要如下输出:
The dbname are ser1 & ser2
dbname `ser1` URL is url1 and port is 12207
dbname `ser2` URL is url2 and port is 23332
下面是我的剧本,但是我不知道如何读取这样的变量:
---
- name: ReadJsonfile
hosts: localhost
tasks:
- include_vars: myvar.yml
- debug:
msg: "{{ dbname }}"
以上仅打印第二个 dbname 值,即 ser2
而不是我所需要的两者。
- debug:
msg: "{{ '*'+item+'*' }}"
loop: "{{ dbname }}"
以上失败并出现错误。
你能否建议我如何从 yaml 中获取变量的所需值。
你必须这样写你的myvar.yml:
dbname: ['ser1', 'ser2']
url_ser1: url1
url_ser2: url2
my_ser1_port: 12207
my_ser2_port: 23332
所以你可以遍历 dbname...
- name: Reproduce issue
hosts: localhost
gather_facts: no
tasks:
- include_vars: variables.yml
- debug:
msg: "dbname {{ item }} URL is {{ url }} and port is {{ port }}"
loop: "{{ dbname }}"
vars:
port: "{{ lookup('vars', 'my_' + item + '_port' ) }}"
url: "{{ lookup('vars', 'url_' + item) }}"
when: hostvars[ansible_host]['my_' + item + '_port'] is defined and
hostvars[ansible_host]['url_' + item] is defined
所以lookup('vars', 'my_' + item + '_port' )
= hostvars[ansible_host]['my_' + item + '_port']
结果:
ok: [localhost] => (item=ser1) => {
"msg": "dbname ser1 URL is url1 and port is 12207"
}
ok: [localhost] => (item=ser2) => {
"msg": "dbname ser2 URL is url2 and port is 23332"
}
我得到的变量文件如下:
猫myvar.yml
dbname: ser1
url_ser1: url1
url_ser2: url2
dbname: ser2
my_ser1_port: 12207
my_ser2_port: 23332
为了在第一次调试中开始,我希望显示 dbname
的两个键(也可以超过 2 个键)值,即 ser1
和 ser2
然后我希望使用第一个键的值,即 ser1
来提取其键包含 ser1
的所有值,即 url1
和 12207
然后ser2
值得到url2
和23332
因此,我需要如下输出:
The dbname are ser1 & ser2
dbname `ser1` URL is url1 and port is 12207
dbname `ser2` URL is url2 and port is 23332
下面是我的剧本,但是我不知道如何读取这样的变量:
---
- name: ReadJsonfile
hosts: localhost
tasks:
- include_vars: myvar.yml
- debug:
msg: "{{ dbname }}"
以上仅打印第二个 dbname 值,即 ser2
而不是我所需要的两者。
- debug:
msg: "{{ '*'+item+'*' }}"
loop: "{{ dbname }}"
以上失败并出现错误。
你能否建议我如何从 yaml 中获取变量的所需值。
你必须这样写你的myvar.yml:
dbname: ['ser1', 'ser2']
url_ser1: url1
url_ser2: url2
my_ser1_port: 12207
my_ser2_port: 23332
所以你可以遍历 dbname...
- name: Reproduce issue
hosts: localhost
gather_facts: no
tasks:
- include_vars: variables.yml
- debug:
msg: "dbname {{ item }} URL is {{ url }} and port is {{ port }}"
loop: "{{ dbname }}"
vars:
port: "{{ lookup('vars', 'my_' + item + '_port' ) }}"
url: "{{ lookup('vars', 'url_' + item) }}"
when: hostvars[ansible_host]['my_' + item + '_port'] is defined and
hostvars[ansible_host]['url_' + item] is defined
所以lookup('vars', 'my_' + item + '_port' )
= hostvars[ansible_host]['my_' + item + '_port']
结果:
ok: [localhost] => (item=ser1) => {
"msg": "dbname ser1 URL is url1 and port is 12207"
}
ok: [localhost] => (item=ser2) => {
"msg": "dbname ser2 URL is url2 and port is 23332"
}