使用 Ansible 在 fstab 中重复输入

Double entry in fstab using Ansible

我想:

  1. 挂载 /dev/xvdb1 到 /mnt/newvar
  2. 将文件从 /var/ 复制到 /mnt/newvar
  3. 挂载 /dev/xvdb1 到 /var

但是当我使用 ansible 执行此操作时,我在 fstab 中得到了两个条目,它应该替换前一个条目,这个问题有什么解决方案吗?:

UUID=7698aac5-0a24-333f-a8c3-b76349gec0e2 /mnt/newvar ext4 默认值,noauto 0 0

UUID=7698aac5-0a24-333f-a8c3-b76349gec0e2 /var ext4 默认值 0 2

- name: "Get UUID for partition"
  command: "lsblk -no UUID /dev/xvdb1"
  register: with_output

- name: Mount /mnt/newvar to /dev/xvdb1
  mount:
    path: "{{ newvar_dir }}"
    src: "UUID={{ item }}"
    fstype: "{{ volume_filesystem_type }}"
    opts: "defaults,noauto"
    state: mounted
  with_items: 
    - "{{ with_output.stdout_lines }}"

- name: copy files from /var/* to /mnt/newvar 
  synchronize:
    src: /var/
    dest: "{{ newvar_dir }}"
    recursive: yes
    archive: yes
    delete: False
  delegate_to: "{{ vault_ip }}"

- name: Mount /dev/xvdb1 to /var
  mount:
    path: /var
    src: "UUID={{ item }}"
    fstype: "{{ volume_filesystem_type }}"
    opts: defaults
    state: mounted
    passno: 2
  with_items: 
    - "{{ with_output.stdout_lines }}"

当你安装相同的 UUID 两次被添加到 /etc/fstab 两次因为 mount ansible 模块是这样工作的:

所以它总是会修改 /etc/fstab 除非你使用 absent 但这不是你的情况

此处仍在讨论中:
https://github.com/ansible/ansible/issues/48134

如果你只想挂载

,我认为你可以使用 shell 模块

示例:

- shell: |
    mount /dev/xvdb1 /var