2014-11-21 62 views
0

我的方案包括两个剧本的:是否可以使用可靠的角色来设置_passed_变量值?

  • 剧本P1采用两个角色A和B,
  • 剧本P2仅使用作用A.

现在,角色A和B的需要执行几个额外的步骤,执行相同的操作,但导致不同的变量。例如:

# role A 
- name: get the current_path 
    shell: pwd 
    register: A_path_variable 

# role B, in a separate file 
- name: get the current_path 
    shell: pwd 
    register: B_path_variable 

所需的操作是相同的 - 但结果变量名称是不同的。

Ansible中是否有一些人告诉角色使用特定的变量?例如:可以将“shell:pwd”分隔为一个新角色,然后要求它使用“specific_variable”来注册最终结果。

回答

0

经过调查 - 事实证明,变量解析只是基于字符串。

角色A和B在单个剧本中执行,都调用角色UTIL,期望它写入不同的变量。角色UTIL应处理一些数据并将结果写入变量。

角色答:

... 
vars: 
    output_var_name: my_variable_A 
roles: 
    - UTIL 

角色B:

... 
vars: 
    output_var_name: my_variable_B 
roles: 
    - UTIL 

UTIL,到底

- name: output result 
    set_fact: 
    "{{ output_var_name }}={{ local_variable }}" 

结果:my_variable_B和my_variable_A都相应地设置。我的理解是,“变数”在“角色”之前被执行,因此它可以在适当的时候改变,正确的价值。

0

您可以使用角色参数:

角色/ UTIL:

- shell: pwd 
    register: util_output 

剧本:

roles: 
    - util 
    - { role: role_a, my_variable_a: "{{util_output}}" } 
    - { role: role_b, my_variable_b: "{{util_output}}" } 
相关问题