我想通过ansible读取一个文件,找到特定的东西,并将它们全部存储在本地主机的文件中,例如,所有主机中都有/tmp/test文件,我想在这个文件中存储特定的东西,并将它们全部存储在我的家里。
我该怎么办?
发布于 2020-08-19 18:57:12
要做到这一点,可能有很多方法。Ansible模块(甚至是工具)的选择可能会有所不同。
一种方法是(仅使用Ansible):
示例:
- hosts: remote_host
tasks:
# Slurp the file
- name: Get contents of file
slurp:
src: /tmp/test
register: testfile
# Filter the contents to new file
- name: Save contents to a variable for looping
set_fact:
testfile_contents: "{{ testfile.content | b64decode }}"
- name: Write a filtered file
lineinfile:
path: /tmp/filtered_test
line: "{{ item }}"
create: yes
when: "'TEXT_YOU_WANT' in item"
with_items: "{{ testfile_contents.split('\n') }}"
# Fetch the file
- name: Fetch the filtered file
fetch:
src: /tmp/filtered_test
dest: /tmp/这将把文件取到/tmp/<ANSIBLE_HOSTNAME>/tmp/filtered_test。
发布于 2020-08-19 16:34:58
您可以使用Ansible fetch module将文件从远程系统下载到本地系统。然后,您可以在本地进行处理,如以下Ansible cli示例所示:
REMOTE=[YOUR_REMOTE_SERVER]; \
ansible -m fetch -a "src=/tmp/test dest=/tmp/ansiblefetch/" $REMOTE && \
grep "[WHAT_YOU_ARE_INTERESTED_IN]" /tmp/ansiblefetch/$REMOTE/tmp/test > /home/user/ansible_files/$REMOTE这段代码运行Ansible的临时版本,使用源文件夹(在远程上)和目标文件夹(在本地)作为参数调用模块fetch。Fetch将文件复制到文件夹[SRC]/[REMOTE_NAME]/[DEST]中,然后从该文件夹中提取我们感兴趣的内容,并将其输出到/home/user/ansible_files/$REMOTE中。
https://stackoverflow.com/questions/63482074
复制相似问题