0

我正在尝试从 scan_services 模块(或 >= 2.5 中的 service_facts )的输出中过滤掉正在运行的服务。这个模块的输出是这样的:

    "ansible_facts": {
    "services": {
        "NetworkManager-dispatcher.service": {
            "name": "NetworkManager-dispatcher.service",
            "source": "systemd",
            "state": "running"
        },
        "NetworkManager-wait-online.service": {
            "name": "NetworkManager-wait-online.service",
            "source": "systemd",
            "state": "stopped"
        },
        "NetworkManager.service": {
            "name": "NetworkManager.service",
            "source": "systemd",
            "state": "running"
        },

现在,我要做的是仅列出处于状态的服务名称:正在运行...此代码似乎根本不起作用:/

---
- hosts: all
  tasks:
  - name : Scan current services status
    scan_services:
    register: running_services
  - name: print
    debug: var=item
    with_items: "{{ running_services.|json_query('ansible_facts.services.[?state=='running'].name')}}"

...但我想我在附近的某个地方。我想这个 json_query 中缺少一些东西:(

4

2 回答 2

3

jmespath 表达式返回一个包含正在运行的服务名称的列表:

ansible_facts.services.* | [?state == `running`].name
于 2018-04-18T12:02:28.417 回答
1

你可以试试这个:

- name: Get Service Facts
  service_facts:

- name: Get running services
  set_fact:
    services_runnig: "{{ ansible_facts | json_query('services.* | [?state == `running`].name') }}"

- name: Debug
  debug:
    msg: "{{ services_running }}"
于 2020-12-29T18:11:05.987 回答