ansible 从文件中的一行读取(多个)值

ansible read (more than one) values from a line in file

我正在尝试将文件中的变量读入某个数组,而不是预定义的变量。

目标是一个剧本,它搜索带有 item.0 而不是 item.1 的行并删除它们,稍后剧本确保存在带有 item.0 item.1 的行,这就是为什么我需要拆分。

例如,我有一个包含如下行的文件:

Parameter Value
Parameter Value
Parameter Value

在循环中使用它直到 EOF。

剧本的示例部分:

- name: lineinfile loop
  lineinfile:
    path: /myfile
    regexp '^{{somethinglike item.0}}.(?!{{something like item.1}})'
    state absent
    ...

有人知道查找和循环的解决方案吗?

此致

这应该对你有帮助。

输入文件

cat filein 
Parameter1,Value1
Parameter2,Value2
Parameter3,Value3

剧本:

---
- hosts: test

  tasks:


  - name: reg
    shell: cat filein
    register: myitems


  - name: deb
    debug: var=myitems.stdout_lines


  - name: echo to file
    shell: "echo {{ item.split(',')[1] }} - {{ item.split(',')[0] }}  >> fileout"
    with_items: "{{ myitems.stdout_lines }}"

输出文件:

cat fileout 
Value1 - Parameter1
Value2 - Parameter2
Value3 - Parameter3

这不是最先进的,但应该可行。

解决方法:

ansible galaxy中有一个ansible角色,它提供了我所需要的:

https://github.com/mkouhei/ansible-role-includecsv