Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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/2/linux/23.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
Arrays 如何将命令输出存储到Ansible中的数组中?_Arrays_Linux_Ansible_Ansible Facts - Fatal编程技术网

Arrays 如何将命令输出存储到Ansible中的数组中?

Arrays 如何将命令输出存储到Ansible中的数组中?,arrays,linux,ansible,ansible-facts,Arrays,Linux,Ansible,Ansible Facts,本质上,我希望能够使用ansible在Linux中处理“通配符文件名”。本质上,这意味着使用ls命令,将文件名的一部分后跟“*”,以便只列出某些文件 但是,我无法将输出正确存储在变量中,因为可能会返回多个文件名。因此,我希望能够存储这些结果,无论在一个任务中一个数组中可能有多少个。然后,我希望能够在以后的任务中从数组中检索所有结果。此外,由于我不知道可能返回多少个文件,所以我无法为每个文件名执行任务,数组更有意义 这背后的原因是,随机存储位置中的某些文件经常更改,但它们的前半部分总是相同的。他们

本质上,我希望能够使用ansible在Linux中处理“通配符文件名”。本质上,这意味着使用ls命令,将文件名的一部分后跟“*”,以便只列出某些文件

但是,我无法将输出正确存储在变量中,因为可能会返回多个文件名。因此,我希望能够存储这些结果,无论在一个任务中一个数组中可能有多少个。然后,我希望能够在以后的任务中从数组中检索所有结果。此外,由于我不知道可能返回多少个文件,所以我无法为每个文件名执行任务,数组更有意义

这背后的原因是,随机存储位置中的某些文件经常更改,但它们的前半部分总是相同的。他们的后半部分名字是随机的,我不想把它们硬编码成ansible

我完全不确定如何在ansible中正确实现/操作数组,因此下面的代码就是我“尝试”实现的一个示例。显然,如果返回多个文件名,它将无法正常工作,这就是为什么我在这个主题上寻求帮助:

- hosts: <randomservername>
  remote_user: remoteguy
  become: yes
  become_method: sudo
  vars:
    aaaa: b
  tasks:

 - name: Copy over all random file contents from directory on control node to target clients.  This is to show how to manipulate wildcard filenames.
    copy:
      src: /opt/home/remoteguy/copyable-files/testdir/
      dest: /tmp/
      owner: remoteguy
      mode: u=rwx,g=r,o=r
    ignore_errors: yes

  - name: Determine the current filenames and store in variable for later use, obviously for this exercise we know part of the filenames.
    shell: "ls {{item}}"
    changed_when: false
    register: annoying
    with_items: [/tmp/this-name-is-annoying*, /tmp/this-name-is-also*]

  - name: Run command to cat each file and then capture that output.
    shell: cat {{ annoying }}
    register: annoying_words

  - debug: msg=Here is the output of the two files.  {{annoying_words.stdout_lines }}

  - name: Now, remove the wildcard files from each server to clean up.
    file:
      path: '{{ item }}'
      state: absent
    with_items:
    - "{{ annoying.stdout }}"
-主机:
远程用户:remoteguy
变成:是的
方法:sudo
变量:
aaaa:b
任务:
-名称:将所有随机文件内容从控制节点上的目录复制到目标客户端。这是为了演示如何操作通配符文件名。
副本:
src:/opt/home/remoteguy/copyable files/testdir/
目的地:/tmp/
店主:remoteguy
模式:u=rwx,g=r,o=r
忽略错误:是
-name:确定当前文件名并存储在变量中以供以后使用,显然在本练习中我们知道部分文件名。
shell:“ls{{item}”
更改时间:false
注册:讨厌
带_项:[/tmp/这个名字很烦人*,/tmp/这个名字也是*]
-name:运行命令对每个文件进行cat,然后捕获该输出。
shell:cat{{{恼人的}
寄存器:恼人的单词
-debug:msg=这是两个文件的输出。{{恼人的单词.标准单词行}
-名称:现在,从每个服务器中删除通配符文件以进行清理。
文件:
路径:“{item}}”
国家:缺席
有以下项目:
-“{{恼人的.stdout}”
我知道YAML格式有点混乱,但是如果它被修复,这个“会”正常运行,它不会给我我想要的输出。因此,如果有50个文件,我希望ansible能够处理所有文件,和/或能够删除所有文件。。等等等等等等


如果这里有人能让我知道如何正确地利用上面测试代码片段中的数组,那将是非常棒的

Ansible将
shell
command
动作模块的输出存储在
stdout
stdout\u行
变量中。后者以列表的形式包含标准输出的单独行

要迭代元素,请使用:

with_items:
  - "{{ annoying.stdout_lines }}"
您应该记住,解析
ls
输出在某些情况下可能会导致问题。

您可以尝试以下方法

- name: Run command to cat each file and then capture that output.
   shell: cat {{ item.stdout_lines }}
   register: annoying_words
   with_items:
    - "{{ annoying.results }}"

恼人。标准行已经是一个列表

来自

返回stdout时,Ansible始终提供字符串列表,每行包含原始输出中的一项

要将列表分配给另一个变量,请执行以下操作:

    ..
      register: annoying
    - set_fact:
        varName: "{{annoying.stdout_lines}}"
    # print first element on the list
    - debug: msg="{{varName | first}}"

看起来你的问题最多可以缩短到五行。我建议使用最少的例子来解释这个问题,并且要比你的具体案例更多。techraf:我知道我可能会有点罗嗦,但在这种情况下,我认为信息越多越好。在将来,我将尝试看看是否可以将这种类型的文本爆炸减少一点。Welp,很高兴知道stdout_行在列表形式中使用单独的行。但是,在我的第三个游戏/任务中,如果我按照您建议的方式运行它,它将在运行期间失败。它给出的失败是声明dict值/arg未定义,即使我在ansible playbook-vvv期间在第二个/play/task期间看到stdout确实存在,并且我注册了一个变量以尝试捕获它。也许我在第三个重头戏/任务中的shell语句替换有问题?