Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 如何设置Ansible变量的值,当条件与_itema循环在一起时_Loops_Variables_If Statement_Ansible_Conditional Statements - Fatal编程技术网

Loops 如何设置Ansible变量的值,当条件与_itema循环在一起时

Loops 如何设置Ansible变量的值,当条件与_itema循环在一起时,loops,variables,if-statement,ansible,conditional-statements,Loops,Variables,If Statement,Ansible,Conditional Statements,我有一个“with_item”循环,其中需要根据条件检查为变量“USER”赋值 如果“{command_result.stdout_lines}}”为“Frontend”,则用户变量应获取值“user1”,否则应为其赋值“user2” 下面是我在寻求帮助后可以实现的一些功能,但问题是即使满足“user1”的条件,“USER”始终会被分配“user2”值 我的剧本: - debug: msg: "User was {{ item.split('\t')[3] }}" w

我有一个“with_item”循环,其中需要根据条件检查为变量“USER”赋值

如果“{command_result.stdout_lines}}”为“Frontend”,则用户变量应获取值“user1”,否则应为其赋值“user2”

下面是我在寻求帮助后可以实现的一些功能,但问题是即使满足“user1”的条件,“USER”始终会被分配“user2”值

我的剧本:

  - debug:
       msg: "User was {{ item.split('\t')[3] }}"
     with_items: "{{ command_result.stdout_lines }}"

   - set_fact:
        USER:  "user1"
     when: item.split('\t')[3] == "FrontEnd"
     with_items: "{{ command_result.stdout_lines }}"

   - set_fact:
        USER:  "user2"
     when: item.split('\t')[3] == "BackEnd"
     with_items: "{{ command_result.stdout_lines }}"

   - debug:
          msg: "User has {{ USER }}"
     with_items: "{{ command_result.stdout_lines }}"
第一次调试打印并确认 项目.拆分('\t')[3]

第二个调试打印“USER”,但正如您在下面的输出中所看到的,它的值为“user2”,即使该值是Frontend

你能推荐一下吗

任务[调试] ************************************************************************************************************************************************确定:[localhost]=>(项=10.12.1.13 10.12.1.13\n-rw-rw-r--user1 2019-09-13 15:39/was//testingjsp/testingcom.jsp 1786385840 /was//testingjsp前端)=>{ “msg”:“用户是前端”}确定:[localhost]=>(item=10.12.1.13 10.12.1.13\n-rw-rw-r--user2 2019-09-13 15:29/fin/scripts/testingscr.scr 367595418\n-rw-rw-r--user2 2019-09-13 15:36/fin/mrt/testingmrt.mrt 1251350031\n-rw-rw-r——用户2 2019-09-13 15:37/fin/exe/testingexe.exe 1390265645\n-rw-rw-r--user2 2019-09-13 15:38/fin/com/testingcom.com 90193476
/fin/scripts\n/fin/mrt\n/fin/exe\n/fin/com后端)=>{ “msg”:“用户是后端”}

任务[调试] ************************************************************************************************************************************************确定:[localhost]=>(项=10.12.1.13 10.12.1.13\n-rw-rw-r--user1 2019-09-13 15:39/was//testingjsp/testingcom.jsp 1786385840 /was//testingjsp前端)=>{ “msg”:“User has user2”}确定:[localhost]=>(item=10.12.1.13 10.12.1.13\n-rw-rw-r--user2 2019-09-13 15:29/fin/scripts/testingscr.scr 367595418\n-rw-rw-r--user2 2019-09-13 15:36/fin/mrt/testingmrt.mrt 1251350031\n-rw-rw-r——用户2 2019-09-13 15:37/fin/exe/testingexe.exe 1390265645\n-rw-rw-r--user2 2019-09-13 15:38/fin/com/testingcom.com 90193476
/fin/scripts\n/fin/mrt\n/fin/exe\n/fin/com后端)=>{ “msg”:“User has user2”}


以下是任务,这些任务将为
命令\u结果
中的每个项目提供
用户

- set_fact:
    USER:  "{% if item.split('\t')[2] == 'FrontEnd' %}user1{% elif item.split('\t')[2] == 'BackEnd' %}user2{% else %}{% endif %}"
  with_items: "{{ command_result.stdout_lines }}"
  register: "facts"

- debug:
    msg: "{{ item.ansible_facts.USER }}"
  with_items: "{{ facts.results }}"
第一个任务根据
item.split('\t')[3]
提取的值设置
USER
的值,并使用jinja2模板设置
USER
的值

如果您的
命令\u结果中有
n项
,则第一个任务将是注册
用户
事实
n次
。因此,我在
facts
命名变量中注册了该值

在第二个任务中访问并打印这些值

类似地,您可以在后续任务中使用第二个任务中显示的值


希望有帮助。

命令\u结果.stdout\u行中有3项
,最后一项是将
后端
作为
项的值。拆分('\t')[3]
。因此,事实
USER
中的最终值是
user2
。所以,剧本做得非常好。请添加您的最终目标是什么?我想在循环的每个迭代中打印USER的值,因此我希望看到user1和user2都根据匹配条件打印在循环中。用户的值最终将提供给Ansible的添加主机模块。请检查我的答案。另外,我们在命令\u result.stdout\u行和项中有4项。split('\t')[3]指的是第四项,它在第一次迭代中是前端,在第二次迭代(即循环的最后一次迭代)中是后端。好的。首先,它适用于
n项
。我可以分配一个空字符串,以防它与任何大小写都不匹配,然后您可以在后续任务中使用值时筛选出
用户:'
所在的条目。我对此进行了测试,效果良好。然而,我面临着将每个“用户”传递到一个包含任务文件的挑战,该文件将构建“添加”主机组。@Ashar,任务中注册的变量可以在另一个剧本中使用
hostvars['host\u on_上执行任务的主机]['facts']