【发布时间】: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