Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.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
Bash ansible-if语句并重定向到具有shell模块的文件';行不通_Bash_Ansible - Fatal编程技术网

Bash ansible-if语句并重定向到具有shell模块的文件';行不通

Bash ansible-if语句并重定向到具有shell模块的文件';行不通,bash,ansible,Bash,Ansible,我理解尽可能多地使用ansible模块是最好的,但是出于一个很好的原因,我被迫使用shell模块 我有一个日期列表文件,其中包含日期列表: 20170811 20170802 20170812 and so on.. 我需要将此日期与外壳模块的可用时间进行比较: - name: Read file date and compare with server date and redirect to a file shell: | if [ {{ item.split('\n')[0]

我理解尽可能多地使用ansible模块是最好的,但是出于一个很好的原因,我被迫使用shell模块

我有一个日期列表文件,其中包含日期列表:

20170811
20170802
20170812
and so on..
我需要将此日期与外壳模块的可用时间进行比较:

- name: Read file date and compare with server date and redirect to a file
  shell: |
    if [ {{ item.split('\n')[0] }} -lt ${{ gv_remote_date.stdout }} ]; then 
        echo {{ item.split('\n')[0] }} >> final_output
    fi
   args:
     executable: /bin/bash
   with_lines: "{{ date_list.stdout_lines }}"
我一点输出都没有。
在调试中:我可以看到它在切换项目,但在最终输出文件中我什么也没有得到。

为什么不使用本机
语句:

- name: Read file date and compare with server date and redirect to a file
  shell: echo {{ item }} >> final_output
  args:
    executable: /bin/bash
  when: item | int < gv_remote_date.stdout | int
  with_items: "{{ date_list.stdout_lines }}"
-name:读取文件日期并与服务器日期进行比较,然后重定向到文件
shell:echo{{item}}>>最终输出
args:
可执行文件:/bin/bash
时间:item | int
或者甚至像这样(使其幂等):

-lineinfle:
dest:最终输出
行:“{{item}}”
声明:“{(项目| int

这将确保该行在文件中,如果条件为false,则该行不在文件中。

语句:

- name: Read file date and compare with server date and redirect to a file
  shell: echo {{ item }} >> final_output
  args:
    executable: /bin/bash
  when: item | int < gv_remote_date.stdout | int
  with_items: "{{ date_list.stdout_lines }}"
-name:读取文件日期并与服务器日期进行比较,然后重定向到文件
shell:echo{{item}}>>最终输出
args:
可执行文件:/bin/bash
时间:item | int
或者甚至像这样(使其幂等):

-lineinfle:
dest:最终输出
行:“{{item}}”
声明:“{(项目| int

如果条件为false,这将确保该行在文件中,如果条件为false,则确保该行不存在。

我将使用Ansible的模板模块在文件中创建shell脚本。通过这种方式,您可以检查生成的shell脚本并在Ansible外部进行调试。

我将使用Ansible的模板模块在文件中创建shell脚本。通过这种方式,您可以检查生成的shell脚本,并在Ansible之外对其进行调试。

首先:您在哪里搜索文件,为什么不使用绝对路径?为了便于阅读本文,绝对路径被省略,我通过“cat日期列表”从文件中读取首先:你在哪里搜索文件,为什么不使用绝对路径?为了便于阅读,绝对路径被省略了,我通过“cat日期列表”从文件中读取