1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Use bytes rather than native string for result (#46281)

This prevents a stack trace in Python 3 when the result is an empty file since
the file is open in binary mode and a native string in Python 3 is str,
not bytes.
This commit is contained in:
Sam Doran 2018-09-28 16:04:50 -04:00 committed by GitHub
parent 2d01966e87
commit 8b1ae30e2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 24 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- blockinfile - use bytes rather than a native string to prevent a stacktrace in Python 3 when writing to the file (https://github.com/ansible/ansible/issues/46237)

View file

@ -309,7 +309,7 @@ def main():
if original is None or original.endswith(b('\n')): if original is None or original.endswith(b('\n')):
result += b('\n') result += b('\n')
else: else:
result = '' result = b''
if module._diff: if module._diff:
diff['after'] = result diff['after'] = result

View file

@ -16,13 +16,18 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
- set_fact: output_dir_test={{output_dir}}/test_blockinfile - set_fact:
output_dir_test: "{{ output_dir }}/test_blockinfile"
- name: make sure our testing sub-directory does not exist - name: make sure our testing sub-directory does not exist
file: path="{{ output_dir_test }}" state=absent file:
path: "{{ output_dir_test }}"
state: absent
- name: create our testing sub-directory - name: create our testing sub-directory
file: path="{{ output_dir_test }}" state=directory file:
path: "{{ output_dir_test }}"
state: directory
## ##
## blockinfile ## blockinfile
@ -40,11 +45,19 @@
Match User ansible-agent Match User ansible-agent
PasswordAuthentication no PasswordAuthentication no
register: blockinfile_test0 register: blockinfile_test0
- name: check content - name: check content
shell: 'grep -e "Match User ansible-agent" -e "PasswordAuthentication no" {{ output_dir_test }}/sshd_config' shell: 'grep -e "Match User ansible-agent" -e "PasswordAuthentication no" {{ output_dir_test }}/sshd_config'
register: blockinfile_test0_grep register: blockinfile_test0_grep
- debug: var=blockinfile_test0
- debug: var=blockinfile_test0_grep - debug:
var: blockinfile_test0
verbosity: 1
- debug:
var: blockinfile_test0_grep
verbosity: 1
- name: validate first example results - name: validate first example results
assert: assert:
that: that:
@ -61,7 +74,41 @@
Match User ansible-agent Match User ansible-agent
PasswordAuthentication no PasswordAuthentication no
register: blockinfile_test1 register: blockinfile_test1
- name: validate idempotence results - name: validate idempotence results
assert: assert:
that: that:
- 'not blockinfile_test1.changed' - 'not blockinfile_test1.changed'
- name: Create a file with blockinfile
blockinfile:
path: "{{ output_dir_test }}/empty.txt"
block: |
Hey
there
state: present
create: yes
register: empty_test_1
- name: Run a task that results in an empty file
blockinfile:
path: "{{ output_dir_test }}/empty.txt"
block: |
Hey
there
state: absent
create: yes
register: empty_test_2
- stat:
path: "{{ output_dir_test }}/empty.txt"
register: empty_test_stat
- name: Ensure empty file was created
assert:
that:
- empty_test_1 is changed
- "'File created' in empty_test_1.msg"
- empty_test_2 is changed
- "'Block removed' in empty_test_2.msg"
- empty_test_stat.stat.size == 0