【问题标题】:Running sed command from ansible从 ansible 运行 sed 命令
【发布时间】:2018-10-28 17:42:30
【问题描述】:

我正在尝试更新一些 .sql 文件并在命令行中使用 sed 成功,但我无法通过 ansible 使其工作。

命令类似于:

  1. cd /tmp/Ratings_DB
  2. 以 sudo - sudo sed -i 's/TYPE/ENGINE/' *.sql 运行命令
  3. 文件是从 root 帐户创建的,因此是 sudo。

我的目标是用 ENGINE 替换 TYPE,因为它们是较旧的 .sql 转储。除非有办法直接在 MySQL 中处理 - 我无法导入它们。

错误 '任务执行过程中发生异常。要查看完整的回溯,请使用 -vvv。错误是:OSError: [Errno 2] No such file or directory: '/home/jesse/Desktop/playbooks/cd /tmp/Ratings_DB'

似乎我没有以某种方式重定向到正确的目录。

【问题讨论】:

  • 你能发一个代码示例吗?

标签: shell ansible command line


【解决方案1】:

你不必制作'cd',你可以直接引用文件名。此外,最好使用 'become=true' 而不是使用 sudo:

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

将剧本运行为:

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

【讨论】:

  • 使用line-in-file模块有更好的方法。但问题是关于在 ansible 中运行 sed。
  • 满意请采纳,谢谢。
【解决方案2】:

最好使用 replace 或 lineinfile 模块而不是通用 sed。

例如。

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

【讨论】:

    猜你喜欢
    • 2013-12-07
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多