Files
dotfiles/config/ansible/tasks/servers/sshfs.yml
2025-03-11 21:50:40 +01:00

56 lines
2.2 KiB
YAML

---
- name: Configure SSHFS
block:
- name: Debug which plugin is being used
ansible.builtin.debug:
msg: "Using lookup plugins from: {{ lookup('pipe', 'ansible-config dump | grep DEFAULT_LOOKUP_PLUGIN_PATH') }}"
- name: Get SSHFS credentials via local lookup
delegate_to: localhost
ansible.builtin.set_fact:
sshfs_user: "{{ lookup('my_1password', 'op://j7nmhqlsjmp2r6umly5t75hzb4/5j5y5axfjr3f3sn5nixb6htg4y/username') }}"
sshfs_pass: "{{ lookup('my_1password', 'op://j7nmhqlsjmp2r6umly5t75hzb4/5j5y5axfjr3f3sn5nixb6htg4y/new_password') }}"
sshfs_host: "{{ lookup('my_1password', 'op://j7nmhqlsjmp2r6umly5t75hzb4/5j5y5axfjr3f3sn5nixb6htg4y/host') }}"
sshfs_port: 23
remote_path: /mnt/storage-box
- name: Install SSHFS package
become: true
ansible.builtin.package:
name: sshfs
state: present
- name: Create mount point directory
become: true
ansible.builtin.file:
path: "{{ remote_path }}"
state: directory
mode: '0755'
- name: Create credentials file for SSHFS
become: true
ansible.builtin.copy:
content: "username={{ sshfs_user }}\npassword={{ sshfs_pass }}"
dest: /etc/sshfs-credentials
mode: '0600'
owner: root
group: root
- name: Add fstab entry for SSHFS
become: true
ansible.builtin.lineinfile:
path: /etc/fstab
line: "sshfs#{{ sshfs_user }}@{{ sshfs_host }}: {{ remote_path }} fuse.sshfs _netdev,credentials=/etc/sshfs-credentials,port={{ sshfs_port }},reconnect,ServerAliveInterval=15,ServerAliveCountMax=3,uid=1000,gid=1000 0 0"
state: present
regexp: "^sshfs#.*{{ remote_path }}"
backup: true
- name: Mount SSHFS filesystem
become: true
ansible.builtin.command:
cmd: "sshfs {{ sshfs_user }}@{{ sshfs_host }}:/ {{ remote_path }} -o port={{ sshfs_port }},password_stdin,_netdev,reconnect,ServerAliveInterval=15,ServerAliveCountMax=3"
stdin: "{{ sshfs_pass }}"
register: mount_result
changed_when: mount_result.rc == 0
failed_when: mount_result.rc != 0 and "already mounted" not in mount_result.stderr