Ansible-迭代字典列表

Ansible-迭代字典列表,ansible,jinja2,Ansible,Jinja2,我构建了下面的列表,但是我没有成功地遍历它。 我应该与_项目一起使用吗?有什么元素?还是别的什么 我的目标是迭代清单中的所有主机,获取它们的名称和IP,最后打印出来 - set_fact: list_of_hosts: | {% set myList = [] %} {% for host in groups['all'] %} {% set ignored = myList.extend([{'server_name': host, 'server_

我构建了下面的列表,但是我没有成功地遍历它。 我应该与_项目一起使用吗?有什么元素?还是别的什么

我的目标是迭代清单中的所有主机,获取它们的名称和IP,最后打印出来

- set_fact:
    list_of_hosts: |
      {% set myList = [] %}
      {% for host in groups['all'] %}
      {% set ignored = myList.extend([{'server_name': host, 'server_ip': hostvars[host].ansible_eth0.ipv4.address }]) %}
      {% endfor %}
      {{ myList }}


- debug: msg="{{ item.server_name }}"
  with_items: "{{ list_of_hosts }}"
以下是我调试时的列表:

TASK [common : debug] ************************************************************************************************

ok: [my1stServer] => {
    "msg": "            [{'server_ip': u'192.168.0.1', 'server_name': u'my1stServer'}, {'server_ip': u'192.168.0.2', 'server_name': u'my2ndServer'}]\n"
}
这是一个错误,但它不是真正相关的:

fatal: [my1stServer]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible.vars.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'server_name'\n\nThe error appears to have been in 'hosts.yml': line 19, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- debug: msg=\"{{ item.server_name }}\"\n  ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes.  Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}

请原谅我的直言不讳,但拟议的实施使我们努力理解这个想法的实际含义。这很简单:为根据各种条件选择的主机列表打印
hostvars[host]
中存在的一些变量。 如果我们使实现接近于这个想法,那么实现就会更简单

所以我要做的是创建一个由组成员选择的主机列表,或者可能是“手工选择”的主机列表,实际执行我刚才写的操作:)。 考虑这个任务列表:

# this task creates an empty list
- name: create my_cool_list
  set_fact:
    my_cool_list: []
# this task adds to the list all hosts in groups we're iterating over
- name: update my cool list with whole groups
  set_fact: '{{my_cool_list + groups[curr_grp]}}'
  with_items:
  - grp1
  - grp2
  loop_control:
    loop_var: curr_grp
# this task adds to the list all hosts we're iterating over
- name: update my cool list with specific hosts
  set_fact: '{{my_cool_list + [curr_node]}}'
  with_items:
  - node001
  - node101
  loop_control:
    loop_var: curr_node

# now we can iterate over the list, accessing specific fields on each host
- name: go over my cool list and print ansible_init_mgr
  debug:
    msg: 'hostvars["{{curr_host}}"].ansible_init_mgr: {{hostvars[curr_host].ansible_init_mgr}}'
  with_items: '{{my_cool_list|default([], true)|list}}'
此外,您可以在以下情况下添加安全性:,方法是验证正在访问的密钥是否已定义

而且,要打印关于每个主机的变量选择,您应该使用jinja filter
map('extract',…)

如果你想增加可读性,你最好写一个过滤器插件,它可以完成上述工作,并以可读的方式隐藏迭代的丑陋之处,这样你就可以:

或者(对于通用方法,即不重命名属性)

或特定方法(以便使用特定的硬编码属性重新映射…)


另外,您最好使用
ansible\u default\u ipv4.address
- name: print some vars from each host
  debug:
    msg: {'server_name': '{{hostvars[curr_node]|selectattr("ansible_hostname")}}', 'ip_address': '{{hostvars[curr_node]|selectattr("ansible_eth0.ipv4.address")}}'}
  with_items: '{{my_cool_list|default([], true)|list}}'
  loop_control:
    loop_var: curr_node
- name: print some vars from each host
  debug:
    msg: '{{my_cool_list|multi_select_in_dict(["ansible_hostname", "ansible_eth0.ipv4.address")}}'
- name: print some vars from each host
  debug:
    msg: '{{my_cool_list|my_cool_filter}}'