【问题标题】:Ansible - rsyslog installation and configurationAnsible - rsyslog 安装和配置
【发布时间】:2021-08-14 01:21:24
【问题描述】:

我是 ansible 的新手,并尝试在远程主机上安装 rsyslog 并将它们配置为客户端并将事件转发到 rsyslog 服务器,如下所述:

  • 如果已安装 rsyslog:
    • 不要进行任何更改。
    • 如果 rsyslog 正在运行
      • 打印找到的转发地址。 (目前我将脚本“listIP.py”复制到远程主机,运行它然后删除它。这一步按预期工作)
    • 其他
      • 用我预定义的 rsyslog.conf 替换配置文件(此步骤也可以正常工作)
  • 其他
    • 安装 rsyslog(工作正常)
    • 替换配置文件
    • 重启服务

当远程主机没有安装 rsyslog 时,我收到错误消息:“在评估条件时 (ansible_facts.services["rsyslog.service"].state != "running"): 'dict object' has no attribute 'rsyslog.service' "

我认为这可能是因为 rsyslog.service.state 是在一开始(安装之前)填充的,因此失败了。

其他可能的选项是在安装 rsyslog 后,服务处于非活动状态(死)。

有什么解决方法的建议吗?

请看下面的剧本-

---

- hosts: Linux_Servers
  become: yes
  gather_facts: True
  debugger: "on_failed"

  handlers:

    - name: Replace rsyslog config file
      copy:
        src: /home/controller/Desktop/rsyslog.conf
        dest: /etc/rsyslog.conf
        force: yes
      listen: "Replace config file"
    - name: verify rsyslog running
      service:
        name: rsyslog
        state: started
      listen: "Replace config file"

  tasks:

    - name: Gathering services state...
      service_facts:

    - name: do facts module to get latest information
      setup:
        filter:
          - 'rsyslog'

    - name: If Rsyslog installed, print rsyslog current status. else - move to OS based installation.
      debug:
        var: ansible_facts.services["rsyslog.service"]

    - name: OS check
      debug: msg="{{ ansible_distribution }}"
      register: distro

    - name: The detected OS is CentOS. Installing rsyslog...
      yum:
        name: rsyslog
      register: installedSuccess
      when: ansible_facts['distribution'] == 'CentOS'

    - name: The detected OS is Ubuntu. Installing rsyslog...
      apt:
        name: rsyslog
      register: installedSuccess
      when: ansible_facts['distribution'] == "Ubuntu"

    - name: After success installation, replace config file.
      copy:
        src: /home/controller/Desktop/rsyslog.conf
        dest: /etc/rsyslog.conf
        force: yes
      when: installedSuccess

# update service data?

    - name: Rsyslog is installed but not running. restarts service now...
      service:
        name: rsyslog
        state: started
      when: ansible_facts.services["rsyslog.service"].state != "running"

    - name: Rsyslog is installed and running. Copying script to find forwrding address...
      copy: src=/home/controller/Desktop/listIP.py dest=listIP.py
      when: ansible_facts.services["rsyslog.service"].state == "running"

    - name: search rsyslog_conf for rsyslog server IP addresses
      command: python listIP.py
      register: IPaddress
      when: ansible_facts.services["rsyslog.service"].state == "running"

    - name: Found forwarding address-
      debug: msg="{{ IPaddress.stdout }}"

    - name: Clean Script From Remote Host
      file:
        state: absent
        path: "listIP.py"
      when: IPaddress

    - name: No forwarding address found. Replace conf file...
      copy:
        src: /home/controller/Desktop/rsyslog.conf

【问题讨论】:

  • 您需要在检查服务状态之前运行安装步骤

标签: ubuntu automation ansible rsyslog centos8


【解决方案1】:

我知道这个回复晚了 8 个月,但我想我会在这里提出我的解决方案,以防其他人遇到这个问题。

这里的主要问题是 Ansible 仅在播放开始时收集事实并且不会在播放中更新事实变量,因此当您通过检查事实检查安装后服务的状态时,您正在检查反对初始状态。

有几种方法可以解决这个问题,但最简单和最直接的方法是再次调用 Ansible 设置模块 (ansible.builtin.setup),过滤您想要的事实。类似的东西看起来像这样:

- name: SELinux - Force ansible to regather facts
  setup: filter='rsyslog'

我还没有完全测试过,但原理是正确的;像上面这样调用 setup 会重新调用 setup 模块,该模块在开始时收集事实。 我认为这不会更新 ansible_facts 变量,因为您很可能必须通过 set_fact 模块重新分配变量,或者使用不同的选项调用 setup 模块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-10
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 2016-08-24
    • 1970-01-01
    相关资源
    最近更新 更多