我是ansible新手,尝试在远程主机上安装rsyslog并将它们配置为客户端,并将事件转发到rsyslog服务器,如下所述:
如果rsyslog已经安装了
的形式工作
- else
- replace the the config file with my predefined rsyslog.conf (this step also works as expected)安装rsyslog (工作fine)
)
当远程主机没有安装rsyslog时,我得到一个错误:“在计算条件(ansible_facts.services"rsyslog.service".state !=”rsyslog.service时“):'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发布于 2021-08-13 17:15:22
我知道这个答复迟了8个月,但我想我会把我的解决方案放在这里,以防其他人遇到这种情况。
这里的主要问题是Ansible只在播放开始时收集事实,而不更新vars中间播放的事实,所以当您在安装后通过检查事实来检查服务的状态时,您是在对照初始状态进行检查。
有几种方法可以解决这个问题,但是最简单、最直接的方法就是再次调用Ansible setup模块(ansible.builtin.setup),过滤您想要的事实。像这样的东西看起来是这样的:
- name: SELinux - Force ansible to regather facts
setup: filter='rsyslog'我还没有对此进行准确的测试,但原理是合理的;像上面这样调用安装程序会重新调用安装模块,该模块从一开始就收集事实。--我不认为这会更新ansible_facts变量,因为您很可能不得不通过set_fact模块重新分配变量,或者用不同的选项调用安装模块。
https://stackoverflow.com/questions/65091645
复制相似问题