【问题标题】:Ansible: How to get service status by Ansible?Ansible:如何通过 Ansible 获取服务状态?
【发布时间】:2016-08-09 10:07:19
【问题描述】:

我想通过 Ansible 获得类似 redis-server 运行状态的服务。

我知道如何使用 Ansible 服务模块来停止或启动系统服务。但是如何获取当前的服务状态呢?

【问题讨论】:

  • 描述你的任务。您通常只想告诉 ansible 您希望事情如何,而不是手动查询任何内容,例如:service: name=httpd state=started.
  • @KonstantinSuvorov 这是真的,但service 模块缺少对关键用例的支持:启动/停止/启用/禁用服务当且仅当它存在时我>。对于这个用例和可能的其他用例,有必要查询服务的状态。

标签: ansible


【解决方案1】:

您也可以使用service_facts 模块。

示例用法:

- name: collect facts about system services
  service_facts:
  register: services_state

- name: Debug
  debug:
    var: services_state

示例输出:

...

TASK [Debug] ***************************************************************************************************************************************************************************************************************
ok: [local] => {
    "services_state": {
        "ansible_facts": {
            "services": {
                "cloud-init-local.service": {
                    "name": "cloud-init-local.service",
                    "source": "systemd",
                    "state": "stopped"
                },
                "firewalld.service": {
                    "name": "firewalld.service",
                    "source": "systemd",
                    "state": "stopped"
                },
                ...
            }
        }
    }
}

【讨论】:

  • 如何将 firewalld.service 放入变量中?由于它包含句点,Ansible 认为它是一个对象,因此运行 debug: var: firewall_status.ansible_facts.services.firewalld.service 会导致 "VARIABLE IS NOT DEFINED!"
  • @MaksimLuzik 称之为:var: firewall_status.ansible_facts.services['firewalld.service']
  • 如果firewalld 存储在变量中会怎样?
  • 我收到了ERROR! 'service_facts' is not a valid attribute for a Play(Ansible 2.9.6)
  • @xjcl 检查你的缩进...并确保你有一个 : 在它后面。 service_facts 是一个模块 (ansible.builtin.service_facts:)
【解决方案2】:

只需使用选项--check 运行任务service: name=httpd state=started。这告诉您,如果服务需要启动,这意味着它已关闭。如果任务显示没有变化,它已经完成了。

示例服务宕机,changed 为真,因为需要启动:

$ ansible -m service -a 'name=rpc/bind state=started' --check host
host | SUCCESS => {
    "changed": true, 
    "msg": "service state changed"
}

示例服务已启动,changed 为 false,因为无需执行任何操作:

$ ansible -m service -a 'name=system-log state=started' --check host
host | SUCCESS => {
    "changed": false, 
    "name": "system-log", 
    "state": "started"
}

