2017-09-11 39 views
0

我想將文件中的變量從文件讀入到某個數組中,但沒有寫入預先定義的變量。ansible從文件中的一行讀取(多個)值

目標是一個劇本,它搜索item.0和NOT item.1的行,並刪除它們,稍後劇本可以確定,存在item.0 item.1的行,這就是爲什麼我需要這分裂了。

例如我有這樣行的文件:

Parameter Value 
Parameter Value 
Parameter Value 

在循環中使用本直至EOF。

例如部分OT了劇本:

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

有誰知道查找和循環的解決方案?

致以問候

回答

0

這應該對您有所幫助。

輸入文件

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 

這不是最先進的,但應該工作。

+0

謝謝你,但是外殼搜索目標主機上的文件,我需要那個文件,其中的劇本是 – Andre

+0

ansible-playbook -i hosts -c local playbook.yml – itiic