1

I have an Ansible Inventory file which has ansible_password value which needs to be replaced by user input

what I have is below in a windows.yaml

ansible_user=sa_user@somewindowsdomain.external
ansible_password =

and I take an userInput from Jenkins, where user enters &32lihdye34jC5W

parameters {
            password defaultValue: '', description: 'Please Enter ansible_password ', name: 'sa_pass'
                      }

sh 'echo ${sa_pass}'

I tried below

sed -i "s/ansible_password=.*/ansible_password=${sa_pass}/g" $inventory_path/windows.yaml

which doesn't do what I wanted but got below

ansible_password=ansible_password32lihdye34jC5W

what I was expecting is

ansible_password = &32lihdye34jC5W

Aserre
  • 4,916
  • 5
  • 33
  • 56
Mahesh
  • 19
  • 2
  • In your yaml file, you write `ansible_password =` yet in your sed command you write `ansible_password=.*` (without a space). Is that a typo in your question or did you also forget the space when you executed the command ? – Aserre Feb 19 '19 at 16:34
  • It was a typo. Sorry – Mahesh Feb 19 '19 at 20:26

1 Answers1

1

One approach would be to avoid the s///. If you are using GNU sed you can do this:

$: a_pass="&32lihdye34jC5W"; echo "foo stuff
ansible_password=ansible_password32lihdye34jC5W
bar stuff" | sed -E "/ansible_password\s*=\s*/ {
                    e echo \"ansible_password = ${sa_pass}\";
                    d; }"
foo stuff
ansible_password = &32lihdye34jC5W
bar stuff

GNU sed's e is "execute" and runs a subcommand.
The d is needed to keep sed from printing that record aside from the e command.

Another way would be to pre-quote the password with POSIX chracter classes.

$: sa_pass="$( echo "&32lihdye34jC5W" |
                 sed 's/[[:punct:]]/\\&/g' )"
   echo "foo stuff
   ansible_password=ansible_password32lihdye34jC5W
   bar stuff" | sed -E "s/ansible_password\s*=\s*.*/ansible_password = ${sa_pass}/"
foo stuff
ansible_password = &32lihdye34jC5W
bar stuff

I think that should work with non-GNU versions.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
  • Fixed it in a dirty way :) ```TempVar=`echo $sa_pass | tr -s "&" "<"` sed -E -i "s/ansible_password/ansible_password=$TempVar/g" windows.yaml cat windows.yaml | tr -s "<" "&" > windows1.yaml && rm windows.yaml && mv windows1.yaml windows.yaml``` pardon my code formatting. – Mahesh Feb 20 '19 at 11:35
  • If it's a one-time thing, why not just open the file and change the password? – Paul Hodges Feb 20 '19 at 14:18