【讨论】:

    【解决方案3】:

    一个非常简短的使用 ansible 检查服务的程序 -

    - name: checking service status
      hosts: www.linuxfoundation.org
      tasks:
      - name: checking service status
        command: systemctl status "{{ item }}"
        with_items:
        - firewalld
        - httpd
        - vsftpd
        - sshd
        - postfix
        register: result
        ignore_errors: yes
      - name: showing report
        debug:
         var: result
    

    【讨论】:

      【解决方案4】:

      将command 模块与service redis-server status 一起使用并解析标准输出。
      或者使用patched服务模块。

      【讨论】:

      • service redis-server status这个命令在不同的操作系统上会有不同的结果。
      • 这个补丁比较好,但是我想自己实现...但是找不到入口在哪里
      【解决方案5】:

      您通常不会使用 Ansible 执行此操作。 Ansible 应该用于以声明方式定义您希望服务器的外观。

      因此,您通常只需执行以下操作:

      - name: start redis
        service:
          name=redis-server
          state=started
          enabled=yes
      

      你可以有条件地做这样的事情:

      - name: restart redis
        service:
          name=redis-server
          state=restarted
          enabled=yes
        when: redis_config.changed
      

      在配置发生更改但很少需要检查服务是否正在运行时重新启动 Redis。

      在您确实需要检查服务是否正在运行的绝对情况下(我强烈建议您重新考虑一下您的 Ansible 角色/剧本),那么您总是可以掏空:

      - name: check redis status
        shell: service redis-service status
      

      【讨论】:

      • 我无法理解这个......请给我一些时间,我没有以这种方式使用 Ansible。
      • Per You wouldn't typically do this with Ansible. - 为此应该使用哪些工具?
      • @alex Ansible 旨在以声明方式定义基础架构或系统的状态。您可以直接使用 ansible 而不是 ansible-playbook 对 Ansible 清单或其他主机列表运行临时命令,但这对于更复杂的工具来说只是一个非常小的用法。当然,如果您已经在使用 Ansible,那么使用相同的工具可能会有所帮助,但通常会使用集群 SSH 或更好的工具来检查类似的东西,对于这个用例,监控工具。
      • 这不是真的。如果您想启动或停止可能安装或未安装在所有目标上的服务,您可能需要这样做。
      【解决方案6】:

      这是一个带有ansible.builtin.systemd 模块的干净示例。

      - name: Get Service Status
        ansible.builtin.systemd:
          name: "postgresql@13-main"
        register: pg_service_status
      
      - debug:
          var: pg_service_status.status.ActiveState
      

      输出将如下所示;

      ok: [db01] => {
          "pg_service_status.status.ActiveState": "inactive"
      }
      

      【讨论】:

        【解决方案7】:

        您可以通过 ansible AD-HOC 命令执行此操作:

        $ansible all -m shell -a "service redis-server status"
        

        【讨论】:

          【解决方案8】:

          你可以这样说:

          ansible all -m shell -a "if ! systemctl is-active firewalld; then echo 'inactive' ; fi" -i inventory

          如果你想在 plyabook 中使用它,你可以尝试以下方法:

          - name: my playbook example
            hosts: all
            gather_facts: no
            tasks:
            - name: test_task
              shell: "if ! systemctl is-active firewalld; then  echo 'inactive' ; fi"
              register: firewalld_active
              failed_when: False
              changed_when: False
          
            - debug: var=firewalld_active
          
            - name: check_value_firewalld
              debug: 
                msg: "'firewalld is inactive' if firewalld_active.stdout=='inactive' else 'service is active' "
          

          希望对你有帮助!

          【讨论】:

          • 超级,在剧本中你有语法吗?
          • 您好,我更新了答案,希望对您有所帮助!
          【解决方案9】:

          就我个人而言,我希望有某种支持 Playbooks 来获取我的服务在我的环境中的状态并能够重新启动它们等。

          因此,一方面我将按照Konstantin Suvorov 的建议使用命令模块,但另外我还将检查预期的端口以确保所有必需的端口都已启动并且我的服务按预期工作。 在您的情况下,这将如下所示:

          - name: verify redis-server service
            command: /usr/sbin/sservice redis-server status
            changed_when: false
          
          - name: verify redis-server is listening on 6379
            wait_for: port=6379 timeout=1
          

          changed_when 只是被使用,因为命令模块总是将 changed 设置为 true,尽管它只是一个只读命令。

          【讨论】:

            【解决方案10】:

            如果您的服务没有启用systemctl /service systemd 脚本。在我的情况下,我通过执行此命令手动启动 zookeeper 服务/opt/zookeeper/bin/zkServer.sh start

            把它变成ansible

            - name: Start Zookeeper service
              command: /opt/zookeeper/bin/zkServer.sh start
              tags:
              - start_zookeeper
            
            - name: Validate whether zookeeper service is running or not 
              shell: netstat -plnt | grep $(ps -ef | grep zookeeper.server.quorum.QuorumPeerMain | grep -v "grep" | awk '{print $2}')
              args:
                executable: /bin/bash
              register: zookeeper_port_status
              retries: 5
              delay: 3
              until: zookeeper_port_status.stdout.find('{{zookeeper_port}}') != -1 
              tags:
              - validate_zookeeeper_service
            

            我正在使用netstat 和ps -ef linux 命令检查zookeeper 服务状态

            如果 zookeeper 服务没有获取到 2181 端口,那么第二个 ansible 模块(- name: Validate whether zookeeper service is running or not) 尝试 5 次后会失败(retries: 5)

            Zookeeper-Port {{zookeeper_port}}Variable 我已在库存文件中定义

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-08-08
              • 2021-12-18
              • 1970-01-01
              • 2018-06-24
              相关资源
              最近更新 更多