无法使用Ansible的blockinfile模块构造XML

时间:2019-12-05 11:35:01

标签: xml module ansible

我正在尝试使用“ blockinfile”模块在Ansible中构造如下所示的xml。

<section name="FAILED!!" fontcolor="RED">
    // To display a field
    <field name="FAILED" titlecolor="RED" value="FAILED!!!" detailcolor="RED" href="bad_{{ slist[index] }}_{{ dlist[index] }}_{{ foldertype }}.txt"> <![CDATA[  ]]> </field>

    // To display a table
    <table>
        <tr>
            <td value="{{ slist[index] }}_{{ dlist[index] }}_{{ foldertype }}" bgcolor="" fontcolor="" title="" fontattribute="" href="bad_{{ slist[index] }}_{{ dlist[index] }}_{{ foldertype }}.txt" align="" width=""/>
      </tr>
    </table>

</section>

注意:将有多个条目...。为此,我编写了第二个blockinfile模块。

下面是我尝试过的剧本代码中相关的部分。但是,它失败并显示错误。

- name: Construct HEADER
  local_action: blockinfile path: "{{ playbook_dir }}/fdump/report_header.xml" block: |
"<section name=\"FAILED!!\" fontcolor=\"RED\">
   <field name=\"FAILED\" titlecolor=\"RED\" value=\"FAILED!!!\" detailcolor=\"YELLOW\" href=\""bad_{{ slist[index] }}_{{ dlist[index] }}_{{ foldertype }}.txt"\"> <![CDATA[  ]]> </field>
       <table>"
  run_once: True
  when: outputfiles.rc != 0

- name: Construct XML BODY
  local_action: blockinfile path: "{{ playbook_dir }}/fdump/report_body.xml" block: |
        <tr>
            <td value="Hi" bgcolor="" fontcolor="" title="" fontattribute="" href="bad_{{ slist[index] }}_{{ dlist[index] }}_{{ foldertype }}.txt" align="" width=""/>
        </tr>
  when: outputfiles.rc != 0

- name: Construct XML FOOTER
  local_action: blockinfile path: "{{ playbook_dir }}/fdump/report_body.xml" block: |
    </table>
</section>

但是,它失败并显示以下错误:

fatal: [localhost]: FAILED! => {"reason": "Syntax Error while loading YAML.\n  mapping values are not allowed in this context\n\nThe error appears to be in '/app/comp.yml': line 40, column 33, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n- name: Construct HEADER\n  local_action: blockinfile path: \"{{ playbook_dir }}/fdump/report_header.xml\" block: |\n                                ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}
fatal: [localhost]: FAILED! => {"reason": "Syntax Error while loading YAML.\n  mapping values are not allowed in this context\n\nThe error appears to be in '/app/comp.yml': line 40, column 33, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n- name: Construct HEADER\n  local_action: blockinfile path: \"{{ playbook_dir }}/fdump/report_header.xml\" block: |\n                                ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}

我正在使用有关Ansible的版本。

我是第一次使用blockinfile模块。您能建议我如何使它工作并满足我的要求。

我也不确定“ blockinfile”是否适合我的需求。

1 个答案:

答案 0 :(得分:1)

YAML文档中存在许多语法错误。

在您的任务中,您使用了如下语法:

local_action: blockinfile path: "{{ playbook_dir }}/fdump/report_body.xml" block: |

您似乎已经混淆了传统的key=value指定参数的格式和YAML指定参数的机制。使用local_action使您的生活变得复杂,在所有情况下都应将其替换为delegate_to: localhost

您的YAML中还存在一些缩进问题;当使用|运算符引入文字块时,该块中的行必须缩进。

如果我们要解决您的剧本的结构性问题,我们将得到以下信息:

---
- name: Construct HEADER
  delegate_to: localhost
  blockinfile:
    path: "{{ playbook_dir }}/fdump/report_header.xml"
    block: |
      <section name=\"FAILED!!\" fontcolor=\"RED\">
         <field name=\"FAILED\" titlecolor=\"RED\" value=\"FAILED!!!\" detailcolor=\"YELLOW\" href=\""bad_{{ slist[index] }}_{{ dlist[index] }}_{{ foldertype }}.txt"\"> <![CDATA[  ]]> </field>
             <table>
  when: outputfiles.rc != 0

- name: Construct XML BODY
  delegate_to: localhost
  blockinfile:
    path: "{{ playbook_dir }}/fdump/report_body.xml"
    block: |
      <tr>
          <td value="Hi" bgcolor="" fontcolor="" title="" fontattribute="" href="bad_{{ slist[index] }}_{{ dlist[index] }}_{{ foldertype }}.txt" align="" width=""/>
      </tr>
  when: outputfiles.rc != 0

- name: Construct XML FOOTER
  delegate_to: localhost
  blockinfile:
    path: "{{ playbook_dir }}/fdump/report_body.xml"
    block: |
      </table>
      </section>

但是尽管它可以正常运行,但可能无法满足您的要求。

  • 我怀疑您的意思是report_footer.xml或您的最终任务中的某些内容。
  • 默认情况下,blockinfile模块将使用以下形式的行围绕每个块:

    # BEGIN ANSIBLE MANAGED BLOCK
    ...
    # END ANSIBLE MANAGED BLOCK
    

    这将导致无效的XML文档。

在这里的示例中,您似乎不需要blockinfile,它用于在现有文档中插入或更新文本块。在这里,您似乎只是在创建一系列打算在某个时候连接在一起的文件。将其简化为单个任务并使用template module会更好。