I just started working with Ansible recently. I am trying to mount 4 disks on azure VM which were attached using terraform. Each disk was passed with a LUN number and I am fetching the device name(sdc,sdd etc.) for each disk using that LUN no.and grep.
- name: get volume name
shell: echo "/dev/$(ls -l /dev/disk/azure/scsi1 |grep {{ item.lun }}|egrep -o "([^\/]+$)")"
register: volumename
- parted:
device: "{{ volumename.stdout }}"
number: 1
state: present
- filesystem:
fstype: xfs
dev: "{{ volumename.stdout }}"
- mount:
fstype: xfs
opts: noatime
src: "{{ volumename.stdout }}"
path: "{{ item.mountpoint }}"
state: mounted
- command: blkid -s UUID -o value {{ volumename.stdout }}
register: volumename_disk
- blockinfile:
path: /etc/fstab
state: present
block: |
UUID={{ volumename_disk.stdout }} {{ volumename.stdout }} xfs defaults,noatime,nofail 0 0
loop:
- { lun: 'lun0', mountpoint: '/apps/mysql/binlog', diskname: 'binlog' }
- { lun: 'lun1', mountpoint: '/apps/mysql/innodb', diskname: 'innodb' }
- { lun: 'lun2', mountpoint: '/apps/mysql/data', diskname: 'data' }
- { lun: 'lun3', mountpoint: '/apps/mysql', diskname: 'backup' }
tags: test
I keep getting volumename variable is not defined error. Is there any way to set volumename as global so that the next playbook can access it or How I can improve this code ?
After reading through documentation, I was able to find a way to achieve this.
The right approach would be to use Ansible's dictionary variable.
raid_config:
- lun: "{{ backup_lun }}"
mountpoint: "{{ backup_mountpoint }}"
- lun: "{{ data_lun }}"
mountpoint: "{{ data_mountpoint }}"
- lun: "{{ binlog_lun }}"
mountpoint: "{{ binlog_mountpoint }}"
- lun: "{{ innodb_lun }}"
mountpoint: "{{ innodb_mountpoint }}"
Moved the tasks to a separate file mountdisk.yml and passed a dictionary to the tasks like this
- include_tasks: "mountdisk.yml"
loop: "{{ raid_config }}"
loop_control:
loop_var: disk
tags: test