在父 Playbook 中使用 include_vars 在子 Playbook 中。

huangapple go评论67阅读模式
英文:

Use include_vars in sub playbook from parent playbook

问题

I have a playbook playbook.yml, which import sub playbooks, and I want to include play2.yml conditionally based on my_app_vars[key1]

- import_playbook: play1.yml
- import_playbook: play2.yml
  when: {{ my_app_vars[key1] }}
  vars:
    key1: "{{mock_key}}"

in play1.yml, I have

- name: "Get Some Info"
  hosts: mock_servers
  pre_tasks:
    - name:
      include_vars:
        file: mock-vars.yml
        name: my_app_vars
      register: "{{my_app_vars}}"  #adding register did not help

After execution, I get the error my_app_vars is undefined, after adding register, still get the same error.

I could use my_app_vars, mock_key and key1 under a role correctly in play2.yml.

But now I want to use my_app_vars, mock_key and key1 in playbook.yml, is there any way to achieve it?
I use ansible-playbook-2.8.1, thanks in advance


Update:

follow answer from @β.εηοιτ.βε, I tried below [no register in play1.yml]

when: not ( {{ hostvars[groups[mock_servers][0]].my_app_vars[key1] }} )
''mock_servers'' is undefined'

when: not ( {{ hostvars[groups['mock_servers'][0]].my_app_vars[key1] }} )
''ansible.vars.hostvars.HostVarsVars object'' has no attribute ''my_app_vars'''

when: not ( {{ hostvars[groups.mock_servers.0].my_app_vars[key1] }} )
''ansible.vars.hostvars.HostVarsVars object'' has no attribute ''my_app_vars'''

when: not ( {{ hostvars[groups.mock_servers.0] }}.my_app_vars[key1] )
''dict object'' has no attribute ''my_app_vars'


After add gather_facts: false, work as expected, fantastic

英文:

I have a playbook playbook.yml, which import sub playbooks, and I want to include play2.yml conditionally based on my_app_vars[key1]

- import_playbook: play1.yml
- import_playbook: play2.yml
  when: {{ my_app_vars[key1] }}
  vars:
    key1: "{{mock_key}}"

in play1.yml, I have

- name: "Get Some Info"
  hosts: mock_servers
  pre_tasks:
    - name:
      include_vars:
        file: mock-vars.yml
        name: my_app_vars
      register: "{{my_app_vars}}"  #adding register did not help

After execution, I get the error my_app_vars is undefined, after adding register, still get the same error.

I could use my_app_vars, mock_key and key1 under a role correctly in play2.yml.

But now I want to use my_app_vars, mock_key and key1 in playbook.yml, is there any way to achieve it?
I use ansible-playbook-2.8.1, thanks in advance


Update:

follow answer from @β.εηοιτ.βε, I tried below [no register in play1.yml]

when: not ( {{ hostvars[groups[mock_servers][0]].my_app_vars[key1] }} )
''mock_servers'' is undefined'

when: not ( {{ hostvars[groups['mock_servers'][0]].my_app_vars[key1] }} )
''ansible.vars.hostvars.HostVarsVars object'' has no attribute ''my_app_vars'''

when: not ( {{ hostvars[groups.mock_servers.0].my_app_vars[key1] }} )
''ansible.vars.hostvars.HostVarsVars object'' has no attribute ''my_app_vars'''

when: not ( {{ hostvars[groups.mock_servers.0] }}.my_app_vars[key1] )
''dict object'' has no attribute ''my_app_vars'

After add gather_facts: false, work as expected, fantastic

答案1

得分: 2

这是模块概述的最后一条隐含陈述:

  • 要将包含的变量分配给与 inventory_hostname 不同的主机,请使用 delegate_to 并设置 delegate_facts=yes

这意味着由 include_vars 包含的变量实际上存储为主机变量。

这意味着,为了在主要playbook中访问它们,该playbook包括另外两个playbook,不选择任何主机或组,您将需要通过hotsvars特殊变量来访问它们。

在您的情况下,可以是这样的:

when: hostvars[groups.mock_servers.0].my_app_vars[key1]

这种行为的示例如下。

play.yml

- import_playbook: play1.yml
  
- import_playbook: play2.yml
  when: hostvars[groups.group1.0].vars_of_play1.foo == 'bar'

play1.yml:

- hosts: group1
  gather_facts: false

  pre_tasks:
    - include_vars:
        file: vars.yml
        name: vars_of_play1

vars.yml:

foo: bar

play2.yml:

- hosts: group2
  gather_facts: false

  pre_tasks:
    - debug:

这将如预期地运行第二个playbook:

$ ansible-playbook play.yml 

PLAY [group1] ************************************************************

TASK [include_vars] ******************************************************
ok: [ansible-node-1]

PLAY [group2] ************************************************************

TASK [debug] *************************************************************
ok: [ansible-node-2] => 
  msg: Hello world!
英文:

This is implicitly stated in the last bullet point of the module synopsis:

> * To assign included variables to a different host than inventory_hostname, use delegate_to and set delegate_facts=yes.

<sup>Source: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/include_vars_module.html#synopsis&lt;/sup&gt;

Which means the variables included by include_vars are actual stored as hosts variables.

This means, that, in order to access them in your main playbook, that includes the two other playbooks and which is not selecting any hosts or groups, you will have to access those via the hotsvars special variable.

In your case it could be something like:

when: hostvars[groups.mock_servers.0].my_app_vars[key1]

An example of that behaviour would be as follow.

play.yml:

- import_playbook: play1.yml
  
- import_playbook: play2.yml
  when: hostvars[groups.group1.0].vars_of_play1.foo == &#39;bar&#39;

play1.yml:

- hosts: group1
  gather_facts: false

  pre_tasks:
    - include_vars:
        file: vars.yml
        name: vars_of_play1

vars.yml:

foo: bar

play2.yml:

- hosts: group2
  gather_facts: false

  pre_tasks:
    - debug:

Which would, as expected, run the second playbook:

$ ansible-playbook play.yml 

PLAY [group1] ************************************************************

TASK [include_vars] ******************************************************
ok: [ansible-node-1]

PLAY [group2] ************************************************************

TASK [debug] *************************************************************
ok: [ansible-node-2] =&gt; 
  msg: Hello world!

huangapple
  • 本文由 发表于 2023年6月12日 18:47:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定