1

I want to delete below environment variables from etc/environment using Ansible.

export http_proxy="http://194.138.0.25:9400/"

export https_proxy="http://194.138.0.25:9400/"

export ftp_proxy="http://194.138.0.25:9400/"

Below code deletes only one env variable.

  • name: Delete variables from etc/environment

    replace: 
      path: /etc/environment
      regexp: 'export http_proxy="http://194.138.0.25:9400/"'
      replace: ''
    
  1. How to delete all the 3 environment variables?
  2. After deleting/replacing any one env variable, empty line is being added. how to avoid this?
2
  • Please read a regexp tutorial and edit your question with a proposition for your regexp that shows you at least made some effort to solve your issue by yourself. As is your question is barely asking "please write my regexp for me" as you pasted a totally fixed string which will match a single possibility (which totally blows-up the purpose of using a regexp). If you don't know where to start regex101.com is a pretty good reference. Regarding your second point (new lines left in file), you should look at lineinfile with state: absent which is better suited for your purpose. Commented Oct 8, 2022 at 8:27
  • your right, thank you, I will go through the link. Commented Oct 8, 2022 at 8:55

1 Answer 1

2

Use lineinfile. For example, the task below will remove all lines starting export and including the address 194.138.0.25:9400/

    - lineinfile:
        path: /tmp/environment
        regex: '^export.*194\.138\.0\.25:9400.*$'
        state: absent

Given the file

shell> cat /tmp/environment 
first line
export http_proxy="http://194.138.0.25:9400/"
export https_proxy="http://194.138.0.25:9400/"
export ftp_proxy="http://194.138.0.25:9400/"
last line

Running the playbook with options --check --diff gives (abridged)

TASK [lineinfile] ***************************************************
--- before: /tmp/environment (content)
+++ after: /tmp/environment (content)
@@ -1,5 +1,2 @@
 first line
-export http_proxy="http://194.138.0.25:9400/"
-export https_proxy="http://194.138.0.25:9400/"
-export ftp_proxy="http://194.138.0.25:9400/"
 last line
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.