Running sed command from ansible

Viewed 24092

I'm trying to update a few .sql files and had success using sed in the command line but I cannot get it to work from ansible.

The command goes something like:

  1. cd /tmp/Ratings_DB
  2. Run the command as sudo - sudo sed -i 's/TYPE/ENGINE/' *.sql
  3. The files are created from root account hence sudo.

My goal is to replace TYPE with ENGINE as they are older .sql dumps. Unless there is a way to do with directly in MySQL - I cannot import them.

Error 'An exception occurred during task execution. To see the full traceback, use -vvv. The error was: OSError: [Errno 2] No such file or directory: '/home/jesse/Desktop/playbooks/cd /tmp/Ratings_DB'

Seems I'm not redirecting to the correct directory somehow.

2 Answers

You dont have to make 'cd', you can refer directly to the filename. Also, its better to use 'become=true' instead of using sudo:

---
- name: pb
  hosts: localhost
  become: true
  tasks:
    - name: replace in sql files
      shell: sed -i 's/TYPE/ENGINE/' /tmp/*.sql

run playbook as:

$ ansible-playbook pb.yml -e "ansible_sudo_pass=1234"

Better use replace or lineinfile module rather than generic sed .

eg.

- replace:
path: /etc/hosts
regexp: '(\s+)old\.host\.name(\s+.*)?$'
replace: '\1new.host.name\2'
backup: yes
Related