ansible:从同一剧本中的其他剧本访问寄存器变量 [英] ansible: accessing register variables from other plays within same playbook

查看:21
本文介绍了ansible:从同一剧本中的其他剧本访问寄存器变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从第二个剧本的剧本中的第一个播放"中访问名为计数"的变量.我在这里找到了一些关于同一问题的其他帖子,我认为我遵循了正确的步骤,但下面的代码仍然失败.

I'm trying to access the variable called "count" from the first "play" in my playbook in the second playbook. I found some other posts here about the same issue and I thought I was following the right steps, but the code below is still failing.

代码

- hosts: group1
  tasks:
  - name: count registrations on primary node
    shell: psql -U widgets widgets -c 'SELECT COUNT(*) FROM location' -t
    register: count
  - debug: var=count.stdout

- hosts: group2
  tasks:
#the line below works...
#  - debug: msg={{ hostvars['myserver1.mydomain.com']['count']['stdout'] }}
# but this one fails
  - debug: msg={{ hostvars['group1']['count']['stdout'] }}

这会产生以下输出:

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [myserver1.mydomain.com]

TASK [count registrations on node] **************************************
changed: [myserver1.mydomain.com]

TASK [debug] *******************************************************************
ok: [myserver1.mydomain.com] => {
    "count.stdout": "     2"
}

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [myserver2.mydomain.com]

TASK [debug] *******************************************************************
fatal: [myserver1.mydomain.com]: FAILED! => {"failed": true, "msg": "'ansible.vars.hostvars.HostVars object' has no attribute 'can_sip1'"}

NO MORE HOSTS LEFT *************************************************************
 [ERROR]: Could not create retry file 'playbooks/test.retry'. The error was: [Errno 13] Permission denied: 'playbooks/test.retry'


PLAY RECAP *********************************************************************

myserver1.mydomain.com : ok=3 changed=1 unreachable=0 failed=0
myserver2.mydomain.com : ok=1 changed=0 unreachable=0 failed=1

myserver1.mydomain.com : ok=3 changed=1 unreachable=0 failed=0
myserver2.mydomain.com : ok=1 changed=0 unreachable=0 failed=1

我提到的另一篇文章可以在这里找到:如何设置注册变量在 ansible 中的播放之间保持不变?

The other post that I referring to is found here: How do I set register a variable to persist between plays in ansible?

这可能很简单,但我看不出错误在哪里.谢谢.

It's probably something simple, but I can't see where the bug lies. Thanks.

编辑 1

我也尝试过像这样使用 set_fact:

I've also tried to use set_fact like this:

- hosts: group1
  tasks:                                  
  - name: count registrations on primary node        
    shell: psql -U widget widget -c 'SELECT COUNT(*) FROM location' -t
    register: result                      
  - debug: var=result.stdout              
  - set_fact: the_count=result.stdout
  - debug: var={{the_count}}              

- hosts: group2                        
  tasks:                                  
  - name: retrieve variable from previous play
    shell: echo hello                     
  - debug: var={{hostvars}}

我得到的结果是:

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [myserver1.mydomain.com]

TASK [count reg on primary] ****************************************************
changed: [myserver1.mydomain.com]

TASK [debug] *******************************************************************
ok: [myserver1.mydomain.com] => {
    "result.stdout": "     2"
}

TASK [set_fact] ****************************************************************
ok: [myserver1.mydomain.com]

TASK [debug] *******************************************************************
ok: [myserver1.mydomain.com] => {
    "result.stdout": "     2"
}

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [myserver2.mydomain.com]

TASK [retrieve variable from previous play] ************************************
changed: [myserver2.mydomain.com]

TASK [debug] *******************************************************************
ok: [myserver2.mydomain.com] => {
    "<ansible.vars.hostvars.HostVars object at 0x7f3b6602b290>": "VARIABLE IS NOT DEFINED!"
}

PLAY RECAP *********************************************************************
myserver1.mydomain.com        : ok=5    changed=1    unreachable=0    failed=0   
myserver2.mydomain.com       : ok=3    changed=1    unreachable=0    failed=0   

所以看起来主机变量中没有对象...

So It looks like there are no objects in the hostvars...

编辑 3

这就是今天早上的剧本.

This is what the playbook looks like this morning.

- hosts: group1
  tasks:
  - name: count reg on primary
    shell: psql -U widgets widgets -c 'SELECT COUNT(*) FROM location' -t
    register: result
  - debug: var=result.stdout
  - set_fact: the_count={{result.stdout}} 
  - debug: var={{the_count}}

- hosts: group2
  tasks:
  - name: retrieve variable from previous play
    shell: echo hello
  - debug: var={{hostvars}}    

第一部剧中的debug: var={{the_count}}"行打印出正确的计数值,但它也说未定义变量......像这样:

The "debug: var={{the_count}}" line from the first play prints out the correct value for the count but it also says the VARIABLE IS NOT DEFINED... like so:

