Ansible Hostvars to List 或 Dictionary 以在以后的任务中匹配(键:值)
Ansible Hostvars to List or Dictionary to match off in later task (key:value)
我有一个基本角色,它将使用 group_vars 中多个文件中的主机变量,这些文件以其组名命名,以查看特定办公室的站点代码和位置;
- 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 的输出,看起来正是我想要的;
问题是将其传递到一个可重复使用的 lust 中,该 lust 将匹配 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'])
我认为如果您在没有授权的情况下解决这个问题会更容易,这会在 (a) 查找事实和 (b) 定义事实的地方引入复杂性。相反,您可以像这样遍历组中的主机:
---
- hosts: localhost
gather_facts: false
tasks:
- vars:
code_location: {}
set_fact:
code_location: >-
{{
code_location |
combine({hostvars[item].sitecode: hostvars[item].location})
}}
loop: "{{ groups.all }}"
以上循环遍历所有主机 (groups.all
),但您当然可以将其限制为特定主机组(甚至是明确的主机列表)。
我有一个基本角色,它将使用 group_vars 中多个文件中的主机变量,这些文件以其组名命名,以查看特定办公室的站点代码和位置;
- 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 的输出,看起来正是我想要的;
问题是将其传递到一个可重复使用的 lust 中,该 lust 将匹配 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'])
我认为如果您在没有授权的情况下解决这个问题会更容易,这会在 (a) 查找事实和 (b) 定义事实的地方引入复杂性。相反,您可以像这样遍历组中的主机:
---
- hosts: localhost
gather_facts: false
tasks:
- vars:
code_location: {}
set_fact:
code_location: >-
{{
code_location |
combine({hostvars[item].sitecode: hostvars[item].location})
}}
loop: "{{ groups.all }}"
以上循环遍历所有主机 (groups.all
),但您当然可以将其限制为特定主机组(甚至是明确的主机列表)。