Ошибка с Ansible include_tasks при добавлении динамических хостов во вложенный цикл - PullRequest
0 голосов
/ 24 сентября 2019

Ниже приведена книга воспроизведения с 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 }}"

Можете ли вы предложить

1 Ответ

3 голосов
/ 24 сентября 2019

Возможно, вы забыли параметр vars, поэтому:

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