我正在尝试从第二个剧本中我的剧本中的第一个“播放”中访问名为“count”的变量 . 我在这里发现了一些关于同一问题的其他帖子,我认为我正在遵循正确的步骤,但下面的代码仍然失败 .
The Code
- 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
我指的另一篇文章在这里找到:How do I set register a variable to persist between plays in ansible?
它可能很简单,但我无法看到bug的位置 . 谢谢 .
EDIT 1
我也试过像这样使用set_fact:
- 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
所以看起来hostvars中没有对象......
EDIT 3
这就是今天早上剧本的样子 .
- 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 = {}”行打印出正确的计数值,但它也表示VARIABLE未定义...如下所示:
任务[set_fact] ********************************************** ******************任务路径:/etc/ansible/playbooks/test.yml:8 ok:[myserver1.mydomain.com] => {“ansible_facts”:{ “the_count”:“2”},“已更改”:false,“调用”:{“module_args”:{“the_count”:“2”},“module_name”:“set_fact”}}
任务[调试] ********************************************** *********************任务路径:/etc/ansible/playbooks/test.yml:10 ok:[myserver1.mydomain.com] => {“2 “:”变量没有定义!“ }
然后,当我第二次播放时,我仍然收到消息
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!"
}
在您的示例中,您建议我使用“debug:var = {}” . 如果你能为我澄清一下 . 看起来这是一个错字 .
EDIT 4:
如果你仔细看看编辑3,你会看到我已经在你的答案中建议我实现了“debug:var = {}” . 但是它给了我同样的错误,即没有定义变量 . 我不只是试图将变量从一个播放传递到另一个播放......而是从一组主机传递到另一组主机 . 注意play 1如何使用group1和play 2仅适用于group2 .
1 回答
寄存器变量,如事实,是每个主机 . 值可能因机器而异 . 因此,您只能使用清单中定义的主机/ IP作为密钥,而不是组名 . 我想你已经知道了这一点,因为你在代码片段1中标记了这一点 .
在代码段2中,
set_fact
行(- set_fact: the_count=result.stdout
)实际上将键the_count
设置为文本值result.stdout
,因为result.stdout
被视为纯文本而不是变量 . 如果要将其视为变量,最好使用{{ result.stdout }}
. 您可以通过使用-v
选项运行playbook来验证这一点 .任务:
set_fact:the_content1 = content.stdout
set_fact:the_content2 = {}
输出:
debug
模块有两个可能的参数:var
和msg
.var
参数需要变量名称 .debug:var = {}
在这一行中,首先,Ansible提取
hostvars
的值,因为它用两个括号括起来 . 其次,它试图找到一个名称为hostvars
的变量,因为var
参数直接需要一个变量名 . 这就是为什么你看到以下奇怪的输出 . 这意味着Ansible找不到名称为<ansible.vars.hostvars.HostVars object at 0x7f3b6602b290>
的变量 .您可以使用以下内容:
debug:var = hostvars
debug:msg = {}
参考文献:
Register variables don't survive across plays with different hosts
set_fact - Set host facts from a task
debug - Print statements during execution