- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何在不提供像第一个代码那样提供列表的情况下,以 state=='running'
列出系统的所有服务?
- name: Populate service facts
service_facts:
no_log: true
register: inspect_services2
when: "ansible_facts.services[] is defined and ansible_facts.services[].state == 'running' "
仅当我使用列表时,我才能设法列出它们:
- name: Running Services
debug:
msg: "{{ ansible_facts.services[item].state == 'running' }}"
when: "ansible_facts.services[item] is defined and ansible_facts.services[item].state == 'running' "
loop: "{{ inspect_services2 }}"
ignore_errors: yes
最佳答案
简而言之:
---
- name: work with service facts
hosts: localhost
tasks:
- name: gather service facts
service_facts:
- name: show running services
debug:
msg: "{{ ansible_facts.services | dict2items
| selectattr('value.state', '==', 'running') | items2dict }}"
这为您提供了一个包含所有正在运行的服务的所有信息的字典。如果例如您只想显示这些服务的名称,您可以将调试任务中的消息更改为:
msg: "{{ ansible_facts.services | dict2items
| selectattr('value.state', '==', 'running') | map(attribute='key') }}"
您当然可以完全自由地使用该结果并将其作为别名放入变量中以重用它。下面是一个无用但实用的示例,在目标服务器上创建一个带有服务名称的文件,只是为了说明:
---
- name: Work with service facts II
hosts: localhost
vars:
# Note1: this will be undefined until service facts are gathered
# Note2: this time this var will be a list of all dicts
# dropping the initial key wich is identical to `name`
running_services: "{{ ansible_facts.services | dict2items
| selectattr('value.state', '==', 'running') | map(attribute='value') }}"
tasks:
- name: gather service facts
service_facts:
- name: useless task creating a file with service name in tmp
copy:
content: "ho yeah. {{ item.name }} is running"
dest: "/tmp/{{ item.name }}.txt"
loop: "{{ running_services }}"
关于ansible - 如何使用 ansible_facts 仅列出正在运行的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70803827/
我正在尝试创建自己的ansible模块(它将更新cmdb)并且我正在寻找如何在模块代码中使用ansible_facts? 我的模块脚本示例是: #!/usr/bin/python from ansib
我想检查我的包是否已安装,但我希望所有包类型都有一个名称。 让我解释一下:我正在寻找 OB2 包,但它们有很多,所以我正在寻找 OB2* 但它跳过了我的搜索。 我用不带通配符的名称 (*) 进行了测试
如何在不提供像第一个代码那样提供列表的情况下,以 state=='running' 列出系统的所有服务? - name: Populate service facts service_facts:
如何在不提供像第一个代码那样提供列表的情况下,以 state=='running' 列出系统的所有服务? - name: Populate service facts service_facts:
我正在从 ansible_facts 收集一些输出并借助复制模块(内容和目标)将它们写入远程服务器上的文件 如果某些项目在服务器上不可用(例如磁盘 sdb 在服务器上不存在),我需要写为 NONE。
我在 Ansible 中使用 package_facts 模块时遇到问题。基本上,我只想安装 zabbix-agent 的版本,因为我需要根据安装的版本做一些事情。 现在我在剧本任务中得到了这个: -
我是一名优秀的程序员,十分优秀!