Ansible 循环中有多个不同的命令
我正在尝试将yml文件转换为json。我需要从file1.txt中选择yml文件列表,并将所有这些文件转换为json 下面是我正在使用的代码Ansible 循环中有多个不同的命令,ansible,Ansible,我正在尝试将yml文件转换为json。我需要从file1.txt中选择yml文件列表,并将所有这些文件转换为json 下面是我正在使用的代码 - hosts: localhost tasks: - name: convert yml to json shell: cat /home/testadmin/{{ item }}.yml register: result - copy: dest: ./{{ item }}.json
- hosts: localhost
tasks:
- name: convert yml to json
shell: cat /home/testadmin/{{ item }}.yml
register: result
- copy:
dest: ./{{ item }}.json
content: "{{ result.stdout | from_yaml | to_nice_json }}"
with_lines: cat file1.txt
代码应该从file1.txt中提取文件名,然后将文件1转换为1。我想知道如何将所有这些命令转换成循环中的yml到json
实际结果应替换file1.txt中的所有.yml文件,并转换为同名的json格式循环仅对其所附加的任务有效。要将多个任务包装在一个循环中,需要将它们拆分到另一个文件中,使用include语句加载它们,然后将循环附加到该include语句 在你的情况下,这些都不是必需的。我认为这应该满足您的要求,假设file1.txt包含一个文件名列表,每行一个,没有文件扩展名:
- host: localhost
connection: local
tasks:
- name: Convert each file listed in file1.txt
copy:
dest: "./{{ item | trim }}.json"
content: "{{ lookup('file', item + '.yml') | from_yaml | to_nice_json }}"
with_lines: cat ./file1.txt
停止Ansible打开到本地主机的SSH连接connection:local
从列表中获取每个项并修剪任何前导或尾随空格{{item | trim}}
读取文件lookup('file',item+'.yml')
是循环中使用的默认变量名,用于包含循环中每个元素的内容item
仅在本地工作,因此如果需要远程运行此功能,则需要修改此功能with_line