我想通过Ansible比较2个红帽服务器的服务列表,并仅打印出正在运行的不同服务。
例如:
- 服务器1运行服务:
a,b,c,d
- 服务器2运行服务:
c,d,e,f
在本例中,我只想打印a,b
服务。
下面是我的剧本。
- hosts: server1:server2
gather_facts: true
tasks:
- name: Run command to get running services on server1
shell: systemctl list-units --type=service --state=running --no-pager | grep '\.service' | awk '{print $1}'
register: result_server1
when: "'server1' in inventory_hostname"
- name: Run command to get running services on server2
shell: systemctl list-units --type=service --state=running --no-pager | grep '\.service' | awk '{print $1}'
register: result_server2
when: "'server2' in inventory_hostname"
- name: Set facts for server1 services
set_fact:
server1_services: "{{ result_server1.stdout_lines }}"
when: "'server1' in inventory_hostname"
- name: Set facts for server2 services
set_fact:
server2_services: "{{ result_server2.stdout_lines }}"
when: "'server2' in inventory_hostname"
- name: Find the difference between services
set_fact:
service_difference: "{{ server1_services | difference(server2_services) }}"
when: "'server1' in inventory_hostname"
- name: Print the difference for server2
debug:
msg: "Service difference for server1: {{ service_difference }}"
when: "'server1' in inventory_hostname"
字符串
问题是它打印出server1
的所有服务。
TASK [Print differences] **********************************************************************************************************************************************************************
task path: /home/jenkins/workspace/custom-playbooks/2getservicelist.yaml:49
ok: [server1] => {
"msg": "Service difference for server1: [u'auditd.service', u'crond.service', u'dbus.service', u'[email protected]', u'httpd.service', u'irqbalance.service', u'lvm2-lvmetad.service', u'NetworkManager.service', u'polkit.service', u'postfix.service', u'rhnsd.service', u'rhsmcertd.service', u'rsyslog.service', u'sshd.service', u'systemd-journald.service', u'systemd-logind.service', u'systemd-udevd.service', u'tuned.service', u'vgauthd.service', u'vmtoolsd.service']"
skipping: [server2] => {}
META: ran handlers
META: ran handlers
型difference
filter不能提供我假设的。我怎么解决这个问题?
1条答案
按热度按时间5kgi1eie1#
最小示例剧本
字符串
可以导致输出,例如
型
进一步阅读
service_facts
的使用案例及其使用方法service_facts
?的service_facts
filtering by partial name的或一般