summaryrefslogtreecommitdiff
path: root/test/integration/targets/ini_file/tasks/main.yml
blob: c54b905ab2c852bb1ed9bbace1607d24cb0a4829 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
---
# test code for ini_file plugins
# (c) 2017 Red Hat Inc.

# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

- name: record the output directory
  set_fact: output_file={{ remote_tmp_dir }}/foo.ini

- name: add "fav=lemonade" is in section "[drinks]" in specified file
  ini_file:
    path: "{{ output_file }}"
    section: drinks
    option: fav
    value: lemonade
  register: result1

- name: verify ini_file 'changed' is true
  assert:
    that:
      - result1.changed == True
      - result1.msg == 'section and option added'

- name: read content from output file
  slurp:
    src: "{{ output_file }}"
  register: output_content

- name: set expected content and get current ini file content
  set_fact:
    expected1: |

      [drinks]
      fav = lemonade
    content1: "{{ output_content.content | b64decode }}"

- name: Verify content of ini file is as expected
  assert:
    that:
      - content1 == expected1

- name: add "fav=lemonade" is in section "[drinks]" again
  ini_file:
    path: "{{ output_file }}"
    section: drinks
    option: fav
    value: lemonade
  register: result2

- name: Ensure unchanged
  assert:
    that:
      - result2.changed == False
      - result2.msg == 'OK'

- name: Ensure "beverage=coke" is in section "[drinks]"
  ini_file:
    path: "{{ output_file }}"
    section: drinks
    option: beverage
    value: coke
  register: result3

- name: read content from output file
  slurp:
    src: "{{ output_file }}"
  register: output_content

- name: set expected content and get current ini file content
  set_fact:
    expected3: |

      [drinks]
      fav = lemonade
      beverage = coke
    content3: "{{ output_content.content | b64decode }}"

- name: assert 'changed' is true and content is OK
  assert:
    that:
      - result3.changed == True
      - result3.msg == 'option added'
      - content3 == expected3

- name: Remove option "beverage=coke"
  ini_file:
    path: "{{ output_file }}"
    section: drinks
    option: beverage
    state: absent
  register: result4

- name: read content from output file
  slurp:
    src: "{{ output_file }}"
  register: output_content

- name: get ini file content
  set_fact:
    content4: "{{ output_content.content | b64decode }}"

- name: assert changed and content is as expected
  assert:
    that:
      - result4.changed == True
      - result4.msg == 'option changed'
      - content4 == expected1

- name: remove section 'drinks'
  ini_file:
    path: "{{ output_file }}"
    section: drinks
    state: absent
  register: result5

- name: read content from output file
  slurp:
    src: "{{ output_file }}"
  register: output_content

- name: get current ini file content
  set_fact:
    content5: "{{ output_content.content | b64decode }}"

- name: assert changed and content is empty
  assert:
    that:
      - result5.changed == True
      - result5.msg == 'section removed'
      - content5 == "\n"

# allow_no_value

- name: test allow_no_value
  ini_file:
    path: "{{ output_file }}"
    section: mysqld
    option: skip-name
    allow_no_value: yes
  register: result6

- name: assert section and option added
  assert:
    that:
      - result6.changed == True
      - result6.msg == 'section and option added'

- name: test allow_no_value idempotency
  ini_file:
    path: "{{ output_file }}"
    section: mysqld
    option: skip-name
    allow_no_value: yes
  register: result6

- name: assert 'changed' false
  assert:
    that:
      - result6.changed == False
      - result6.msg == 'OK'

- name: test allow_no_value with loop
  ini_file:
    path: "{{ output_file }}"
    section: mysqld
    option: "{{ item.o }}"
    value: "{{ item.v }}"
    allow_no_value: yes
  with_items:
    - { o: "skip-name-resolve", v: null }
    - { o: "max_connections", v: "500" }

- name: read content from output file
  slurp:
    src: "{{ output_file }}"
  register: output_content

- name: set expected content and get current ini file content
  set_fact:
    content7: "{{ output_content.content | b64decode }}"
    expected7: |

      [mysqld]
      skip-name
      skip-name-resolve
      max_connections = 500

- name: Verify content of ini file is as expected
  assert:
    that:
      - content7 == expected7

