Yaml 期待口述;得到:shell:错误
我得到下面的错误Yaml 期待口述;得到:shell:错误,yaml,ansible,ansible-playbook,Yaml,Ansible,Ansible Playbook,我得到下面的错误 ERROR: expecting dict; got: shell:"ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt" register:the_file 我想检查文件夹是否存在 如果存在,我需要指定一些路径/mule/acc 如果路径不存在,则需要指向/mule/bcc 以下是ansible的剧本 --- # get name of the .txt file - stat: path=/mule/ansipl
ERROR: expecting dict; got: shell:"ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt" register:the_file
---
# get name of the .txt file
- stat: path=/mule/ansiple/mule-enterprise-standalone-3.4.1
register:the_file
when: the_file.stat.exists == True
- shell:"ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt"
register:the_file
- debug: msg="{{ the_file }}"
- set_fact: app_folder="{{ the_file.stdout | replace('-anchor.txt','') }}"
- debug: msg="{{ app_folder }}"
- debug: msg="{{ the_file.stdout }}"
# delete the .txt file
- name: Delete the anchor.txt file
file: path="{{ the_file.stdout }}" state=absent
# wait until the app folder disappears
- name: Wait for the folder to disappear
wait_for: path="{{ app_folder }}" state=absent
# copy the zip file
- name: Copy the zip file
copy: src="../p" dest="/c"
你需要学习YAML语法。在每个冒号之后,需要添加一个空格。消息来自YAML解析器,告诉您它需要一个字典:
key1: value1
key2: value2
相反,它没有找到键值对:
key1:value1
具体而言,它抱怨这一行:
- shell:"ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt"
应该是哪一个
- shell: "ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt"
但在另外两行中也有同样的问题,看起来像:
register:the_file
应该是:
register: the_file
如果怀疑错误来自Ansible任务或仅仅来自YAML解析错误,请将YAML定义粘贴到任意位置
格式就到此为止。现在谈谈逻辑问题:
- stat: path=/mule/ansiple/mule-enterprise-standalone-3.4.1
register: the_file
when: the_file.stat.exists == True
除非您已经从上一个未显示的任务中注册了\u文件,否则这将无法工作<代码>当
是决定是否应运行任务的条件。无法根据任务的结果执行任务。它必须先运行,然后才能获得结果。在评估条件时,_文件将不存在,这将导致错误,抱怨无对象没有键stat
或类似的东西
然后在下一个任务中,再次使用相同的名称注册结果
- shell: "ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt"
register: the_file
这将覆盖以前注册的结果。也许你的意思是从第一个任务到第二个任务。但即使这样也行不通。仍将从跳过的任务中注册一个结果,只是声明该任务已跳过。您需要将结果存储在唯一的变量中,或者在单个任务中检查所有可能的文件位置
清理后,您的剧本将如下所示:
---
# get name of the .txt file
- stat:
path: /mule/ansiple/mule-enterprise-standalone-3.4.1
register: the_file
when: the_file.stat.exists
- shell: ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt
register: the_file
- debug:
msg: "{{ the_file }}"
- set_fact:
app_folder: "{{ the_file.stdout | replace('-anchor.txt','') }}"
- debug:
msg: "{{ app_folder }}"
- debug:
msg: "{{ the_file.stdout }}"
- name: Delete the anchor.txt file
file:
path: "{{ the_file.stdout }}"
state: absent
- name: Wait for the folder to disappear
wait_for:
path: "{{ app_folder }}"
state: absent
- name: Copy the zip file
copy:
src: ../analytic-core-services-mule-3.0.0-SNAPSHOT.zip
dest: /mule/ansiple/mule-enterprise-standalone-3.4.1
...
您几乎拥有了YAML权限,但您的文件中有一个YAML错误,即在线:
register:the_file
因为它前面的行通过定义一个基于冒号(“:
”)和空格的键值对(后面不能跟标量(或序列),将顶级序列的第一个元素作为映射启动。如果该文件是您的输入,则YAML解析器会出现语法错误,部分消息如下所示:
register:the_file
^
could not find expected ':'
这:
很好,亚马尔。它将顶级序列的第二个元素定义为标量字符串:
shell:"ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt" register:the_file
(可以在YAML中的多行上断开标量字符串)
现在ansible不喜欢这样,希望该元素,可能还有所有顶级序列元素都是映射。对于映射,您需要有一个键值对,它(与register:the_file
)由YAML中的一对冒号空格字符分隔/指示。所以ansible(不是YAML)可能想要:
shell: ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt
register: the_file
请注意,标量值ls/mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt的引号在YAML中是不必要的
ansible对文件结构可能还有其他要求,但我将从上述更改开始,看看ansible是否会在以下方面引发更多错误:
# get name of the .txt file
- stat: path=/mule/ansiple/mule-enterprise-standalone-3.4.1
register: the_file
when: the_file.stat.exists == True
- shell: ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt
register: the_file
- debug: msg="{{ the_file }}"
- set_fact: app_folder="{{ the_file.stdout | replace('-anchor.txt','') }}"
- debug: msg="{{ app_folder }}"
- debug: msg="{{ the_file.stdout }}"
# delete the .txt file
- name: Delete the anchor.txt file
file: path="{{ the_file.stdout }}" state=absent
# wait until the app folder disappears
- name: Wait for the folder to disappear
wait_for: path="{{ app_folder }}" state=absent
# copy the zip file
- name: Copy the zip file
copy: src="../analytic-core-services-mule-3.0.0-SNAPSHOT.zip" dest="/mule/ansiple/mule-enterprise-standalone-3.4.1"
您在哪个任务上遇到此错误?你能粘贴完整的剧本吗?它的执行需要检查/mule/ansiple/mule-enterprise-standalone-3.4.1路径。如果没有,我需要查看不同的路径。上面的脚本在单路径下运行良好。我必须将文件复制到两个不同的主机上,使用相同的文件名,但使用不同的路径。不需要在每次复制后都有一个空格YAML中的冒号(abc:def
是定义7个字符标量的完美YAML文件)。op得到的消息不是来自YAML解析器。您推荐的在线YAML解析器无法处理遵循YAML 1.2规范(2009年发布)的文件,也无法处理所有YAML 1.1(从2005年起)。这可能与ansible无关,但如果你在2016年推荐这样的工具来学习YAML,那就令人困惑了。我承认我认为这在YAML中通常是必需的,因为ansible和在线解析器都需要空间。似乎两者都只支持YAML 1.0。Ansible使用了几乎是YAML 1.1的功能。它似乎坚持将映射作为顶级序列项。
# get name of the .txt file
- stat: path=/mule/ansiple/mule-enterprise-standalone-3.4.1
register: the_file
when: the_file.stat.exists == True
- shell: ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt
register: the_file
- debug: msg="{{ the_file }}"
- set_fact: app_folder="{{ the_file.stdout | replace('-anchor.txt','') }}"
- debug: msg="{{ app_folder }}"
- debug: msg="{{ the_file.stdout }}"
# delete the .txt file
- name: Delete the anchor.txt file
file: path="{{ the_file.stdout }}" state=absent
# wait until the app folder disappears
- name: Wait for the folder to disappear
wait_for: path="{{ app_folder }}" state=absent
# copy the zip file
- name: Copy the zip file
copy: src="../analytic-core-services-mule-3.0.0-SNAPSHOT.zip" dest="/mule/ansiple/mule-enterprise-standalone-3.4.1"