使用Ansible挂载Azure文件共享

时间:2017-12-20 11:03:59

标签: ansible azure-storage

我想通过Ansible挂载Azure文件共享。 如果我手动挂载,则命令如下:

sudo mount -t cifs //xxxxxxxxxx.file.core.windows.net/yyyyyyyyyy /<mount point> -o vers=2.1,username=<username>,password=<password>,dir_mode=0777,file_mode=0777,serverino

有人可以帮我使用安装模块通过Ansible进行安装。

2 个答案:

答案 0 :(得分:1)

只需使用Mount Module的ansible docs中描述的语法,它就应该有效。

示例:

- name : Mount Azure files share's
  mount:
    fstype: cifs
    src: "//xxxxxxxxxx.file.core.windows.net/yyyyyyyyyy"
    path: /mountpoint
    opts: vers=2.1,username=<username>,password=<password>,dir_mode=0777,file_mode=0777,serverino
    state: mounted

答案 1 :(得分:0)

以下代码在 centos 7 上的 Ansible 2.7.5 中运行良好:

首先,确保实例具有 Storage Account Key Operator Service Role 权限或具有该权限的用户分配身份。还应安装 jq

然后,检查是否已经设置了挂载:

  - name: check mount
    command: "mountpoint /mymounts/mysmb"
    register: smb_check
    ignore_errors: True

然后,获取存储令牌:

  - name: get storage token
    shell: "curl -s 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fmanagement.azure.com%2F' -H Metadata:true | jq -r '.access_token'"
    register: storage_token
    no_log: true
    when: smb_check is failed

然后,获取存储密钥。设置您的 subscription_idresource_group_namestorage_account_name

  - name: get storage key
    shell: "curl -s https://management.azure.com/subscriptions/{{ subscription_id }}/resourceGroups/{{ resource_group_name }}/providers/Microsoft.Storage/storageAccounts/{{ storage_account_name }}/listKeys?api-version=2016-12-01 --request POST -d \"\" -H \"Authorization: Bearer {{ storage_token.stdout }}\" | jq -r '.keys[0].value'"
    register: storage_key
    no_log: true
    when: smb_check is failed

将这些行添加到文件中。替换 storage_account_name

  - name: add SMB creds to file
    lineinfile:
      dest: "/etc/.smb"
      line: "{{ item }}"
      state: present
      create: yes
      owner: root
      group: root
      mode: '0600'
    no_log: true
    with_items:
      - "username={{ storage_account_name }}"
      - "password={{ storage_key.stdout }}"
    when: smb_check is failed

现在,安装驱动器。替换 storage_account_namefile_share_name

  - name : mount smb
    mount:
      fstype: cifs
      src: "//{{ storage_account_name }}.file.core.windows.net/{{ file_share_name }}"
      path: "/mymounts/mysmb"
      opts: "nofail,vers=3.0,credentials=/etc/.smb,serverino,dir_mode=0755,file_mode=0755"
      state: mounted
    when: smb_check is failed

最后,为挂载设置适当的权限:

  - name: set correct permissions
    file:
      path: "/mymount/mysmb"
      mode: "2755"
      recurse: yes
    when: smb_check is failed