3

I am new to ansible and try to detect a filesystem and than mount if present. I have gone through below links :-

1. https://docs.ansible.com/ansible/latest/modules/filesystem_module.html
2. https://docs.ansible.com/ansible/latest/modules/mount_module.html

I have attached a hard drive manually which is detected by command fdisk -l as "/dev/sdb". I want ansible code to detect and mount this filesystem on some location. While running the code "df -h" is not showing the mounted filesystem and not failing also. And even if I am listing all filesystem or mount point through ansible code, this filesystem (/dev/sdb) is not listing.

Code Snippet:

    - name: Create File System
      filesystem:
        fstype: ext4
        dev: "{{ mount_src }}"

    - name: Mount File System
      mount:
        path: "{{ mount_path }}"
        src: "{{ mount_src }}"
        fstype: ext4
        state: mounted

Thanks in advance and any help would be appreciated.

OPTIMUS
  • 672
  • 4
  • 14
  • 29

2 Answers2

5

The play below creates a list of mounted devices. If mount_src is not mounted then filesystem is created and mount_src is mounted to mount_path.

    - hosts: localhost
      vars:
        mount_src: /dev/sdb
        mount_path: /export
      tasks:
        - name: Create list of mounted devices
          set_fact:
            mounted_devices: "{{ ansible_mounts|json_query('[].device') }}"
        - name: Create File System
          filesystem:
            fstype: ext4
            dev: "{{ mount_src }}"
          when: mount_src not in mounted_devices
        - name: Mount File System
          mount:
            path: "{{ mount_path }}"
            src: "{{ mount_src }}"
            fstype: ext4
            state: mounted
          when: mount_src not in mounted_devices

(not tested)

Ansible does not gather facts about the block devices. In Linux, it's possible to use the command lsblk instead, e.g.

    - hosts: localhost
      tasks:
        - name: Get list of block devices
          command: 'lsblk -lno NAME'
          register: results
        - name: Create variable block_devices
          set_fact:
            block_devices: "{{ results.stdout_lines }}"
        - debug:
            var: block_devices

gives

    ok: [127.0.0.1] => 
      block_devices:
      - sda
      - sda1
      - sda2
      - sda3
      - sda5
      - sdb
      - sdb1
      - sdb9
      - mmcblk0
      - mmcblk0p1
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
0

My ansible script to mount disk on localhost. i have added path in mount section as earlier version doesn't have this as required parameter now it's mandatory and script are failing at mounting if not added

---
- name: Create and mount new storage
  hosts: localhost
  tasks:
          - name: create new partition
            parted:
                    name: files
                    label: gpt
                    device: /dev/sdb
                    number: 1
                    state: present
                    part_start: 1MiB
                    part_end: 1GiB
          - name: Create xfs filesystem
            filesystem:
                    dev: /dev/sdb1
                    fstype: xfs
          - name: Create mount directory
            file:
                    path: /data
                    state: directory
          - name: mount the filesystem
            mount:
                    path: /data
                    src: /dev/sdb1
                    fstype: xfs
                    state: mounted