Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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
Python 将文件的输出与ansible when语句中的字符串/列表或任何内容进行比较_Python_Ansible - Fatal编程技术网

Python 将文件的输出与ansible when语句中的字符串/列表或任何内容进行比较

Python 将文件的输出与ansible when语句中的字符串/列表或任何内容进行比较,python,ansible,Python,Ansible,假设文件“/etc/my_file.txt”的内容如下: a b CC d a c BB d a d DD e The error was: template error while templating string: unexpected char u'"' 我正在检查它并将输出写入变量: - name: Check 3rd column of file shell: cat /etc/my_file.txt | awk '{print $3}' register: t

假设文件“/etc/my_file.txt”的内容如下:

a b CC d
a c BB d
a d DD e
The error was: template error while templating string: unexpected char u'"'
我正在检查它并将输出写入变量:

- name: Check 3rd column of file
  shell: cat /etc/my_file.txt | awk '{print $3}'
  register: test
是否有办法检查该文件的第3列的值是否与下面调试部分中给出的值完全相同?

我已尝试将其转换为列表:

- set_fact:
   std_out: "{{ test.stdout.split()|list   }}"
。。。然后将其与特定列表项进行比较:

- name: Test if it worked
  debug:
     msg: Values are differrent
  when: std_out[0] == "CC" and std_out[1] == "BB" and std_out[2] == "DD"
但是有这样的错误:

a b CC d
a c BB d
a d DD e
The error was: template error while templating string: unexpected char u'"'
尝试了更多的东西,但仍然有一些问题,感觉像撞在墙上:-)任何帮助都会被感谢

我的设置是:

ansible 2.9.14
python version = 2.7.5

使用注册值,例如

-名称:检查文件的第三列
shell:cat my_file.txt | awk'{print$3}'
注册:测试
给予

test.stdout\u行:
-抄送
-BB
-DD
您可以比较这些列表,例如:

-调试:
msg:值不同
时间:test.stdout\u line!=['CC','BB','DD']
-调试:
msg:值不存在差异
当:test.stdout_line==['CC','BB','DD']
给予

任务[调试]*******************************************************************
正在跳过:[localhost]
任务[调试]*******************************************************************
确定:[本地主机]=>
msg:值没有不同

在set-fact任务=>
std_-out:{{test.stdout.split()| list}}}
中缺少一个结束双引号,而您的
when
子句也缺少一个双引号=>
when:std_-out[0]=“CC”和std_-out[1]=“BB”和std_-out[2]=“DD”
注意:我绝对不确定这是否真的能解决您的逻辑问题……这是正确的,但当然不是真正的情况。我只是重新编写代码,犯了那个愚蠢的错误。很抱歉,谢谢你,很有效!