要列出的Hostvars或要在以后的任务中匹配的词典(key:value)

问题描述 投票:0回答:1

我有一个基本角色,它将使用group_vars中多个文件的hostvars(以其组名命名,以查看特定办公室的站点代码和位置;

  - vars:
      code_location: []
    set_fact:
      code_location: "{{ code_location | combine({item[0]: item[1]}) }}"
    with_nested:
      - "{{ hostvars[inventory_hostname].sitecode }}"
      - "{{ hostvars[inventory_hostname].location }}"
    delegate_to: 127.0.0.1

下面是该特定set_fact的输出,并确切地显示了我想要的样子;问题是将其传递给单个可重复使用的欲望,该欲望将与key | value即[>]匹配

set_fact:
  site_code: "{{ code_location.0 }}" # e.g. - ams01
when: "{{ code_location.1 == var_passed }}" # e.g. - Amsterdam

我不知道要把这个完整列表放入一个可用的字典或列表中,该字典或列表不依赖于其来源主机,并且不会被嵌套循环中的最后一个条目覆盖。

PLAY [Locations -> Sitecodes]
TASK [set_fact]
ok: [ams01-host -> 127.0.0.1] => (item=[u'ams01', u'Amsterdam'])
ok: [aus01-host -> 127.0.0.1] => (item=[u'aus01', u'Austin'])
ok: [blr01-host -> 127.0.0.1] => (item=[u'blr01', u'Bangalore'])
ok: [dub01-host -> 127.0.0.1] => (item=[u'dub01', u'Dublin'])
ok: [dus01-host -> 127.0.0.1] => (item=[u'dus01', u'Dusseldorf'])
ok: [gru01-host -> 127.0.0.1] => (item=[u'gru01', u'Sao Paulo'])
ok: [hyd01-host -> 127.0.0.1] => (item=[u'hyd01', u'Hyderabad'])
ok: [lon01-host -> 127.0.0.1] => (item=[u'lon01', u'London'])
ok: [nrt01-host -> 127.0.0.1] => (item=[u'nrt01', u'Tokyo'])
ok: [nyc01-host -> 127.0.0.1] => (item=[u'nyc01', u'New York'])
ok: [par01-host -> 127.0.0.1] => (item=[u'par01', u'Paris'])
ok: [scf01-host -> 127.0.0.1] => (item=[u'scf01', u'Scottsdale'])
ok: [sea01-host -> 127.0.0.1] => (item=[u'sea01', u'Seattle'])
ok: [sfo01-host -> 127.0.0.1] => (item=[u'sfo01', u'San Francisco'])
ok: [sin01-host -> 127.0.0.1] => (item=[u'sin01', u'Singapore'])
ok: [sjc01-host -> 127.0.0.1] => (item=[u'sjc01', u'San Jose'])
ok: [sql01-host -> 127.0.0.1] => (item=[u'sql01', u'San Mateo'])
ok: [stm01-host -> 127.0.0.1] => (item=[u'stm01', u'Stamford'])
ok: [syd01-host -> 127.0.0.1] => (item=[u'syd01', u'Sydney'])
ok: [yyz01-host -> 127.0.0.1] => (item=[u'yyz01', u'Toronto'])

我有一个基本的角色,它将使用group_vars中多个以组名命名的文件中的hostvar,以查看特定办公室的站点代码和位置; -vars:...

ansible ansible-inventory ansible-role ansible-tasks
1个回答
0
投票

我认为,如果不使用委派即可解决此问题,这会更容易,这会在(a)查找事实和(b)定义事实的地方带来复杂性。相反,您可以像这样循环遍历组中的主机:

© www.soinside.com 2019 - 2024. All rights reserved.