任务 [set_fact] ********************************************************************任务路径:/etc/ansible/playbooks/test.yml:8ok: [myserver1.mydomain.com] => {"ansible_facts": {"the_count": " 2"}, "changed": false, "invocation": {"module_args": {"the_count": " 2"}, "module_name": "set_fact"}}

TASK [set_fact] **************************************************************** task path: /etc/ansible/playbooks/test.yml:8 ok: [myserver1.mydomain.com] => {"ansible_facts": {"the_count": " 2"}, "changed": false, "invocation": {"module_args": {"the_count": " 2"}, "module_name": "set_fact"}}

任务 [调试] ***********************************************************************任务路径:/etc/ansible/playbooks/test.yml:10好的:[myserver1.mydomain.com] => {" 2": "变量未定义!"}

TASK [debug] ******************************************************************* task path: /etc/ansible/playbooks/test.yml:10 ok: [myserver1.mydomain.com] => { " 2": "VARIABLE IS NOT DEFINED!" }

然后一旦我打第二场,我仍然收到消息

And then once I hit the second play, I still get the message

TASK [debug] *******************************************************************
task path: /etc/ansible/playbooks/test.yml:16
ok: [myserver2.mydomain.com] => {
    "<ansible.vars.hostvars.HostVars object at 0x7fb077fdc310>": "VARIABLE IS NOT DEFINED!"
}

在您的示例中,您建议我使用调试:var={{hostlers}}".如果你能帮我澄清一下.看起来这是一个错字.

In your example, you are suggestion that I use "debug: var={{hostlers}}". If you can clarify that for me please. It looks like it's a typo.

编辑 4:

如果您仔细查看 Edit 3,您会发现我已经按照您在回答中的建议实现了debug:var={{hostvars}}".但它给了我同样的错误,即未定义变量.我不只是试图将变量从一个游戏传递到另一个……而是从一组主机到另一组主机.请注意 play 1 如何使用 group1 而 play 2 仅适用于 group2.

If you take a look at Edit 3 carefully, you will see that I have implemented "debug:var={{hostvars}}" as you suggest in your answer. But it gives me the same error that the variable is not defined. I'm not just trying to pass variables from one play to another.. but from one set of hosts to another. Notice how play 1 uses group1 and play two applies only to group2.

推荐答案

  1. 寄存器变量,如事实,是每个主机的.这些值可能因机器而异.因此,您只能使用清单中定义的主机/IP 作为键,而不能使用组名.我想您已经知道这一点,因为您在代码片段 1 中标记了这一点.

  1. Register variables, like facts, are per host. The values can differ depending on the machine. So you can only use host/ip defined in the inventory as key, not the group name. I think you have already knowed this, as you marked this in code snippet 1.

在代码片段 2 中,set_fact 行 (- set_fact: the_count=result.stdout) 实际上设置了键 the_count 到文本值 result.stdout,因为 result.stdout 被视为纯文本,而不是变量.如果你想把它当作一个变量,你最好使用{{ result.stdout }}.您可以通过使用 -v 选项运行剧本来验证这一点.

In the code snippet 2, the set_fact line (- set_fact: the_count=result.stdout) actually set the key the_count to the text value result.stdout, since result.stdout is treated as plain text, not a variable. If you want to treat it as a variable, you'd better use {{ result.stdout }}. You can verify this via running the playbook with -v option.

任务:

  • set_fact:the_content1=content.stdout
  • set_fact: the_content2={{ content.stdout }}

输出:

TASK [set_fact] ****************************************************************
ok: [192.168.1.58] => {"ansible_facts": {"the_content1": "content.stdout"}, "changed": false}

TASK [set_fact] ****************************************************************
ok: [192.168.1.58] => {"ansible_facts": {"the_content2": "hello world"}, "changed": false}

  • debug 模块有两个可能的参数:varmsg.var 参数需要一个变量名.

  • The debug module has two possible parameter: var and msg. The var parameter expect a variable name.

    • 调试:var={{hostvars}}

    在这一行中,首先,Ansible 提取 hostvars 的值,因为它被两个括号括起来.其次,它试图找到一个名称为 hostvars 值的变量,因为 var 参数直接需要一个变量名.这就是为什么您会看到以下奇怪的输出.这意味着 Ansible 找不到名称为 的变量.

    In this line, first of all, Ansible extracts the value of hostvars, since it is enclosed with two brackets. Secondly, it tries to find a variable whose name is the value of hostvars, since var parameter expects a variable name directly. That is why you see the following strange output. This means Ansible couldn't find a variable whose name is <ansible.vars.hostvars.HostVars object at 0x7f3b6602b290>.

    "<ansible.vars.hostvars.HostVars object at 0x7f3b6602b290>": "VARIABLE IS NOT DEFINED!"
    

    您可以使用以下内容:

    • 调试:var=hostvars
    • 调试:msg={{hostvars}}

    参考文献:

    这篇关于ansible:从同一剧本中的其他剧本访问寄存器变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