Home > Blockchain >  Configure a syslog server through Ansible Playbook
Configure a syslog server through Ansible Playbook

Time:11-04

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 "uncommented" (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.

    ---
    - 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

This part works. Now I have to change these lines in the /etc/rsyslog.conf file:

#module(load=”imudp”)
#input(type=”imudp” port=”514”)
#module(load=”imtcp”)
#input(type=”imtcp” port=”514”)

Here are the 3 options I have tried:

    ################
    ### Option 1 ###
    ################
      - name: Swap/ edit lines in config
        lineinfile:
          dest: /etc/rsyslog.conf
          line: 'module(load="imudp")' #new text
          regexp: '#module(load="imudp")' #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="imudp").*)'
              replace: '\1'
            - regexp: '^#(.*input(type="imudp" port="514").*)'
              replace: '\1'
            - regexp: '^#(.*module(load="imtcp").*)'
              replace: '\1'
            - regexp: '^#(.*input(type="imtcp" port="514").*)'
              replace: '\1'
              
    
    ################
    ### Option 3 ###
    ################
      - name: delete comment out lines in config
        ansible.builtin.replace:
          dest: /etc/rsyslog.conf
          regexp: '^#(.*module(load="imudp").*)'
          replace: 'module(load="imudp")'

So for this it says everything is OK but nothing changes. The next part works fine.

      - name: Restart service rsyslog
        ansible.builtin.service:
          name: rsyslog
          state: restarted

Does someone know what is wrong with my code? Thank you so much already!

The output is the same for all 3 options:


    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   

CodePudding user response:

you have parenthesis in your regex, its special char in regex so you have to escape it:

  - name: Swap/ edit lines in config
    lineinfile:
      dest: logs/testfile.log
      line: 'module(load="imudp")' #new text
      regexp: '#module\(load="imudp"\)' #old text
      state: present
      create: true

or use the special char .

  regexp: '#module.load="imudp".' #old text

CodePudding user response:

ansible.builtin.replace can be used:

  - name: replace
    replace:
      path: /etc/rsyslog.conf
      regexp: "{{ item }}"
      replace: "\\1"
    loop:
      - '#(module\(load=”imudp”\))'
      - '#(input\(type=”imudp” port=”514”\))'
      - '#(module\(load=”imtcp”\))'
      - '#(input\(type=”imtcp” port=”514”\))'
  • Related