我有一组定义FQDN的变量。
domains:
- erp: erp.mycompany.com
- crm: crm.mycompany.com
- git: git.mycompany.com
实际上,我都需要遍历它们并访问它们(在模板文件中)。因此像domains.erp
那样访问它们就像一个魅力。但我无法回答这些问题。
显然,如果我这样做:
- name: Print domains
debug:
msg: test {{ item }}
with_items:
- "{{ domains }}"
它会打印键和值...如果我这样做:
- name: Print domains
debug:
msg: test {{ domains[{{ item }}] }}
with_items:
- "{{ domain }}"
但这不起作用。我也试过了文档中提到的hashes form,但也没有运气......
答案 0 :(得分:1)
最后,我不得不使用dict。
它第一次没有用,因为与with_items
不同,with_dict
的每个项目各自都在自己的行上,-
是一个没有domains:
erp:
address: erp.mycompany.com
crm:
address: crm.mycompany.com
git:
address: git.mycompany.com
# used by letsencrypt
webserverType: apache2
withCerts: true
tasks:
- name: Print phone records
debug:
msg: "{{ item.value.address }}"
with_dict: "{{ domains }}"
# I can still access a given domain by its name when needed like so:
{{ domains.erp.address }}
的单行内容,在元素循环之前通过
<?php
$conn = new mysqli ("localhost", "newuser", "password", "testdb");
echo "Database connection successful";
echo '<br />';
$test1 = "a";
$test2 = "a";
$test3 = "a";
$test4 = "a";
$test5 = "a";
$query = "INSERT INTO testtable (testcolumn1, testcolumn2, testcolumn3, testcolumn4, testcolumn5) VALUES ('$test1', '$test2', '$test3', '$test4', '$test5');";
if ($conn->query($query) === TRUE) {
echo "passed";
} else {
echo "failed";
}
答案 1 :(得分:0)
看起来你弄清楚了你的问题。您的原始尝试使用不包含相同键的字典列表,这使得难以在每个列表项之间统一访问值。
您的第二个解决方案会创建一个字典,其中的键引用其他字典。
如果您仍想使用列表,则提供的解决方案不是您发布的解决方案:
- hosts: localhost
vars:
domains:
- name: erp
address: erp.mycompany.com
- name: crm
address: crm.mycompany.com
- name: git
address: git.mycompany.com
tasks:
- name: Print phone records
debug:
msg: "{{ item.address }}"
with_items: "{{ domains }}"
对我而言,这种方法更简单,但您的第二种方法也适用。