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

查看:427
本文介绍了如何在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能够处理所有文件,和/或删除所有文件..etc等,等等.

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操作模块的输出存储在stdoutstdout_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天全站免登陆