【问题标题】:running shall command with ansible使用 ansible 运行应命令
【发布时间】:2020-02-25 12:12:38
【问题描述】:

我是 Ansible 的新手,并且已经编写 .yml 文件来清空文件,就像 ">file_name"

---

tasks:


  - name: Empty Log Files greater then 400M



     shell: 'find "{{ item }}" -name "messages*" -size +400M -exec sh -c '> {}' \;'

    with_items:
      - /var/log
      - /var/opt
      - /var/spool/mail
    ignore_errors: yes

我收到以下错误

错误!加载 YAML 时出现语法错误。

错误似乎出现在 '/tmp/clean.yml':第 7 行,第 11 列,但可能 根据确切的语法问题,位于文件中的其他位置。

违规行似乎是:

  • 名称:删除大于 400M 的日志文件 shell: 'find "{{ item }}" -name "messages*" -size +400M -exec sh -c '> {}' \;' ^ 在这里我们可能是错的,但这个看起来可能是缺少引号的问题。总是引用模板表达式 当他们开始一个值时括号。例如:

    with_items:

    • {{ 富 }}

应该写成:

with_items:
  - "{{ foo }}"

异常类型:异常:映射 在此上下文中的“”行中不允许使用值 7,第11栏

我哪里弄错了?

【问题讨论】:

  • 嗨...WC到SO!我会建议使用 ansible find: 模块而不是 shell 命令。 docs.ansible.com/ansible/latest/modules/find_module.html...the yml 语法在 ansible 中扮演着重要的角色......你应该使用 ansible` --syntax-check` 选项来验证你的语法
  • 你检查过有问题的那条线吗?它在中间包含一个单引号

标签: ansible


【解决方案1】:

请参考下面的剧本作为参考。

---
- name: play to check the file size
  hosts: "{{ servers }}"
  tasks:
    - name: Recursively find /tmp files older than 4 weeks and equal or greater than 1 megabyte
      find:
        paths: "{{ item }}"
        age: 4w
        size: 1m
        recurse: yes
      with_items:
      - path_1
      - path_2
      - path_3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多