Ansible的YAML格式错误

时间:2016-12-01 21:31:12

标签: ansible ansible-playbook

我有一个crontab应该远程部署crontab。但似乎它抱怨一些语法错误。

Ansible剧本是:

---
- hosts: cac
  tasks:
#  - name: Deploy cron to GZIP old log/out files.
   - cron:
       name: "Cron entry to gzip rotated log/out files."
       minute: "0"
       hour: "*"
       job: "find /opt/app/log/ -maxdepth 1 \( -name "*out.*[0-9]" -o -name "*.log.[0-9]" \) -type f -size +100M -exec tar -czf {}.tar.gz {} \;"
       state: present
       disabled: yes

我得到的错误是:

ERROR! Syntax Error while loading YAML.


The error appears to have been in '/home/ansible/playbooks/deploy_cac_cron.yaml': line 9, column 69, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

       hour: "*"
       job: 'find /opt/app/log/ -maxdepth 1  -name '*out.*[0-9]' -o -name '*.log.[0-9]'  -type f -size +100M -exec tar -czf {}.tar.gz {} \; '
                                                                    ^ here
We could be wrong, but this one looks like it might be an issue with
unbalanced quotes.  If starting a value with a quote, make sure the
line ends with the same set of quotes.  For instance this arbitrary
example:

    foo: "bad" "wolf"

Could be written as:

    foo: '"bad" "wolf"'

看起来它正在混淆我发送的工作。我能在这做什么想法?

2 个答案:

答案 0 :(得分:3)

你必须在外部双引号内反斜杠双引号(“)以告诉编译器它们是命令的一部分,否则一旦编译器看到下一个”它将假定它作为结束命令因此使声明的其余部分无效。

另外,我认为你不需要反斜杠括号。

这是一个适合我的yaml。

- name: Stackoverflow
  hosts: localhost
  tasks:
    - cron:
        name: "Cron entry to gzip rotated log/out files."
        minute: "0"
        hour: "*"
        job: "find /opt/app/log/ -maxdepth 1 ( -name \"*out.*[0-9]\" -o -name \"*.log.[0-9]\" ) -type f -size +100M -exec tar -czf {}.tar.gz {} ;"
        state: present
        disabled: yes

<强>输出:

tigerroarz@tigerroarz-Latitude-E6520:~/work/feature/so-playground$ ansible-playbook site.yml
 [WARNING]: provided hosts list is empty, only localhost is available


PLAY [Stackoverflow] ***********************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [cron] ********************************************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0   

答案 1 :(得分:0)

你是混合引用

       job: "find /opt/app/log/ -maxdepth 1 \( -name '*out.*[0-9]' -o -name '*.log.[0-9]' \) -type f -size +100M -exec tar -czf {}.tar.gz {} \;"

这应该更好