summaryrefslogtreecommitdiff
path: root/test/integration/targets/mount/tasks/main.yml
blob: 3c0b79e11f5144768a7989c9d8d0a176a3b85b12 (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
# (c) 2016, Toshio Kuratomi <tkuratomi@ansible.com>

# 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: Create the mount point
  file:
    state: "directory"
    path: "{{ outputdir }}/mount_dest"

- name: Create a directory to bind mount
  file:
    state: "directory"
    path: "{{ outputdir }}/mount_source"

- name: Put something in the directory so we see that it worked
  copy:
    content: "Testing\n"
    dest: "{{ outputdir }}/mount_source/test_file"
  register: orig_info

# The opts type of bind mount only works on Linux
- name: Bind mount a filesystem (Linux)
  mount:
    src: "{{ outputdir }}/mount_source"
    name: "{{ outputdir }}/mount_dest"
    state: "mounted"
    fstype: "None"
    opts: "bind"
  when: ansible_system == 'Linux'
  register: bind_result_linux

# Nullfs is freebsd only
- name: Bind mount a filesystem (FreeBSD)
  mount:
    src: "{{ outputdir }}/mount_source"
    name: "{{ outputdir }}/mount_dest"
    state: "mounted"
    fstype: "nullfs"
  when: ansible_system == 'FreeBSD'
  register: bind_result_freebsd

- name: get checksum for bind mounted file
  stat:
    path: "{{ outputdir }}/mount_dest/test_file"
  when: ansible_system in ('FreeBSD', 'Linux')
  register: dest_stat

- name: assert the bind mount was successful
  assert:
    that:
      - "(ansible_system == 'Linux' and bind_result_linux['changed']) or (ansible_system == 'FreeBSD' and bind_result_freebsd['changed'])"
      - "dest_stat['stat']['exists']"
      - "orig_info['checksum'] == dest_stat['stat']['checksum']"
  when: ansible_system in ('FreeBSD', 'Linux')

# The opts type of bind mount only works on Linux
- name: Bind mount a filesystem (Linux)
  mount:
    src: "{{ outputdir }}/mount_source"
    name: "{{ outputdir }}/mount_dest"
    state: "mounted"
    fstype: "None"
    opts: "bind"
  when: ansible_system == 'Linux'
  register: bind_result_linux

# Nullfs is freebsd only
- name: Bind mount a filesystem (FreeBSD)
  mount:
    src: "{{ outputdir }}/mount_source"
    name: "{{ outputdir }}/mount_dest"
    state: "mounted"
    fstype: "nullfs"
  when: ansible_system == 'FreeBSD'
  register: bind_result_freebsd

- name: Make sure we didn't mount a second time
  assert:
    that:
      - "(ansible_system == 'Linux' and not bind_result_linux['changed']) or (ansible_system == 'FreeBSD' and not bind_result_freebsd['changed'])"
  when: ansible_system in ('FreeBSD', 'Linux')

# The opts type of bind mount only works on Linux
- name: Remount filesystem with different opts (Linux)
  mount:
    src: "{{ outputdir }}/mount_source"
    name: "{{ outputdir }}/mount_dest"
    state: "mounted"
    fstype: "None"
    opts: "bind,ro"
  when: ansible_system == 'Linux'
  register: bind_result_linux

# Nullfs is freebsd only
- name: Remount filesystem with different opts (FreeBSD)
  mount:
    src: "{{ outputdir }}/mount_source"
    name: "{{ outputdir }}/mount_dest"
    state: "mounted"
    fstype: "nullfs"
    opts: "ro"
  when: ansible_system == 'FreeBSD'
  register: bind_result_freebsd

- name: Get mount options
  shell: mount | grep mount_dest | grep -E -w '(ro|read-only)' | wc -l
  register: remount_options

- debug: var=remount_options

- name: Make sure the filesystem now has the new opts
  assert:
    that:
      - "(ansible_system == 'Linux' and bind_result_linux['changed']) or (ansible_system == 'FreeBSD' and bind_result_freebsd['changed'])"
      - "'1' in remount_options.stdout"
      - "1 == remount_options.stdout_lines | length"
  when: ansible_system in ('FreeBSD', 'Linux')

- name: Unmount the bind mount
  mount:
    name: "{{ outputdir }}/mount_dest"
    state: "absent"
  when: ansible_system in ('Linux', 'FreeBSD')
  register: unmount_result

- name: Make sure the file no longer exists in dest
  stat:
    path: "{{ outputdir }}/mount_dest/test_file"
  when: ansible_system in ('FreeBSD', 'Linux')
  register: dest_stat

- name: Check that we unmounted
  assert:
    that:
      - "unmount_result['changed']"
      - "not dest_stat['stat']['exists']"
  when: ansible_system in ('FreeBSD', 'Linux')