title: Ansible - rsyslog installation and configuration
body: <p>I am new to ansible, and trying to install rsyslog on remote hosts and configure them as clients and to forward events to the rsyslog server as described below:</p>
  <ul>
  <li>If rsyslog is already installed:
  <ul>
  <li>don't make any changes.</li>
  <li>If rsyslog is running
  <ul>
  <li>print the forwarding addresses found. (Currently I copy the script &quot;listIP.py&quot; to the remote host, run it and then delete it. This step is working as expected)</li>
  </ul>
  </li>
  <li>else
  <ul>
  <li>replace the the config file with my predefined rsyslog.conf (this step also works as expected)</li>
  </ul>
  </li>
  </ul>
  </li>
  <li>else
  <ul>
  <li>install rsyslog (working fine)</li>
  <li>replace config file</li>
  <li>restart service</li>
  </ul>
  </li>
  </ul>
  <p>when the remote host doesn't have rsyslog installed, I get the error: &quot;while evaluating conditional (ansible_facts.services[&quot;rsyslog.service&quot;].state != &quot;running&quot;): 'dict object' has no attribute 'rsyslog.service' &quot;</p>
  <p>I think it might be because the rsyslog.service.state was populated at the very beginning (before the installation) and therefore the failure.</p>
  <p>Other possible option is that after the installing rsyslog, the service is inactive (dead).</p>
  <p>Any suggestions how to solve it ?</p>
  <p>Please the the playbook below-</p>
  <pre><code>---
  
  - hosts: Linux_Servers
    become: yes
    gather_facts: True
    debugger: &quot;on_failed&quot;
  
    handlers:
  
      - name: Replace rsyslog config file
        copy:
          src: /home/controller/Desktop/rsyslog.conf
          dest: /etc/rsyslog.conf
          force: yes
        listen: &quot;Replace config file&quot;
      - name: verify rsyslog running
        service:
          name: rsyslog
          state: started
        listen: &quot;Replace config file&quot;
  
    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[&quot;rsyslog.service&quot;]
  
      - name: OS check
        debug: msg=&quot;{{ ansible_distribution }}&quot;
        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'] == &quot;Ubuntu&quot;
  
      - 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[&quot;rsyslog.service&quot;].state != &quot;running&quot;
  
      - 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[&quot;rsyslog.service&quot;].state == &quot;running&quot;
  
      - name: search rsyslog_conf for rsyslog server IP addresses
        command: python listIP.py
        register: IPaddress
        when: ansible_facts.services[&quot;rsyslog.service&quot;].state == &quot;running&quot;
  
      - name: Found forwarding address-
        debug: msg=&quot;{{ IPaddress.stdout }}&quot;
  
      - name: Clean Script From Remote Host
        file:
          state: absent
          path: &quot;listIP.py&quot;
        when: IPaddress
  
      - name: No forwarding address found. Replace conf file...
        copy:
          src: /home/controller/Desktop/rsyslog.conf
  
  </code></pre>