背景信息:
我需要在一组主机(web1)上动态地设置一个变量,然后在不同的主机集中检查相同的变量。一旦他们匹配,我可以执行进一步的行动。
码
我的主机文件如下所示:
[web1]
web1.ttv.mydomain.com
[web1:vars]
primary_count=0
[web2]
web2.ttv.mydomain.com
[web2:vars]
secondary_count=0
[web]
web1
web2这就是剧本:
- hosts: web1
tasks:
- name: query primary servers
shell: psql -U widget widget -c 'SELECT COUNT(*) FROM test' -t
register: result
- set_fact: primary_count={{result.stdout}}
- hosts: web
tasks:
- name: retrieve variable from previous play
shell: echo hello
- debug: var=primary_count该剧本产生了以下结果:
TASK [setup] *******************************************************************
ok: [web1.ttv.mydomain.com]
TASK [query primary servers] ****************************************************
changed: [web1.ttv.mydomain.com]
TASK [debug] *******************************************************************
ok: [web1.ttv.mydomain.com] => {
"primary_count": 0
}
TASK [set_fact] ****************************************************************
ok: [web1.ttv.mydomain.com]
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [web1.ttv.mydomain.com]
ok: [web2.ttv.mydomain.com]
TASK [retrieve variable from previous play] ************************************
changed: [web1.ttv.mydomain.com]
changed: [web2.ttv.mydomain.com]
TASK [debug] *******************************************************************
ok: [web2.ttv.mydomain.com] => {
"primary_count": "VARIABLE IS NOT DEFINED!"
}
ok: [web1.ttv.mydomain.com] => {
"primary_count": " 2"
}问题
在第二部戏中,我需要一种方法来做以下几件事:
问题:
如何用匹配的primary_count主机名计算web1主机上的“web2”变量?将来,我的主机文件将如下所示:
[web1]
web1.ttv.mydomain.com
web1.ttx.mydomain.com
[web2]
web2.ttv.mydomain.com
web2.ttx.mydomain.com
[web]
web1
web2因此,我需要编写这样一种代码:(伪代码)。
while looping through ***ALL*** web servers
if primary_count on web1.ttv.mydomain.com matches secondary_count on web2.ttx.mydomain.com then
restart service x on web2.ttx.mydomain.com
else
wait a few seconds and repeat
end
end loop我认为解决方案取决于我的主机/库存文件。不知何故,我需要这个剧本来运行所有的web1服务器和所有的web2服务器..。但我也需要一种方法来将web1.ttv与web2.ttv和web1.ttx与web2.ttx联系起来,以此类推。
我只是在学习ansible,如果这个方法是完全错误的,请告诉我!
谢谢。
编辑1
在做一些关于group_vars的研究时,group_vars似乎并没有真正帮助我,因为我仍然有同样的问题。当循环遍历所有web服务器(play 2)时,我在play 1中在web1服务器上设置的变量在web2服务器中不可见。
编辑2:
- hosts: web1
tasks:
- name: query primary servers
shell: psql -U widget widget -c 'SELECT COUNT(*) FROM widget' -t
register: result
- local_action: shell echo {{ result.stdout }} > varacrossplay.txt在local_action行上出现此错误的失败:
致命: web1.ttv.mydomain.com -> localhost:=> {"changed":true,"cmd":"echo 2> varacrossplay.txt","delta":"0:00:00.001641","end":":“"echo 2”> varacrossplay.txt","_uses_shell":true,"chdir":null,“创建”:null,“可执行文件”null“,”删除“:null,”varacrossplay.txt“:true},"mod 1:无法创建varacrossplay.txt:权限拒绝“、"stdout":"”、"stdout_lines":[]、“警告”:[]}
发布于 2016-03-09 19:37:02
尝试使用此示例剧本:
[jenkins@batman ansible]$ cat testplaybook.yml
- hosts: web1
tasks:
- name: query primary servers
shell: echo "TEST"
register: result
- local_action: shell echo {{ result.stdout }} > varacrossplay.txt
- hosts: web
tasks:
- local_action: shell cat varacrossplay.txt
register: result
- set_fact: other_fact="{{ result.stdout }}"
- debug: var=other_fact我的服务器都可以正常工作,xD
[jenkins@batman ansible]$ ansible-playbook -i inventory testplaybook.yml
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [10.0.0.100]
TASK [query primary servers] ***************************************************
changed: [10.0.0.100]
TASK [command] *****************************************************************
changed: [10.0.0.100 -> localhost]
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [10.0.0.2]
ok: [10.0.0.1]
TASK [command] *****************************************************************
changed: [10.0.0.1 -> localhost]
changed: [10.0.0.2 -> localhost]
TASK [set_fact] ****************************************************************
ok: [10.0.0.1]
ok: [10.0.0.2]
TASK [debug] *******************************************************************
ok: [10.0.0.2] => {
"other_fact": "TEST"
}
ok: [10.0.0.1] => {
"other_fact": "TEST"
}
PLAY RECAP *********************************************************************
10.0.0.100 : ok=3 changed=2 unreachable=0 failed=0
10.0.0.1 : ok=4 changed=1 unreachable=0 failed=0
10.0.0.2 : ok=4 changed=1 unreachable=0 failed=0https://stackoverflow.com/questions/35897835
复制相似问题