- name: change option with no value to option with value
  ini_file:
    path: "{{ output_file }}"
    section: mysqld
    option: skip-name
    value: myvalue
  register: result8

- name: read content from output file
  slurp:
    src: "{{ output_file }}"
  register: output_content

- name: set expected content and get current ini file content
  set_fact:
    content8: "{{ output_content.content | b64decode }}"
    expected8: |

      [mysqld]
      skip-name = myvalue
      skip-name-resolve
      max_connections = 500

- name: assert 'changed' and msg 'option changed' and content is as expected
  assert:
    that:
      - result8.changed == True
      - result8.msg == 'option changed'
      - content8 == expected8

- name: change option with value to option with no value
  ini_file:
    path: "{{ output_file }}"
    section: mysqld
    option: skip-name
    allow_no_value: yes
  register: result9

- name: read content from output file
  slurp:
    src: "{{ output_file }}"
  register: output_content

- name: set expected content and get current ini file content
  set_fact:
    content9: "{{ output_content.content | b64decode }}"
    expected9: |

      [mysqld]
      skip-name
      skip-name-resolve
      max_connections = 500

- name: assert 'changed' and msg 'option changed' and content is as expected
  assert:
    that:
      - result9.changed == True
      - result9.msg == 'option changed'
      - content9 == expected9

- name: Remove option with no value
  ini_file:
    path: "{{ output_file }}"
    section: mysqld
    option: skip-name-resolve
    state: absent
  register: result10

- name: read content from output file
  slurp:
    src: "{{ output_file }}"
  register: output_content

- name: set expected content and get current ini file content
  set_fact:
    content10: "{{ output_content.content | b64decode }}"
    expected10: |

      [mysqld]
      skip-name
      max_connections = 500

- name: assert 'changed' and msg 'option changed' and content is as expected
  assert:
    that:
      - result10.changed == True
      - result10.msg == 'option changed'
      - content10 == expected10

- name: Clean test file
  copy:
    content: ""
    dest: "{{ output_file }}"
    force: yes

- name: Ensure "beverage=coke" is created within no section
  ini_file:
    section:
    path: "{{ output_file }}"
    option: beverage
    value: coke
  register: result11

- name: read content from output file
  slurp:
    src: "{{ output_file }}"
  register: output_content

- name: set expected content and get current ini file content
  set_fact:
    expected11: "beverage = coke\n\n"
    content11: "{{ output_content.content | b64decode }}"

- name: assert 'changed' is true and content is OK (no section)
  assert:
    that:
      - result11 is changed
      - result11.msg == 'option added'
      - content11 == expected11

- name: Ensure "beverage=coke" is modified as "beverage=water" within no section
  ini_file:
    path: "{{ output_file }}"
    option: beverage
    value: water
    section:
  register: result12

- name: read content from output file
  slurp:
    src: "{{ output_file }}"
  register: output_content

- name: set expected content and get current ini file content
  set_fact:
    expected12: "beverage = water\n\n"

    content12: "{{ output_content.content | b64decode }}"

- name: assert 'changed' is true and content is OK (no section)
  assert:
    that:
      - result12 is changed
      - result12.msg == 'option changed'
      - content12 == expected12

- name: remove option 'beverage' within no section
  ini_file:
    section:
    path: "{{ output_file }}"
    option: beverage
    state: absent
  register: result13

- name: read content from output file
  slurp:
    src: "{{ output_file }}"
  register: output_content

- name: get current ini file content
  set_fact:
    content13: "{{ output_content.content | b64decode }}"

- name: assert changed (no section)
  assert:
    that:
      - result13 is changed
      - result13.msg == 'option changed'
      - content13 == "\n"

- name: Check add option without section before existing section
  block:
    - name: Add option with section
      ini_file:
        path: "{{ output_file }}"
        section: drinks
        option: beverage
        value: water
    - name: Add option without section
      ini_file:
        path: "{{ output_file }}"
        section:
        option: like
        value: tea

- name: read content from output file
  slurp:
    src: "{{ output_file }}"
  register: output_content

- name: set expected content and get current ini file content
  set_fact:
    expected14: |
      like = tea

      [drinks]
      beverage = water
    content14: "{{ output_content.content | b64decode }}"

- name: Verify content of ini file is as expected
  assert:
    that:
      - content14 == expected14