0

I'm using the following state to try to comment out two lines in a file:

/etc/cloud/cloud.cfg:
  file.comment:
    - regex: ^ - set_hostname
    - regex: ^ - update_hostname

Unfortunately, as expected it's only using the latter regex line, and ignoring the first.

How can I comment out more than one line in a file using file.comment?

Soviero
  • 4,306
  • 7
  • 34
  • 59

2 Answers2

0

Kind of hackish, but you can chain the regex with an or:

/etc/cloud/cloud.cfg:
  file.comment:
    - regex: ^ - set_hostname|^ - update_hostname
dglloyd
  • 171
  • 5
  • Unfortunately, due to the way Salt interprets the regex, that won't work. Salt doesn't use the regex directly, instead it converts `^text` into `^(text)$` and uses that to find the line to comment, so chaining won't work since it won't convert it correctly. Thanks though. – Soviero Jul 18 '15 at 19:41
  • I actually tested that line on a 2014.7.5 install prior to commenting and it did in fact comment out both lines in my test file – dglloyd Jul 19 '15 at 20:47
0

After working on this all night, I found a solutions that works.

Here's a solutions that DOESN'T WORK:

/etc/cloud/cloud.cfg:
  file.comment:
    - regex: ^ - set_hostname

/etc/cloud/cloud.cfg:
  file.comment:
    - regex: ^ - update_hostname

The reason that doesn't work is that the /etc/cloud/cloud.cfg bit is used as the ID for the state, and no two IDs can ever be the same in a SLS file since IDs are global. However, there's an alternative way to write states:

comment_set_hostname:
  file.comment:
    - name: /etc/cloud/cloud.cfg
    - regex: ^ - set_hostname

comment_update_hostname:
  file.comment:
    - name: /etc/cloud/cloud.cfg
    - regex: ^ - update_hostname

This version sets the file manually inside the state rather than including it as the ID of the state. In doing so, I can solve my problem.

Soviero
  • 4,306
  • 7
  • 34
  • 59