Score:0

How to delete a specific string from a line using ansible

co flag

I have some package added in yum exclude list in /etc/yum.conf and I want to remove a specific package from a exclude list

example:

exclude=java* exclude=kernel* java* exclude=java* kernel* exclude=kernel* abc* java* def*

I tried to add # but that do not serve the purpose and add comments to all exclude statement.

- name: Comment Java exclusion
  replace:
    path: /etc/yum.conf
    regexp: '(.*java*)'
    replace: '#\1'

Added more case scenarios.

Score:1
in flag

Replace it with an empty string.

- name: Comment Java exclusion
  replace:
    path: /etc/yum.conf
    regexp: '(java\*\s*)'
    replace: ''

Changes to the regex:

  • .* would have removed the exclude= as well, so I removed it
  • The * needs to be escaped, it's a regex modifier
  • added \s* to remove trailing whitespace as well

EDIT

Even better:

- name: Comment Java exclusion
  replace:
    path: ~/ansible/test.blah
    regexp: '^(exclude=.*)java\*\s*'
    replace: '\1'

This makes sure that only the line starting with exclude= is affected and works even when java* is not the first item in the list.

Kamal Asdeo avatar
co flag
grep exclude /etc/yum.conf exclude=java* exclude=kernel* java* exclude=java* kernel* exclude=kernel* abc* java* def* ----------------------------------------- $ grep exclude /etc/yum.conf exclude=exclude=kernel* exclude=kernel* exclude=kernel* abc* def*
Kamal Asdeo avatar
co flag
It did not work
in flag
It did work perfectly in my tests prior to posting this. If it did not work for you please edit your question to provide more details.
Kamal Asdeo avatar
co flag
I have added below cases also in my question, Can you please check exclude=java* exclude=kernel* java* exclude=java* kernel* exclude=kernel* abc* java* def*
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.