如何使用Ansible挂载fstab上列出的所有分区

如何使用Ansible挂载fstab上列出的所有分区,ansible,Ansible,在使用Ansible的lineinfle模块向/etc/fstab添加多个装载点之后,我想运行一个简单的mount-a,使它们生效 它确实与 - name: mount all command: mount -a become: true 然而,ansible坚持建议使用mount模块 [WARNING]: Consider using mount module rather than running mount 但在我看来,要做到这一点并不容易 我遗漏了什么吗?你说得对

在使用Ansible的
lineinfle
模块向
/etc/fstab
添加多个装载点之后,我想运行一个简单的
mount-a
,使它们生效

它确实与

  - name: mount all
    command: mount -a
    become: true
然而,ansible坚持建议使用
mount
模块

[WARNING]: Consider using mount module rather than running mount
但在我看来,要做到这一点并不容易


我遗漏了什么吗?

你说得对,Ansible当前版本中的
挂载
模块以每次挂载为基础工作,不允许挂载
/etc/fstab
中定义的所有文件系统。您执行任务的方式是正确的

您在输出中看到的是一个警告:

[警告]:考虑使用模块而不是运行挂载

Ansible通过
command
模块检查您正在运行的
mount
命令,并建议使用本机模块

Ansible不执行高级语法分析来验证您在命令调用中使用的特定参数集是否可以使用本机模块参数实现

通过将
warn
参数设置为
no
,可以在每个任务的基础上禁用
shell
command
模块的警告:

- name: mount all
  command: mount -a
  args:
    warn: no
  become: true

请注意,您可以使用mount模块和状态“present”添加FS选项卡条目。这样,就不会发生挂载,您可以使用mount all一次挂载所有条目

- name: Mount drive
  mount:
    name: new mount
    src: /dev/xvdb1
    fstype: ext4
    state: present

我基本上使用这种方法,因为ansible在尝试挂载分区时使用了标志“remount”。例如,此标志不能很好地与glusterfs配合使用,因为它会抛出“驱动器已装入”的错误,这将中断您的ansible运行。

当前fstab的备份:

#cp -a /etc/fstab /etc/fstab.bk
使用sedawk处理lsblk-f的输出,并将输出重定向到fstab:

#lsblk -f|sed 's/\[SWAP]/swap /g'|awk '/(-)/{printf"UUID=%-36s %-23s %-7s defaults 
           0 0\n", $3, ($4==""?mnt"NR:$4), $2}'>/etc/fstab
通过调用

#mount -a