Ansible 的 YAML 格式错误

YAML formatting error for Ansible

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

A​​nsible 剧本是:

---
- 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"'

看起来它混淆了我发送的作业。我可以在这里做什么?

你混合引用了

       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 {} \;"

这个应该更好

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

此外,我认为您不需要反斜杠括号。

这是一个适合我的 yaml。

- name: Whosebug
  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 [Whosebug] ***********************************************************

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

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

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