首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反式rsyslog安装和配置

反式rsyslog安装和配置
EN

Stack Overflow用户
提问于 2020-12-01 13:42:57
回答 1查看 2.9K关注 0票数 1

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

如果rsyslog已经安装了

    • 不做任何更改。如果rsyslog运行
      • ,则
      • 打印找到的转发地址。(目前,我将脚本"listIP.py“复制到远程主机,运行它,然后删除它。此步骤以expected)

的形式工作

代码语言:javascript
复制
- else 
    - replace the the config file with my predefined rsyslog.conf (this step also works as expected)

安装rsyslog (工作fine)

  • replace配置文件

  • 重新启动service

)

当远程主机没有安装rsyslog时,我得到一个错误:“在计算条件(ansible_facts.services"rsyslog.service".state !=”rsyslog.service时“):'dict‘没有属性'rsyslog.service’”

我认为这可能是因为rsyslog.service.state是在安装开始时(安装之前)填充的,因此出现了故障。

其他可能的选择是,在安装rsyslog之后,服务是不活动的(死的)。

有什么解决办法吗?

请把下面的剧本-

代码语言:javascript
复制
---

- 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
EN

回答 1

Stack Overflow用户

发布于 2021-08-13 17:15:22

我知道这个答复迟了8个月,但我想我会把我的解决方案放在这里,以防其他人遇到这种情况。

这里的主要问题是Ansible只在播放开始时收集事实,而不更新vars中间播放的事实,所以当您在安装后通过检查事实来检查服务的状态时,您是在对照初始状态进行检查。

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

代码语言:javascript
复制
- name: SELinux - Force ansible to regather facts
  setup: filter='rsyslog'

我还没有对此进行准确的测试,但原理是合理的;像上面这样调用安装程序会重新调用安装模块,该模块从一开始就收集事实。--我不认为这会更新ansible_facts变量,因为您很可能不得不通过set_fact模块重新分配变量,或者用不同的选项调用安装模块。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65091645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档