Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ubuntu Ansible-rsyslog安装和配置_Ubuntu_Automation_Ansible_Rsyslog_Centos8 - Fatal编程技术网

Ubuntu Ansible-rsyslog安装和配置

Ubuntu Ansible-rsyslog安装和配置,ubuntu,automation,ansible,rsyslog,centos8,Ubuntu,Automation,Ansible,Rsyslog,Centos8,我是ansible新手,正在尝试在远程主机上安装rsyslog,并将其配置为客户端,并将事件转发到rsyslog服务器,如下所述: 如果已安装rsyslog: 不要做任何改变 如果rsyslog正在运行 打印找到的转发地址。(目前我将脚本“listIP.py”复制到远程主机,运行它,然后删除它。此步骤按预期工作) 否则 将配置文件替换为预定义的rsyslog.conf(此步骤也按预期工作) 否则 安装rsyslog(工作正常) 替换配置文件 重新启动服务 当远程主机没

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

  • 如果已安装rsyslog:
    • 不要做任何改变
    • 如果rsyslog正在运行
      • 打印找到的转发地址。(目前我将脚本“listIP.py”复制到远程主机,运行它,然后删除它。此步骤按预期工作)
    • 否则
      • 将配置文件替换为预定义的rsyslog.conf(此步骤也按预期工作)
  • 否则
    • 安装rsyslog(工作正常)
    • 替换配置文件
    • 重新启动服务
当远程主机没有安装rsyslog时,我会得到错误:“在计算条件(ansible_facts.services[“rsyslog.service”].state!=“running”):“dict对象”没有属性“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


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