如何在 Ansible 中将命令输出存储到数组中? [英] How to store command output into array in Ansible?

查看:47
本文介绍了如何在 Ansible 中将命令输出存储到数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本质上,我希望能够使用 ansible 在 Linux 中处理通配符文件名".本质上,这意味着使用带有部分文件名后跟*"的 ls 命令,以便它仅列出某些文件.

Essentially, I want to be able to handle "wildcard filenames" in Linux using ansible. In essence, this means using the ls command with part of a filename followed by an "*" so that it will list ONLY certain files.

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

However, I cannot store the output properly in a variable as there will likely be more than one filename returned. Thus, I want to be able to store these results no matter how many there might be in an array during one task. I then want to be able to retrieve all of the results from the array in a later task. Furthermore, since I don't know how many files might be returned, I cannot do a task for each filename, and an array makes more sense.

这背后的原因是随机存储位置中的文件经常更改,但它们始终具有相同的前半部分.他们名字的后半部分是随机的,我根本不想把它硬编码成 ansible.

The reason behind this is that there are files in a random storage location that are changed often, but they always have the same first half. It's their second half of their names that are random, and I don't want to have to hard code that into ansible at all.

我完全不确定如何在 ansible 中正确实现/操作数组,因此以下代码是我尝试"完成的示例.显然,如果返回多个文件名,它将无法按预期运行,这就是我寻求有关此主题的帮助的原因:

I'm not certain at all how to properly implement/manipulate an array in ansible, so the following code is an example of what I'm "trying" to accomplish. Obviously it won't function as intended if more than one filename is returned, which is why I was asking for assistance on this topic:

- 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 }}"

我知道 YAML 格式有点混乱,但如果它被修复,这个会"正常运行,它不会给我我正在寻找的输出.因此,如果有 50 个文件,我希望 ansible 能够对它们进行操作,和/或能够将它们全部删除......等等等等.

I understand the YAML format got a little mussed up, but if it's fixed, this "would" run normally, it just won't give me the output I'm looking for. Thus if there were 50 files, I'd want ansible to be able to manipulate them all, and/or be able to delete them all.. etc etc etc.

如果这里有人能告诉我如何正确使用上述测试代码片段中的数组,那就太棒了!

If anyone here could let me know how to properly utilize an array in the above test code fragment that would be fantastic!

推荐答案

Ansible 将 shellcommand 动作模块的输出存储在 stdout 中和 stdout_lines 变量.后者以列表的形式包含标准输出的单独行.

Ansible stores the output of shell and command action modules in stdout and stdout_lines variables. The latter contains separate lines of the standard output in a form of a list.

要遍历元素,请使用:

with_items:
  - "{{ annoying.stdout_lines }}"

您应该记住,在某些情况下解析 ls 输出可能会导致问题.

You should remember that parsing ls output might cause problems in some cases.

这篇关于如何在 Ansible 中将命令输出存储到数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