linux Ansible:如何比较2个主机的服务与Ansible?

1sbrub3j  于 12个月前  发布在  Linux
关注(0)|答案(1)|浏览(122)

我想通过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不能提供我假设的。我怎么解决这个问题?

5kgi1eie

5kgi1eie1#

最小示例剧本

---
- hosts: test
  become: true
  gather_facts: false

  tasks:

  - name: Gather Service Facts
    service_facts:

  - name: Show all
    debug:
      var: services

  - name: Show difference
    ansible.utils.fact_diff:
      before: "{{ hostvars['test1.example.com'].services | to_nice_yaml }}"
      after:  "{{ hostvars['test2.example.com'].services | to_nice_yaml }}"

字符串
可以导致输出,例如

changed: [test1.example.com]
--- before
+++ after
@@ -27,11 +27,6 @@
...
-besclient.service:
-    name: besclient.service
-    source: systemd
-    state: running
...
+io.podman.service:
+    name: io.podman.service
+    source: systemd
+    state: inactive
+    status: disabled
...
+node_exporter.service:
+    name: node_exporter.service
+    source: systemd
+    state: running
+    status: enabled
...

进一步阅读

service_facts的使用案例及其使用方法

或一般

相关问题