Tyler Dane
Tyler Dane

Reputation: 1069

How to use regex in SaltStack to comment out entire line

Background:

I'd like to comment out the following line in /etc/fstab:

/dev/mapper/rhel-swap   swap                    swap    defaults        0 0

After reading the Salt documentation on salt.states.file.comment, I came up with this in my .sls:

Permanently Disable Swap Memory:
  file.comment:
    - name: /etc/fstab
    - regex: swap

(I also tried ^swap.)

The output:

      ID: Permanently Disable Swap Memory on Cassandra Nodes
Function: file.comment
    Name: /etc/fstab
  Result: True
 Comment: Commented lines successfully
 Started: 08:13:09.809454
Duration: 25.587 ms
 Changes:
          ----------
          /etc/fstab:
              updated

The result: The line still isn't commented out. Notice the last line in my /etc/fstab after the Salt install:

/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=69cb98f5-5325-4168-a28c-c1219081ae16 /boot                   xfs     defaults        0 0
/dev/mapper/rhel-home   /home                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
~

Questions:

Other info:

Upvotes: 0

Views: 1680

Answers (1)

Tyler Dane
Tyler Dane

Reputation: 1069

Not sure why it wasn't working originally, but here's the regex that worked: ^(\/).*swap.*$. This matches any line starting with / and containing the word swap.

Here's the full salt step:

Permanently Disable Swap Memory:
  file.comment:
    - name: /etc/fstab
    - regex: ^(\/).*swap.*$

Upvotes: 2

Related Questions