你组变量的定义在玩

0

的问题

我已经实现了一个动态的库存需求,在其执行它确定了一组变量,我使用的是后面的剧本。

在剧本,我是使用这两次:

  • 为循环过它的内容:
    tasks:
       - include_tasks: include/restart.yaml
         loop: "{{ host_set | default(['all']) }}"
         loop_control:
           loop_var: h
    
  • 并且作为一种手段的设定有条件的序列
    serial: "{{ host_set | default(false) | ternary('100%', '5') }}"
    

行为,我得到那个我不能解释的是 host_set 变量的定义以及填充正在执行的任务,但是,当试图设置的 serial 这似乎是不确定的。

这是预期的行为,都需求组变量的定义时设置一个发挥的属性,但是弹出后,当任务的执行,特别是游戏开始?

ansible
2021-11-23 21:48:17
2

最好的答案

3

小组变量只适用于个人的主机。 戏剧进行评估,在一个非主机上下文,因此唯一的变量,提供非东道-具体的变量,像那些从 --extra-vars.

2021-11-24 01:32:48
1

当因这种行为是完全说通过 @flowerysong的答案,还有一个解决方法你可以用它来达到你的要求。

诀窍就是使用事实上,你可以界定两剧里的剧本和,那么,使用的变量集在主(s)的第一次播放。

因此,我们要做的是:

  1. 运行上玩 localhost 并设置一个事实,来自东道组,我们的目标是到目标在第二玩
  2. 任选:我也定义了 target_hosts 变量,在第一起,以确保,如果第二玩的目标是修改,主组变量始终是一个第二起是要目标。
  3. 使用这个变量在第二播放,从 localhost's hostvars

额外注意:没有核算的味道,但是我喜欢的 内联如果ternary 滤波器,仍然,他们都是严格等同的。


因此,鉴于指南:

- hosts: localhost
  gather_facts: no

  tasks:
    - set_fact:
        target_hosts: nodes
        
    - set_fact:
        host_set: "{{ hostvars[groups[target_hosts].0].host_set }}"

- hosts: "{{ hostvars.localhost.target_hosts }}"
  gather_facts: no
  serial: "{{ '100%' if hostvars.localhost.host_set else '1' }}"
  
  tasks:
    - debug: 
  • 时运行 host_set: truenodes小组:
    PLAY [localhost] *******************************************************************************************************************
    
    TASK [set_fact] ********************************************************************************************************************
    ok: [localhost]
    
    TASK [set_fact] ********************************************************************************************************************
    ok: [localhost]
    
    PLAY [nodes] ***********************************************************************************************************************
    
    TASK [debug] ***********************************************************************************************************************
    ok: [node1] => 
      msg: Hello world!
    ok: [node2] => 
      msg: Hello world!
    ok: [node3] => 
      msg: Hello world!
    
    PLAY RECAP *************************************************************************************************************************
    localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    node1                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    node2                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    node3                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    
  • 时运行 host_set: falsenodes小组:
    PLAY [localhost] *******************************************************************************************************************
    
    TASK [set_fact] ********************************************************************************************************************
    ok: [localhost]
    
    TASK [set_fact] ********************************************************************************************************************
    ok: [localhost]
    
    PLAY [nodes] ***********************************************************************************************************************
    
    TASK [debug] ***********************************************************************************************************************
    ok: [node1] => 
      msg: Hello world!
    
    PLAY [nodes] ***********************************************************************************************************************
    
    TASK [debug] ***********************************************************************************************************************
    ok: [node2] => 
      msg: Hello world!
    
    PLAY [nodes] ***********************************************************************************************************************
    
    TASK [debug] ***********************************************************************************************************************
    ok: [node3] => 
      msg: Hello world!
    
    PLAY RECAP *************************************************************************************************************************
    localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    node1                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    node2                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    node3                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    
2021-11-24 11:14:59

谢谢你的额外的信息!
Radu Stoenescu

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................