Score:0

Ansible: escaping special characters in a regexp loop

us flag

I been having problems getting this to work, can anyone provide any insights?

    - name Configuring rsyslog.conf playbook
      hosts: "{{host}}"
      tasks:
      - name: Configuring rsyslog.conf
        lineinfile:
          path: /etc/rsyslog.conf
          regexp: "{{item.regexp}}"
          line: "{{item.line}}"
          state: present
        loop:
        - { regexp='^action\\(type=', line='action(type"omfwd"' }
        - { regexp='^Target=', line='Target="192.168.1.20" Port="514" Protocol="tcp")' }
        - { regexp='^$PreserveFQDN', line='$PreserveFQDN on' }
        - { regexp='^$template defaultTemplate', line='$template defaultTemplate,\"<%PRI%>%TIMESTAMP% %HOSTNAME% %syslogtag% %msg% ID:123456789\n\"' }
        - { regexp='^$template writeLogs', line='$template writeLogs,\"/var/log/ID-123456789-%$year%%$month%%$day%.log\"' }

My main issue is the 4th loop. I escaped the double quotes to get rid of the "missing comma error" but now I get the "colon error"

EDIT: So my end result I'd like is pretty much this with that 4th loop:

find:

$template defaultTemplate

replace with:

$template defaultTemplate,"<%PRI%>%TIMESTAMP% %HOSTNAME% %syslogtag% %msg% ID:123456789\n"

I'm unable to cut and paste anything from my computer as it's on a different network. The big issue I'm seeing is knowing what needs to be escaped. I thought it was kinda weird that I had to escape the "(" in the first loop. I been trying all kinds of escapes and none seems to work. It would be so much easier if everything placed in single quotes would be what was literally written in, but it doesn't seem to work that way.

Hope this makes it a bit more clear.

EDIT: one thing I'd like to add, is doing these tasks individually works just fine. It's just when I put it in a loop that it doesn't want to work.

br flag
1) Provide an example of minimal *rsyslog.conf* you want to update. 2) Provide the expected result.
Score:2
cn flag

First, the json/yaml syntax for the list of dictionaries in the loop should be one of the following (note first item vs the rest of them):

- name: Configuring rsyslog.conf playbook
  hosts: localhost
  gather_facts: no
  tasks:
  - debug: var=item
    loop:
    - regexp: '^action\(type='
      line: 'action(type"omfwd"'
    - { regexp: '^Target=', line: 'Target="192.168.1.20" Port="514" Protocol="tcp")' }
    - { regexp: '^Target=', line: 'Target="192.168.1.20" Port="514" Protocol="tcp")' }
    - { regexp: '^$PreserveFQDN', line: '$PreserveFQDN on' }
    - { regexp: '^$template defaultTemplate', line: '$template defaultTemplate,\"<%PRI%>%TIMESTAMP% %HOSTNAME% %syslogtag% %msg% ID:123456789\n\"' }
    - { regexp: '^$template writeLogs', line: '$template writeLogs,\"/var/log/ID-123456789-%$year%%$month%%$day%.log\"' }

Next, you will need to fix the regexp/line in file too. They are too borked for us to fix them. You need to provide a minimal example of original and modified file.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.