Sunday, May 5, 2024
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 1040  / 1 Year ago, sat, march 4, 2023, 5:16:21

I'm trying to used sed to find and encrypt ansible_password field in an inventory file and encrypt it with ansible-vault command. But ansible-command is failing throwing an sed error, where as a simple echo is working fine.


sed -n "s/( *)(ansible_password: *)"(.*)"/$(echo 3)/p" sample.yml

is outputting the attribute value without the double quotes.


The ansible-vault command:


sed -n "s/( *)(ansible_password: *)"(.*)"/$(ansible-vault encrypt_string --vault-password-file ~/password.txt --name 'ansible_password' '3')/p" sample.yml

is throwing an error message: sed: -e expression #1, char 67: unterminated ``s' command


Here's the line where the substitution is happening:


        ansible_password: "somepassword"

More From » command-line

 Answers
3

This cannot work. The nested command will run first. echo works by accident because it will put 3 as replacement pattern which happens to be what you want in this case (= output the original value).


Not sure what you exactly want, maybe you can try grep and xargs:


grep -Po 'ansible_password: K"[^"]*' sample.yaml 
| xargs -n1 ansible-vault encrypt_string --vault-password-file ~/password.txt --name 'ansible_password'

However, you might want to use a proper yaml parser instead of grep or sed.


[#1553] Monday, March 6, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
truwom

Total Points: 101
Total Questions: 99
Total Answers: 100

Location: Ivory Coast
Member since Tue, Sep 15, 2020
4 Years ago
;