summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/lineinfile/tasks/test_string02.yml
blob: 1fa48b85131ba3b8a07a5e416d2b99c616f03ec1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
---
###################################################################
# 2nd search_string tests

- name: Deploy the teststring.conf file
  copy:
    src: teststring.conf
    dest: "{{ remote_tmp_dir }}/teststring.conf"
  register: result

- name: Assert that the teststring.conf file was deployed
  assert:
    that:
      - result is changed
      - result.checksum == '6037f13e419b132eb3fd20a89e60c6c87a6add38'
      - result.state == 'file'

# Test instertafter
- name: Insert lines after with string
  lineinfile:
    path: "{{ remote_tmp_dir }}/teststring.conf"
    search_string: "{{ item.regexp }}"
    line: "{{ item.line }}"
    insertafter: "{{ item.after }}"
  with_items: "{{ test_befaf_regexp }}"
  register: _multitest_5

- name: Do the same thing again and check for changes
  lineinfile:
    path: "{{ remote_tmp_dir }}/teststring.conf"
    search_string: "{{ item.regexp }}"
    line: "{{ item.line }}"
    insertafter: "{{ item.after }}"
  with_items: "{{ test_befaf_regexp }}"
  register: _multitest_6

- name: Assert that the file was changed the first time but not the second time
  assert:
    that:
      - item.0 is changed
      - item.1 is not changed
  with_together:
    - "{{ _multitest_5.results }}"
    - "{{ _multitest_6.results }}"

- name: Stat the file
  stat:
    path: "{{ remote_tmp_dir }}/teststring.conf"
  register: result

- name: Assert that the file contents match what is expected
  assert:
    that:
      - result.stat.checksum == '06e2c456e5028dd7bcd0b117b5927a1139458c82'

- name: Do the same thing a third time without string and check for changes
  lineinfile:
    path: "{{ remote_tmp_dir }}/teststring.conf"
    line: "{{ item.line }}"
    insertafter: "{{ item.after }}"
  with_items: "{{ test_befaf_regexp }}"
  register: _multitest_7

- name: Stat the file
  stat:
    path: "{{ remote_tmp_dir }}/teststring.conf"
  register: result

- name: Assert that the file was changed when no string was provided
  assert:
    that:
      - item is not changed
  with_items: "{{ _multitest_7.results }}"

- name: Stat the file
  stat:
    path: "{{ remote_tmp_dir }}/teststring.conf"
  register: result

- name: Assert that the file contents match what is expected
  assert:
    that:
      - result.stat.checksum == '06e2c456e5028dd7bcd0b117b5927a1139458c82'

# Test insertbefore
- name: Deploy the test.conf file
  copy:
    src: teststring.conf
    dest: "{{ remote_tmp_dir }}/teststring.conf"
  register: result

- name: Assert that the teststring.conf file was deployed
  assert:
    that:
      - result is changed
      - result.checksum == '6037f13e419b132eb3fd20a89e60c6c87a6add38'
      - result.state == 'file'

- name: Insert lines before with string
  lineinfile:
    path: "{{ remote_tmp_dir }}/teststring.conf"
    search_string: "{{ item.regexp }}"
    line: "{{ item.line }}"
    insertbefore: "{{ item.before }}"
  with_items: "{{ test_befaf_regexp }}"
  register: _multitest_8

- name: Do the same thing again and check for changes
  lineinfile:
    path: "{{ remote_tmp_dir }}/teststring.conf"
    search_string: "{{ item.regexp }}"
    line: "{{ item.line }}"
    insertbefore: "{{ item.before }}"
  with_items: "{{ test_befaf_regexp }}"
  register: _multitest_9

- name: Assert that the file was changed the first time but not the second time
  assert:
    that:
      - item.0 is changed
      - item.1 is not changed
  with_together:
    - "{{ _multitest_8.results }}"
    - "{{ _multitest_9.results }}"

- name: Stat the file
  stat:
    path: "{{ remote_tmp_dir }}/teststring.conf"
  register: result

- name: Assert that the file contents match what is expected
  assert:
    that:
      - result.stat.checksum == 'c3be9438a07c44d4c256cebfcdbca15a15b1db91'

- name: Do the same thing a third time without string and check for changes
  lineinfile:
    path: "{{ remote_tmp_dir }}/teststring.conf"
    line: "{{ item.line }}"
    insertbefore: "{{ item.before }}"
  with_items: "{{ test_befaf_regexp }}"
  register: _multitest_10

- name: Stat the file
  stat:
    path: "{{ remote_tmp_dir }}/teststring.conf"
  register: result

- name: Assert that the file was changed when no string was provided
  assert:
    that:
      - item is not changed
  with_items: "{{ _multitest_10.results }}"

- name: Stat the file
  stat:
    path: "{{ remote_tmp_dir }}/teststring.conf"
  register: result

- name: Assert that the file contents match what is expected
  assert:
    that:
      - result.stat.checksum == 'c3be9438a07c44d4c256cebfcdbca15a15b1db91'

# End of string tests
###################################################################