1--- 2################################################################### 3# 1st search_string tests 4 5- name: deploy the test file for lineinfile string 6 copy: 7 src: teststring.txt 8 dest: "{{ output_dir }}/teststring.txt" 9 register: result 10 11- name: assert that the test file was deployed 12 assert: 13 that: 14 - result is changed 15 - "result.checksum == '481c2b73fe062390afdd294063a4f8285d69ac85'" 16 - "result.state == 'file'" 17 18- name: insert a line at the beginning of the file, and back it up 19 lineinfile: 20 dest: "{{ output_dir }}/teststring.txt" 21 state: present 22 line: "New line at the beginning" 23 insertbefore: "BOF" 24 backup: yes 25 register: result1 26 27- name: insert a line at the beginning of the file again 28 lineinfile: 29 dest: "{{ output_dir }}/teststring.txt" 30 state: present 31 line: "New line at the beginning" 32 insertbefore: "BOF" 33 register: result2 34 35- name: Replace a line using string 36 lineinfile: 37 dest: "{{ output_dir }}/teststring.txt" 38 state: present 39 line: "Thi$ i^ [ine 3" 40 search_string: (\\w)(\\s+)([\\.,]) 41 register: backrefs_result1 42 43- name: Replace a line again using string 44 lineinfile: 45 dest: "{{ output_dir }}/teststring.txt" 46 state: present 47 line: "Thi$ i^ [ine 3" 48 search_string: (\\w)(\\s+)([\\.,]) 49 register: backrefs_result2 50 51- command: cat {{ output_dir }}/teststring.txt 52 53- name: assert that the line with backrefs was changed 54 assert: 55 that: 56 - backrefs_result1 is changed 57 - backrefs_result2 is not changed 58 - "backrefs_result1.msg == 'line replaced'" 59 60- name: stat the test after the backref line was replaced 61 stat: 62 path: "{{ output_dir }}/teststring.txt" 63 register: result 64 65- name: assert test checksum matches after backref line was replaced 66 assert: 67 that: 68 - "result.stat.checksum == '8084519b53e268920a46592a112297715951f167'" 69 70- name: remove the middle line using string 71 lineinfile: 72 dest: "{{ output_dir }}/teststring.txt" 73 state: absent 74 search_string: "Thi$ i^ [ine 3" 75 register: result 76 77- name: assert that the line was removed 78 assert: 79 that: 80 - result is changed 81 - "result.msg == '1 line(s) removed'" 82 83- name: stat the test after the middle line was removed 84 stat: 85 path: "{{ output_dir }}/teststring.txt" 86 register: result 87 88- name: assert test checksum matches after the middle line was removed 89 assert: 90 that: 91 - "result.stat.checksum == '89919ef2ef91e48ad02e0ca2bcb76dfc2a86d516'" 92 93- name: run a validation script that succeeds using string 94 lineinfile: 95 dest: "{{ output_dir }}/teststring.txt" 96 state: absent 97 search_string: <FilesMatch ".py[45]?$"> 98 validate: "true %s" 99 register: result 100 101- name: assert that the file validated after removing a line 102 assert: 103 that: 104 - result is changed 105 - "result.msg == '1 line(s) removed'" 106 107- name: stat the test after the validation succeeded 108 stat: 109 path: "{{ output_dir }}/teststring.txt" 110 register: result 111 112- name: assert test checksum matches after the validation succeeded 113 assert: 114 that: 115 - "result.stat.checksum == 'ba9600b34febbc88bfb3ca99cd6b57f1010c19a4'" 116 117- name: run a validation script that fails using string 118 lineinfile: 119 dest: "{{ output_dir }}/teststring.txt" 120 state: absent 121 search_string: "This is line 1" 122 validate: "/bin/false %s" 123 register: result 124 ignore_errors: yes 125 126- name: assert that the validate failed 127 assert: 128 that: 129 - "result.failed == true" 130 131- name: stat the test after the validation failed 132 stat: 133 path: "{{ output_dir }}/teststring.txt" 134 register: result 135 136- name: assert test checksum matches the previous after the validation failed 137 assert: 138 that: 139 - "result.stat.checksum == 'ba9600b34febbc88bfb3ca99cd6b57f1010c19a4'" 140 141# End of string tests 142################################################################### 143