【问题标题】:Error with Ansible include_tasks while adding dynamic hosts in nested loop在嵌套循环中添加动态主机时 Ansible include_tasks 出错
【发布时间】:2019-09-24 11:57:37
【问题描述】:

下面是带有 import_tasks get_hosts.yml 的剧本,用于在嵌套循环中构建动态主机。但是,我在运行 playbook 时遇到语法错误。

{{ item.split('\t')[0] }} 的 IP 地址由逗号 , 分隔,然后是一个由 /t 分隔的字符串

---

- name: "Play 1"
  hosts: localhost
  tasks:
   - name: "Search database"
     command: >       mysql --user=root --password=p@ssword deployment
       --host=localhost  -Ns -e "SELECT dest_ip,file_dets FROM deploy_dets"
     register: command_result

   - name: Add hosts
     include_tasks: "{{ playbook_dir }}/gethosts.yml"
       dest_ip: "{{ item.split('\t')[0] }}"
       groups: dest_nodes
       file_dets: "{{ item.split('\t')[1] }}"
       ansible_host: localhost
       ansible_connection: local
     with_items: "{{ command_result.stdout_lines }}"

下面是我的 get_hosts.yml 文件

 add_host:
   name: "{{ item }}" 
 with_items: "{{ dest_ip.split(',') }}"

输出:

$ ansible-playbook testinclude.yml

ERROR! Syntax Error while loading YAML.   did not find expected key

The error appears to be in '/app/deployment/testinclude.yml': line 23, column 8, but may be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

     include_tasks: "{{ playbook_dir }}/gethosts.yml"
       dest_ip: "{{ item.split('\t')[0] }}"
       ^ here We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when they start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

你能推荐一下吗

【问题讨论】:

  • 什么是dest_ip?它是一个变量吗?

标签: loops ansible nested-loops


【解决方案1】:

也许你忘记了vars参数,所以:

include_tasks: "{{ playbook_dir }}/gethosts.yml"
vars:  #  <------------------------------------------- HERE
    dest_ip: "{{ item.split('\t')[0] }}"
    groups: dest_nodes

【讨论】:

    猜你喜欢
    • 2015-05-24
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    相关资源
    最近更新 更多