summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/lineinfile/tasks/test_string01.yml
blob: b86cd09acd2b61f96311bd767589d75b0bb246ff (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
---
###################################################################
# 1st search_string tests

- name: deploy the test file for lineinfile string
  copy:
    src: teststring.txt
    dest: "{{ remote_tmp_dir }}/teststring.txt"
  register: result

- name: assert that the test file was deployed
  assert:
    that:
      - result is changed
      - "result.checksum == '481c2b73fe062390afdd294063a4f8285d69ac85'"
      - "result.state == 'file'"

- name: insert a line at the beginning of the file, and back it up
  lineinfile:
    dest: "{{ remote_tmp_dir }}/teststring.txt"
    state: present
    line: "New line at the beginning"
    insertbefore: "BOF"
    backup: yes
  register: result1

- name: insert a line at the beginning of the file again
  lineinfile:
    dest: "{{ remote_tmp_dir }}/teststring.txt"
    state: present
    line: "New line at the beginning"
    insertbefore: "BOF"
  register: result2

- name: Replace a line using string
  lineinfile:
    dest: "{{ remote_tmp_dir }}/teststring.txt"
    state: present
    line: "Thi$ i^ [ine 3"
    search_string: (\\w)(\\s+)([\\.,])
  register: backrefs_result1

- name: Replace a line again using string
  lineinfile:
    dest: "{{ remote_tmp_dir }}/teststring.txt"
    state: present
    line: "Thi$ i^ [ine 3"
    search_string: (\\w)(\\s+)([\\.,])
  register: backrefs_result2

- command: cat {{ remote_tmp_dir }}/teststring.txt

- name: assert that the line with backrefs was changed
  assert:
    that:
      - backrefs_result1 is changed
      - backrefs_result2 is not changed
      - "backrefs_result1.msg == 'line replaced'"

- name: stat the test after the backref line was replaced
  stat:
    path: "{{ remote_tmp_dir }}/teststring.txt"
  register: result

- name: assert test checksum matches after backref line was replaced
  assert:
    that:
      - "result.stat.checksum == '8084519b53e268920a46592a112297715951f167'"

- name: remove the middle line using string
  lineinfile:
    dest: "{{ remote_tmp_dir }}/teststring.txt"
    state: absent
    search_string: "Thi$ i^ [ine 3"
  register: result

- name: assert that the line was removed
  assert:
    that:
      - result is changed
      - "result.msg == '1 line(s) removed'"

- name: stat the test after the middle line was removed
  stat:
    path: "{{ remote_tmp_dir }}/teststring.txt"
  register: result

- name: assert test checksum matches after the middle line was removed
  assert:
    that:
      - "result.stat.checksum == '89919ef2ef91e48ad02e0ca2bcb76dfc2a86d516'"

- name: run a validation script that succeeds using string
  lineinfile:
    dest: "{{ remote_tmp_dir }}/teststring.txt"
    state: absent
    search_string: <FilesMatch ".py[45]?$">
    validate: "true %s"
  register: result

- name: assert that the file validated after removing a line
  assert:
    that:
      - result is changed
      - "result.msg == '1 line(s) removed'"

- name: stat the test after the validation succeeded
  stat:
    path: "{{ remote_tmp_dir }}/teststring.txt"
  register: result

- name: assert test checksum matches after the validation succeeded
  assert:
    that:
      - "result.stat.checksum == 'ba9600b34febbc88bfb3ca99cd6b57f1010c19a4'"

- name: run a validation script that fails using string
  lineinfile:
    dest: "{{ remote_tmp_dir }}/teststring.txt"
    state: absent
    search_string: "This is line 1"
    validate: "/bin/false %s"
  register: result
  ignore_errors: yes

- name: assert that the validate failed
  assert:
    that:
      - "result.failed == true"

- name: stat the test after the validation failed
  stat:
    path: "{{ remote_tmp_dir }}/teststring.txt"
  register: result

- name: assert test checksum matches the previous after the validation failed
  assert:
    that:
      - "result.stat.checksum == 'ba9600b34febbc88bfb3ca99cd6b57f1010c19a4'"

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