发出传递变量,以在Ansible中包含添加主机的任务文件

发出传递变量,以在Ansible中包含添加主机的任务文件,ansible,parameter-passing,nested-loops,scoping,Ansible,Parameter Passing,Nested Loops,Scoping,我正在处理嵌套循环,以便使用add_host构建动态主机 Outer Loop: with_items: "{{ command_result.stdout_lines }}" // gets me the list of users Inner Loop: with_items: "{{ dest_ip.split(',') }}" // gets me the list of IP addresses seperated by comma (,) 下

我正在处理嵌套循环,以便使用add_host构建动态主机

Outer Loop:      with_items: "{{ command_result.stdout_lines }}"  // gets me the list of users
Inner Loop:      with_items: "{{ dest_ip.split(',') }}"          // gets me the list of IP addresses seperated by comma (,)
下面的剧本很好用

   - name: Add hosts
     include_tasks: "{{ playbook_dir }}/gethosts.yml"
     vars:
       dest_ip: "{{ item.split('\t')[0] }}"
       file_dets: "{{ item.split('\t')[1] }}"
       USER_pass: "anystring"
#       USER_pass:  "{% if item.split('\t')[3] == 'FrontEnd' %}user1{% elif item.split('\t')[3] == 'BackEnd' %}user2{% else %}{% endif %}"
       ansible_host: localhost
       install_dir_pass: "anystring"
#       install_dir_pass: "{{ item.split('\t')[2] }}"

     with_items: "{{ command_result.stdout_lines }}"
下面是我的include_任务gethost.yml文件:

---
 - name: Generate JSON data
    add_host:
      name: "{{ item }}"
      groups: dest_nodes
      ansible_user: "{{ USER_pass }}"
      install_dir: "{{ install_dir_pass }}"
    with_items: "{{ dest_ip.split(',') }}"
如果我取消注释USER\u pass或install\u dir\u pass并注释现有值,则会出现以下错误:

任务[生成JSON数据] ***********************************************************************************************************************************任务路径:/app/deployment/gethosts.yml:2[警告]:循环 变量“item”已在使用中。您应该设置
循环变量
值
在任务的
循环\u控制
选项中,执行其他要避免的操作 变量冲突和意外行为

致命:[localhost]:失败!=>{ “msg”:“该任务包含一个带有未定义变量的选项。错误是:列表对象没有元素2\n\n错误似乎是 在“/app/deployment/gethosts.yml”中:第2行第4列,但可能是\n 文件中的其他位置,具体取决于语法问题。\n\n 有问题的行似乎是:\n\n---\n-name:Generate JSON data\n
^此处\n“}

没有更多的主人了


重演 **************************************************************************************************************************************************localhost:ok=12 changed=1 unreachable=0
失败=1跳过=0解救=0忽略=0

要求解决这个问题,并解释我的几个问题

  • dest_ip是读取的,当install_dir_pass等其他变量似乎不起作用时,可以使用include_task get_hosts.yml文件中的.split(,)方法正常工作

  • 当给用户传递和安装目录传递一个简单的字符串“AnyString”时,它可以工作,并且在get_hosts.yml中可以很好地读取,就好像它们是使用item.split('\t')[]分配的值一样,剧本错误如上所述

  • 我已经使用debug测试了command_result中的所有条目都是良好的,并且应该正确填充这些值,如下所示

       - debug:
              msg: "{{  item.split('\t')[0]  }}"
             # msg: "{{  item.split('\t')[1]  }}"
              #msg: "{{  item.split('\t')[2]  }}"
           #   msg: "{{  item.split('\t')[3]  }}"
         with_items: "{{ command_result.stdout_lines }}"
    

    正如您的错误消息中所解释的那样:

    • 每个嵌套循环的
      之间存在名称冲突
    • 您必须在选项中设置
      循环\u var
      名称,以解决任一循环中的冲突
    我建议您在
    include_任务中执行此操作,这样就可以解决包含文件中任何其他循环的问题。由于我不知道您的
    命令\u结果。标准输出行
    确切包含什么,因此我在下面的示例中使用了var name
    我的结果
    ,您应该根据您的情况进行更改。请注意,如有必要,此var将直接在包含的文件中可用

       - name: Add hosts
         include_tasks: "{{ playbook_dir }}/gethosts.yml"
         vars:
           dest_ip: "{{ my_result.split('\t')[0] }}"
           file_dets: "{{ my_result.split('\t')[1] }}"
           USER_pass:  "{% if my_result.split('\t')[3] == 'FrontEnd' %}user1{% elif my_result.split('\t')[3] == 'BackEnd' %}user2{% else %}{% endif %}"
           ansible_host: localhost
           install_dir_pass: "{{ my_result.split('\t')[2] }}"
         with_items: "{{ command_result.stdout_lines }}"
         loop_control:
           loop_var: my_result