Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ansible 如何通过使用when语句检查清单中的主机组来启动服务_Ansible_Ansible Inventory - Fatal编程技术网

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

Ansible 如何通过使用when语句检查清单中的主机组来启动服务,ansible,ansible-inventory,Ansible,Ansible Inventory,有人能帮我做到以下几点吗 我的ansible库存文件中有不同的组。我需要在一个剧本中为一个组执行几个命令,为另一个组执行几个命令 --- - name: chekcing Service status hosts: hari tasks: - name : service status shell: systemctl status "{{item}}" with_items: - 'jboss_prod' 我已经为单组执行编写了如下剧本,这很好,但我需要

有人能帮我做到以下几点吗

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

---
- name: chekcing Service status
  hosts: hari
  tasks:
  - name : service status
    shell: systemctl status "{{item}}"
    with_items:
     - 'jboss_prod'
我已经为单组执行编写了如下剧本,这很好,但我需要将所有内容合并到单个剧本中

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

你能推荐一下吗

对于不同组中的不同任务,您可以编写如下内容

---
- 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文档)


所以你需要在第二组中做一些其他的任务?嗨,欢迎来到so。请查看编辑页面上的格式化指南。你能正确格式化你的代码吗?提前谢谢。我可以问一下通过直接命令获得服务状态的目标是什么吗?尽管有时可能需要,但使用ansible,您通常会描述幂等任务中的状态(例如,
状态:对于
systemd
模块,启动
),然后运行它。如果服务已经启动,任务将报告
ok
,如果需要,任务将报告(
changed
),如果未安装,任务甚至会报告
error