带有变量的 Ansible 和字典键

带有变量的 Ansible 和字典键

我正在尝试创建一个模板Ansible 剧本中的任务是迭代包含一些变量的字典:

- name: Task name
  lineinfile:
    path: /foo/bar
    regexp: "{{ item.regexp }}"
    line: "{{ item.line }}"
    with_items:
      - { regexp: "^(\/\/)?const NodeName", line: "const NodeName = '{{ inventory_hostname }}'" }
      - { regexp: "^(\/\/)?const ZoneName", line: "const ZoneName = '{{ inventory_hostname }}'" }
      - { regexp: "^(\/\/)?const {{ plugins_custom_dir_var_name }} =(.+)", line: "const {{ plugins_custom_dir_var_name }} = '{{ plugins_dest_dir }}'" }

此任务给出以下语法错误:

The offending line appears to be:\n\n    with_items:\n      - { regexp: \"^(\\/\\/)?const NodeName\", line: \"const NodeName = '{{ inventory_hostname }}'\" }\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"}

我不明白我错在哪里。

答案1

你输入错了,with_items 不是参数。

- name: Task name
  lineinfile:
    path: /foo/bar
    regexp: "{{ item.regexp }}"
    line: "{{ item.line }}"
  with_items:
    - { regexp: "^(\/\/)?const NodeName", line: "const NodeName = '{{ inventory_hostname }}'" }
    - { regexp: "^(\/\/)?const ZoneName", line: "const ZoneName = '{{ inventory_hostname }}'" }
    - { regexp: "^(\/\/)?const {{ plugins_custom_dir_var_name }} =(.+)", line: "const {{ plugins_custom_dir_var_name }} = '{{ plugins_dest_dir }}'" }

相关内容