1---
2###################################################################
3# 2nd search_string tests
4
5- name: Deploy the teststring.conf file
6  copy:
7    src: teststring.conf
8    dest: "{{ output_dir }}/teststring.conf"
9  register: result
10
11- name: Assert that the teststring.conf file was deployed
12  assert:
13    that:
14      - result is changed
15      - result.checksum == '6037f13e419b132eb3fd20a89e60c6c87a6add38'
16      - result.state == 'file'
17
18# Test instertafter
19- name: Insert lines after with string
20  lineinfile:
21    path: "{{ output_dir }}/teststring.conf"
22    search_string: "{{ item.regexp }}"
23    line: "{{ item.line }}"
24    insertafter: "{{ item.after }}"
25  with_items: "{{ test_befaf_regexp }}"
26  register: _multitest_5
27
28- name: Do the same thing again and check for changes
29  lineinfile:
30    path: "{{ output_dir }}/teststring.conf"
31    search_string: "{{ item.regexp }}"
32    line: "{{ item.line }}"
33    insertafter: "{{ item.after }}"
34  with_items: "{{ test_befaf_regexp }}"
35  register: _multitest_6
36
37- name: Assert that the file was changed the first time but not the second time
38  assert:
39    that:
40      - item.0 is changed
41      - item.1 is not changed
42  with_together:
43    - "{{ _multitest_5.results }}"
44    - "{{ _multitest_6.results }}"
45
46- name: Stat the file
47  stat:
48    path: "{{ output_dir }}/teststring.conf"
49  register: result
50
51- name: Assert that the file contents match what is expected
52  assert:
53    that:
54      - result.stat.checksum == '06e2c456e5028dd7bcd0b117b5927a1139458c82'
55
56- name: Do the same thing a third time without string and check for changes
57  lineinfile:
58    path: "{{ output_dir }}/teststring.conf"
59    line: "{{ item.line }}"
60    insertafter: "{{ item.after }}"
61  with_items: "{{ test_befaf_regexp }}"
62  register: _multitest_7
63
64- name: Stat the file
65  stat:
66    path: "{{ output_dir }}/teststring.conf"
67  register: result
68
69- name: Assert that the file was changed when no string was provided
70  assert:
71    that:
72      - item is not changed
73  with_items: "{{ _multitest_7.results }}"
74
75- name: Stat the file
76  stat:
77    path: "{{ output_dir }}/teststring.conf"
78  register: result
79
80- name: Assert that the file contents match what is expected
81  assert:
82    that:
83      - result.stat.checksum == '06e2c456e5028dd7bcd0b117b5927a1139458c82'
84
85# Test insertbefore
86- name: Deploy the test.conf file
87  copy:
88    src: teststring.conf
89    dest: "{{ output_dir }}/teststring.conf"
90  register: result
91
92- name: Assert that the teststring.conf file was deployed
93  assert:
94    that:
95      - result is changed
96      - result.checksum == '6037f13e419b132eb3fd20a89e60c6c87a6add38'
97      - result.state == 'file'
98
99- name: Insert lines before with string
100  lineinfile:
101    path: "{{ output_dir }}/teststring.conf"
102    search_string: "{{ item.regexp }}"
103    line: "{{ item.line }}"
104    insertbefore: "{{ item.before }}"
105  with_items: "{{ test_befaf_regexp }}"
106  register: _multitest_8
107
108- name: Do the same thing again and check for changes
109  lineinfile:
110    path: "{{ output_dir }}/teststring.conf"
111    search_string: "{{ item.regexp }}"
112    line: "{{ item.line }}"
113    insertbefore: "{{ item.before }}"
114  with_items: "{{ test_befaf_regexp }}"
115  register: _multitest_9
116
117- name: Assert that the file was changed the first time but not the second time
118  assert:
119    that:
120      - item.0 is changed
121      - item.1 is not changed
122  with_together:
123    - "{{ _multitest_8.results }}"
124    - "{{ _multitest_9.results }}"
125
126- name: Stat the file
127  stat:
128    path: "{{ output_dir }}/teststring.conf"
129  register: result
130
131- name: Assert that the file contents match what is expected
132  assert:
133    that:
134      - result.stat.checksum == 'c3be9438a07c44d4c256cebfcdbca15a15b1db91'
135
136- name: Do the same thing a third time without string and check for changes
137  lineinfile:
138    path: "{{ output_dir }}/teststring.conf"
139    line: "{{ item.line }}"
140    insertbefore: "{{ item.before }}"
141  with_items: "{{ test_befaf_regexp }}"
142  register: _multitest_10
143
144- name: Stat the file
145  stat:
146    path: "{{ output_dir }}/teststring.conf"
147  register: result
148
149- name: Assert that the file was changed when no string was provided
150  assert:
151    that:
152      - item is not changed
153  with_items: "{{ _multitest_10.results }}"
154
155- name: Stat the file
156  stat:
157    path: "{{ output_dir }}/teststring.conf"
158  register: result
159
160- name: Assert that the file contents match what is expected
161  assert:
162    that:
163      - result.stat.checksum == 'c3be9438a07c44d4c256cebfcdbca15a15b1db91'
164
165# End of string tests
166###################################################################
167