【问题标题】:Ansible playbook create new directory failsAnsible playbook 创建新目录失败
【发布时间】:2021-04-07 10:36:08
【问题描述】:

我使用以下 playbook 结构将数据从 yml 文件加载到 Jinja2 模板,然后生成具有相同结构的新 yml 文件。

ansible-starter.yml

- name : Properties configuration - Jinja2
  hosts: localhost
  vars_files:
     - ../data.yml
  tasks:
    - name: Creating config directory
       file:
         path: ../j2-resources/generated
         state: directory
         mode: '0755'
     - name: Applying configuration template to data.yml
       template:
          src: ./template-files/template.yml.j2
          dest: ./generated/updated.yml

但它总是给出以下错误:

Syntax Error while loading YAML.
mapping values are not allowed in this context

The error appears to be in '/j2-resources/ansible-starter.yml': line 17, column 11, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

 - name: Creating config directory
    file:
        ^ here

似乎存在一些缩进问题。确保在执行之前让 yml 文件通过 yml 格式化程序。

【问题讨论】:

  • 检查file: 缩进。有一个额外的前导空格。
  • 已检查。事实并非如此
  • 错误有变化吗?
  • 没有。仍然是同样的错误。也检查了 ansible.builtin.file: 。不工作
  • 我发现了错误。行- name: Applying configuration... 缩进不好。有一个额外的前导空格。

标签: ansible yaml


【解决方案1】:

有几个缩进错误:

应该是:

- name: Properties configuration - Jinja2
  hosts: localhost
  vars_files:
     - ../data.yml
  tasks:
    - name: Creating config directory
      file:
         path: ../j2-resources/generated
         state: directory
         mode: '0755'
    - name: Applying configuration template to data.yml
      template:
         src: ./template-files/template.yml.j2
         dest: ./generated/updated.yml

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 2020-03-28
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多