Following code works fine in Ansible 2.7.5 on centos 7:
First, make sure the instance has the Storage Account Key Operator Service Role
permission or a user assigned identity which has that permission. Also jq
should be installed:
Then, check if the mount is already set up:
- name: check mount
command: "mountpoint /mymounts/mysmb"
register: smb_check
ignore_errors: True
Then, get a storage token:
- 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
Then, get the storage key. Set your subscription_id
, resource_group_name
, and storage_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
Add the lines into a file. Replace 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
Now, mount the drive. Replace storage_account_name
and file_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
Finally, set the proper permission for the mount:
- name: set correct permissions
file:
path: "/mymount/mysmb"
mode: "2755"
recurse: yes
when: smb_check is failed