2016-12-01 104 views
1

我有一個應該遠程部署crontab的crontab。但似乎它是抱怨一些語法錯誤。Ansible的YAML格式錯誤

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

看起來是混合起來,我發送作業。任何想法我可以在這裏做什麼?

回答

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 

輸出:

[email protected]:~/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 
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 {} \;" 

這應該是更好的

+0

我嘗試這樣做。但它不起作用。薩繆爾給出的答案工作。謝謝。 –