skip to Main Content

I have an Ansible playbook which is supposed to replace some characters using stream editor.

sed -i "s+foo+$(<replace_file)+g" some_file

or

sed -i "s/foo/$(<replace_file)/g" some_file

cat some_file

line one: foo

cat replace_file

bar

expected result

line one: bar

actual result

line one: $(<bar)

I have tried the command directly on Ubuntu distro and it works perfectly, but running the command through Ansible gives the error.

2

Answers


  1. The redirection syntax you are trying to use is a Bash feature; if you are running the script using sh, it will not work (though I would expect a syntax error rather than what you say in your question that you are getting; it is probably no longer correct after you changed the quotes). See also Difference between sh and bash.

    Your approach seems extremely brittle, but

    sed -i "s+foo+$(cat replace_file)+g" some_file
    

    should work portably in any POSIX shell.

    Login or Signup to reply.
  2. Another solution would be to actually use bash as the executable:

    - shell: sed -i "s+foo+$(<replace_file)+g" some_file
      args:
        executable: /bin/bash
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search