如何通过使用when语句检查清单中的主机组来启动服务

问题描述 投票:0回答:1

有人可以帮助我实现以下目标吗?

我在ansible的库存文件中有不同的组。我需要在一个剧本中为一组执行几条命令,而为另一组执行几条命令。

我已经为单人小组执行编写了以下类似的剧本,这很好,但是我需要将所有内容合并为一个剧本。

---
- name: chekcing Service status
  hosts: hari
  tasks:
  - name : service status
    shell: systemctl status "{{item}}"
    with_items:
     - 'jboss_prod'

您能否建议。

ansible ansible-inventory
1个回答
0
投票

对于不同组上的不同任务,您可以编写如下。

---
- hosts: host1
  tasks:
  - name : service status
    shell: systemctl status "{{item}}"
    with_items:
     - 'jboss_prod'

- hosts: host2
  tasks:
  - name : service status
    shell: systemctl status "{{item}}"
    with_items:
     - 'apache_prod'

如评论中所述,您也可以使用systemd模块来检查服务状态。例如:(来自ansible文档)

- name: Make sure a service is running
  systemd:
    state: started
    name: httpd
© www.soinside.com 2019 - 2024. All rights reserved.