2016-09-19 2 views
0

lineinfileを使用して複数の行を追加または編集しようとしていますが、動作しません。 ansible: lineinfile for several lines?可能なfileinlineがループで動作しない

# vim /etc/ansible/playbook/test-play.yml 

- hosts: tst.wizvision.com 
    tasks: 
    - name: change of line 
    lineinfile: 
     dest: /root/test.txt 
     regexp: "{{ item.regexp }}" 
     line: "{{ item.line }}" 
     backrefs: yes 
     with_items: 
     - { regexp: '^# line one', line: 'NEW LINE ONE' } 
     - { regexp: '^# line two', line: 'NEW LINE TWO' } 

Ansibleエラー:ノー運REFとコードの下に使用しています

# ansible-playbook test-2.yml 

TASK [ラインの変更] ************** *******************************

fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'item' is undefined\n\nThe error appears to have been in '/etc/ansible/playbook/test-2.yml': line 3, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: change of line\n^here\n"}

答えて

2

with_itemsは、タスク内で正しくインデントされていません。

with_itemsは、モジュール自体のパラメータではなく、モジュールのレベルにある必要があります。あなたの場合、with_itemsをパラメータとしてlineinfileモジュールに渡しています。lineinfileモジュールのパラメータがwith_itemsであると不平を言います。

あなたの仕事は次のようになります -

tasks: 
- name: change of line 
    lineinfile: 
    dest: /root/test.txt 
    regexp: "{{ item.regexp }}" 
    line: "{{ item.line }}" 
    backrefs: yes 
    with_items: 
    - { regexp: '^# line one', line: 'NEW LINE ONE' } 
    - { regexp: '^# line two', line: 'NEW LINE TWO' } 
+0

おかげで多くのことを。それは今働く。それは私の時間の多くを無駄にした.... –

+0

問題はない、それはあなたのために働いてうれしい! :) – rk2

関連する問題