0

I need to delete a particular string from particular tag in a file using ansible, as below you can see from [pipeline:public_api] I need to remove admin_token_auth

    [pipeline:public_api]
    pipeline = cors sizelimit url_normalize request_id admin_token_auth build_auth_context token_auth json_body ec2_extension public_service

Desired outcome:

[pipeline:public_api]
pipeline = cors sizelimit url_normalize request_id build_auth_context token_auth json_body ec2_extension public_service
1

Have a look at the replace module. It is what I normally use for regular expressions in ansible:

This module will replace all instances of a pattern within a file.

Something like:

- replace:
    path: /etc/keystone/keystone-paste.ini
    regexp: '^(pipeline = .*) admin_token_auth(.*)'
    replace: '\1\2'

Should work.

EDIT:

Since that did not do the trick, I would instead use sed for this, and pass it to ansible with the script module.

First a simple sed script that gets the line number of your tag, and then does the change on the line after that tag. Example:

#!/bin/bash

# Get line number of tag
tagLine=$(sed -n '/pipeline:public_api/=' mohd.txt)

# The line we want to change is below $tagLine
editLine=$((tagLine+1))

# Do the actual replacement on the correct line
sed "${editLine}s/ admin_token_auth//" mohd.txt


PoC:

Example file (mohd.txt):

cat mohd.txt 
    [pipeline:public_bar]
    pipeline = cors sizelimit url_normalize request_id admin_token_auth build_auth_context token_auth json_body ec2_extension public_service


    [pipeline:public_foo]
    pipeline = cors sizelimit url_normalize request_id admin_token_auth build_auth_context token_auth json_body ec2_extension public_service


    [pipeline:public_api]
    pipeline = cors sizelimit url_normalize request_id admin_token_auth build_auth_context token_auth json_body ec2_extension public_service

    [pipeline:public_abc]
    pipeline = cors sizelimit url_normalize request_id admin_token_auth build_auth_context token_auth json_body ec2_extension public_service

Running the script on mohd.txt:

./pipeline_public_api.sh 
    [pipeline:public_bar]
    pipeline = cors sizelimit url_normalize request_id admin_token_auth build_auth_context token_auth json_body ec2_extension public_service


    [pipeline:public_foo]
    pipeline = cors sizelimit url_normalize request_id admin_token_auth build_auth_context token_auth json_body ec2_extension public_service


    [pipeline:public_api]
    pipeline = cors sizelimit url_normalize request_id build_auth_context token_auth json_body ec2_extension public_service

    [pipeline:public_abc]
    pipeline = cors sizelimit url_normalize request_id admin_token_auth build_auth_context token_auth json_body ec2_extension public_service

As you can see, admin_token_auth was removed from the line below [pipeline:public_api].

Now use the script module to execute that script on the hosts on your inventory. EXCEPT of course with the -i flag passed to sed, and the actual path and name to the file you want to change.

  • I need to remove content which is below [pipeline:public_api which is admin_token_auth – Mohammed Ali Jul 1 '17 at 17:20
  • Ok. Gotcha, see my edit. – maulinglawns Jul 1 '17 at 17:38
  • it did not worked as per plan, admin_token_auth had 3 instances in the file and they all were removed, where i only wanted to remove it from a particular tag of [pipeline: public_api] – Mohammed Ali Jul 2 '17 at 5:02
  • Hi any more thoughts on this???? – Mohammed Ali Jul 3 '17 at 16:44
  • Wow is it that difficult??? No answer from anyone!! – Mohammed Ali Jul 9 '17 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.