title: Configure a syslog server through Ansible Playbook
body: <p>I'm a student System and Network administrator and I have to install a syslog server on a linux virtual machine. In my playbook I configured that some of the config lines that are commented, should be &quot;uncommented&quot; (aka from #example to example). My code should work but for some reason it doesn't. It doesn't give an error when I run it, but it doesn't change anything.. Here is my playbook with 3 different options I have tried.</p>
  <pre><code>    ---
      - name: Configure syslog server
        hosts: servers
        become: true
        tasks:
        - name: Install rsyslog
          apt:
            name: rsyslog
            state: present
      
        - name: Start service rsyslog, if not started
          ansible.builtin.service:
            name: rsyslog
            state: started
      
        - name: Enable service rsyslog to start on boot
          ansible.builtin.service:
            name: rsyslog
            enabled: yes
  </code></pre>
  <p>This part works. Now I have to change these lines in the /etc/rsyslog.conf file:</p>
  <pre><code>#module(load=”imudp”)
  #input(type=”imudp” port=”514”)
  #module(load=”imtcp”)
  #input(type=”imtcp” port=”514”)
  </code></pre>
  <p>Here are the 3 options I have tried:</p>
  <pre><code>    ################
      ### Option 1 ###
      ################
        - name: Swap/ edit lines in config
          lineinfile:
            dest: /etc/rsyslog.conf
            line: 'module(load=&quot;imudp&quot;)' #new text
            regexp: '#module(load=&quot;imudp&quot;)' #old text
            state: present
            create: true
      
      
      ################
      ### Option 2 ###
      ################
        - name: delete comment out lines in config
          ansible.builtin.replace:
            dest: /etc/rsyslog.conf
            regexp: '^#\s*{{ item.regexp }}(.*)$'
            replace: '{{ item.replace }}'
          loop:
              - regexp: '^#(.*module(load=&quot;imudp&quot;).*)'
                replace: '\1'
              - regexp: '^#(.*input(type=&quot;imudp&quot; port=&quot;514&quot;).*)'
                replace: '\1'
              - regexp: '^#(.*module(load=&quot;imtcp&quot;).*)'
                replace: '\1'
              - regexp: '^#(.*input(type=&quot;imtcp&quot; port=&quot;514&quot;).*)'
                replace: '\1'
                
      
      ################
      ### Option 3 ###
      ################
        - name: delete comment out lines in config
          ansible.builtin.replace:
            dest: /etc/rsyslog.conf
            regexp: '^#(.*module(load=&quot;imudp&quot;).*)'
            replace: 'module(load=&quot;imudp&quot;)'
  </code></pre>
  <p>So for this it says everything is OK but nothing changes. The next part works fine.</p>
  <pre><code>      - name: Restart service rsyslog
          ansible.builtin.service:
            name: rsyslog
            state: restarted
  </code></pre>
  <p>Does someone know what is wrong with my code? Thank you so much already!</p>
  <p>The output is the same for all 3 options:</p>
  <pre><code>
      jess@jess-client1:~$ ansible-playbook -i hosts.ini syslog.yaml --ask-become-pass
      BECOME password: 
      
      PLAY [Configure syslog server] **********************************************************************************************************************
      
      TASK [Gathering Facts] ******************************************************************************************************************************
      ok: [naserver]
      
      TASK [Install rsyslog] ******************************************************************************************************************************
      ok: [naserver]
      
      TASK [Start service rsyslog, if not started] ********************************************************************************************************
      ok: [naserver]
      
      TASK [Enable service rsyslog to start on boot] ******************************************************************************************************
      ok: [naserver]
      
      TASK [delete comment out lines in config] ***********************************************************************************************************
      ok: [naserver]
      
      TASK [Restart service rsyslog] **********************************************************************************************************************
      changed: [naserver]
      
      PLAY RECAP ******************************************************************************************************************************************
      naserver                   : ok=6    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  
  </code></pre>