【问题标题】:Ansible mount for different directories on different servers不同服务器上不同目录的 Ansible 挂载
【发布时间】:2017-09-29 15:40:41
【问题描述】:

我们在/etc/fstab 文件中为不同的服务器配置了不同的挂载点。我怎样才能使用 Ansible 达到同样的效果?

例如:

服务器 A 有一个 source node1:/data/col1/RMAN 安装在 path /mountPointA

以上我们可以使用Ansible的mount模块如


- name: Add Mount Points
mount:
  path: /mnt/dvd
  src : /dev/sr0
  fstype: iso09660
  opts: ro
  boot: yes
  state: present

但是

如果我有另一个服务器“B”,它的source /node1:/data/col1/directoryB 需要安装在path /mountPointB。但是,此服务器不需要配置第一个挂载点。

是否可以在单个 yml 文件中实现?

换句话说

Host      source                   dest

hostA    /source/directoryA     /mnt

hostB    /source/directoryB     /mnt or /mnt/subdirectory #assuming subdir exists

我希望这是有道理的。对困惑感到抱歉。该剧本将在大量主机上运行,​​我如何确保自动选择正确的主机以使用正确的挂载点

【问题讨论】:

    标签: ansible mount-point


    【解决方案1】:

    有很多方法可以解决这个问题。如果您愿意,我将建议一种方法,让您为每个主机定义多个挂载点。如果您在 playbook 所在的位置有一个 host_vars 目录,Ansible 将在那里查找以您的主机命名的文件并从这些文件中加载变量。例如,如果您的清单中有一个名为 serverA 的主机,Ansible 会查找 host_vars/serverA.yml

    我们将利用它来指定每个主机的挂载配置。创建一个文件host_vars/serverA.yml,其中包含:

    mountinfo:
      - src: node1:/data/col1/RMAN
        dst: /mountpointA
        fstype: nfs
    

    并使用以下命令创建host_vars/serverB.yml

    mountinfo:
      - src: node1:/data/col1/directoryB
        dst: /mountpointB
        fstype: nfs
    

    然后在你的剧本中:

    - name: Add Mount Points
      mount:
        path: "{{ item.dst }}"
        src : "{{ item.src }}"
        fstype: "{{ item.fstype }}"
        opts: ro
        boot: yes
        state: present
      with_items: "{{ mountinfo }}"
      when: mountinfo is defined
    

    如果此答案中的某些内容不清楚,我已将上述内容作为可运行的示例 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多