vinayak sawant
vinayak sawant

Reputation: 11

How to detect and create partition on newly attached disk in rhel with ansible

I am new to Ansible ( have basic working knowledge ). Trying to automate disk addition on linux machine from ansible playbook.

Below are the things trying out to achieve this.

  1. I tried to identify new disk which is attached on linux machine. From command line I am able to identify the new disk name but failing to find out with ansible.

Which module i need to write for this. Have tried with shell command for /sys/block but its not working hence I left this condition.

  1. Now i decided that i will scan the disk manually and will provide that name in ansible to create partition automatically.

For this i wrote below code.

- name: list out currnet PV
  shell: pvs --noheadings -o pv_name
  register: pvs_list

- debug: var=pvs_list.stdout

- name: create partition on the given disk name
  shell: /bin/echo -e "n\np\n1\n\n\nt\n8e\nw" | fdisk "{{ disk_name }}" ## Create the partition on a disk.
  register: partitioning

Above code works fine but if i run this job again it does not fail and it recreate new partition in disk again.

I tried to apply when / failed_when condition but its not working.

If the already existing disk is been provided again the play should fail with proper message.

failed_when: "'{{ disk_name }}"' in pvs_list.stdout" This condition also not working.

Also not able to identify new disk with Ansible.

Upvotes: 1

Views: 6051

Answers (2)

vinayak sawant
vinayak sawant

Reputation: 11

Finally able to test the fail condition. Like if i have already added /dev/sdb disk and if i try to run the play again it will fail.

- name: list out currnet PV
  shell: pvs --noheadings -o pv_name
  register: pvs_list

- debug: var=pvs_list.stdout

- name: create partition on the given disk name
  shell: /bin/echo -e "n\np\n1\n\n\nt\n8e\nw" | fdisk "{{ disk_name }}" ## Create the partition on a disk.
  register: partitioning

- debug: var=partitioning

- name: test failure condition
  fail:
    msg: " you are trying to add the same disk which is already there "
  when: "'{{ disk_name }}' in pvs_list.stdout"

Jenkins output on this play

#####################################################################################

Started by user Jenkins-Admin
Rebuilds build #64
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/add_lun
[add_lun] $ sshpass ******** /usr/bin/ansible-playbook /etc/ansible/Project-Automation/add_lun.yml -i /tmp/inventory536897656267250565.ini -f 5 -u sysadm -k -e disk_name=/dev/sdb

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [10.167.53.190]

TASK [add_lun : list out currnet PV] *******************************************
changed: [10.167.53.190]

TASK [add_lun : debug] *********************************************************
ok: [10.167.53.190] => {
    "pvs_list.stdout": "  /dev/sda2 \n  /dev/sdb1 "
}

TASK [add_lun : create partition on the given disk name] ***********************
changed: [10.167.53.190]

TASK [add_lun : test failure condition] ****************************************
 [WARNING]: when statements should not include jinja2 templating delimiters
such as {{ }} or {% %}. Found: '{{ disk_name }}' in pvs_list.stdout

fatal: [10.167.53.190]: FAILED! => {"changed": false, "msg": " you are trying to add the same disk which is already there "}
 [WARNING]: Could not create retry file '/etc/ansible/Project-
Automation/add_lun.retry'.         [Errno 13] Permission denied: u'/etc/ansible
/Project-Automation/add_lun.retry'


PLAY RECAP *********************************************************************
10.167.53.190              : ok=4    changed=2    unreachable=0    failed=1  

##################################################################################

Now i need to solve the issue of auto-disk identification.

Upvotes: 0

Vladimir Botka
Vladimir Botka

Reputation: 68384

Q: "Not able to identify new disk with Ansible"

A: Take a look at setup – Gathers facts about remote hosts. For example, given a disk in Linux localhost

$ lsscsi 
[N:0:1:1]    disk    SSDPEKKF256G8 NVMe INTEL 256GB__1          /dev/nvme0n1

take a look at what information is provided by Ansible

$ ansible localhost -m setup | grep nvme0n1 -A 2
            "nvme0n1": [
                "nvme-SSDPEKKF256G8_NVMe_INTEL_256GB_BTHH832111P1256B", 
                "nvme-eui.5cd2e42981b06cef"
  ...

For details see How to gather facts about disks using Ansible.

Take a look at How to create a new partition with Ansible (, format and mount it).

Upvotes: 1

Related Questions