Ansible playbook:在同一组的不同主机上多次运行同一任务后,注册变量的值

Ansible playbook:在同一组的不同主机上多次运行同一任务后,注册变量的值,ansible,devops,Ansible,Devops,我的/etc/ansible/hosts文件有3台机器: [local] 172.17.0.1 172.17.0.2 172.17.0.3 我的剧本是这样的: name: test shell: systemctl status httpd register: httpdstatus 假设httpd在172.17.0.1和172.17.0.2上运行,而不是在172.17.0.3上运行,那么httpdstatus.rc的最终值是多少 我可以使用httpdstatus的单个结果代码值吗 如果在1

我的
/etc/ansible/hosts
文件有3台机器:

[local]
172.17.0.1
172.17.0.2
172.17.0.3
我的剧本是这样的:

name: test
shell: systemctl status httpd
register: httpdstatus
假设httpd在172.17.0.1和172.17.0.2上运行,而不是在172.17.0.3上运行,那么httpdstatus.rc的最终值是多少

我可以使用httpdstatus的单个结果代码值吗

如果在172.17.0.1、172.17.0.2或172.17.0.3中的任何一个上安装了httpd,我基本上希望在另一个组中的第四台服务器上安装一些东西

如下逻辑:

If {
    (httpdstatus.rc for server 1) or
    (httpdstatus.rc for server 2) or
    (httpdstatus.rc for server 3)==1
}
然后将组件安装到第四台机器上。 在Ansible中如何执行此操作

假设httpd在172.17.0.1和172.17.0.2上运行,而不是在 172.17.0.3,httpdstatus.rc的最终值是多少

当您遍历
httpd
服务器列表时,当前项的结果将被放入
寄存器
变量中

参考官方文档了解循环中寄存器变量的行为

我可以使用httpdstatus的单个结果代码值吗

是的,您可以使用单个结果代码

我基本上想在第四台服务器上安装一些东西,它位于 不同的组,如果在172.17.0.1中的任何一个上安装了httpd, 172.17.0.2或172.17.0.3

是的,您可以使用具有ansible 2.0及更高版本的include操作处理程序来实现这一点。你可以在你的剧本中写下处理程序来做这件事

handlers:
    - name: install httpd
      yum: name=httpd
      service: name=httpd state=started
在任务中,如果在任务中看到失败,可以通知上述处理程序

- name: test
  service: systemctl status httpd
  register: httpdstatus
  when: httpdstatus.rc==1
  notify: "install httpd"

希望这有帮助

也许你可以在Devops上得到更多的关注:Devops.stackexchange.com