Thanks Dick. I have modified the playbook and used "block".

- name: Create partition
  hosts: all
  become: true
  become_user: root
  tasks:
    - name: Gather disk info
      parted:
        device: /dev/sdb
        unit: GB
      register: device_info
      when: ansible_devices.sdb is defined

    - name: Create 3000MiB  partition
      block:
       - name: Create 3000MiB partition
         parted:
           device: /dev/sdb
           number: 1
           part_start: 1MiB
           part_end: 3000MiB
           state: present
         when: ansible_devices.sdb1 is not defined

      rescue:
       - name: Could not create 3000MiB partition
         debug:
           msg: "{{ ansible_hostname }} does not have sufficient space"
         when: device_info.disk.size < 3000

       - name: Create 800MiB partition
         parted:
           device: /dev/sdb
           number: 1
           part_start: 1MiB
           part_end: 800MiB
           state: present

      always:
       - name: /dev/sdb  doesn't exist
         debug:
           msg: "/dev/sdb doesn't exist"
         when: ansible_devices.sdb is not defined

       - name: Create file system
         filesystem:
           dev: /dev/sdb1
           fstype: ext4

       - name: Create mountpoint
         file:
           path: /newpart
           state: directory
           owner: root
           group: root
           mode: '0755'

       - name: Mount the file system
         mount:
           path: /newpart
           src: /dev/sdb1
           fstype: ext4
           state: mounted

$ ansible-playbook partition.yml 

PLAY [Create partition] 
********************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] 
*********************************************************************************************************************************************************************************************************************
ok: [ansible4.example.com]
ok: [ansible5.example.com]
ok: [ansible3.example.com]
ok: [ansible2.example.com]

TASK [Gather disk info] 
********************************************************************************************************************************************************************************************************************
skipping: [ansible5.example.com]
ok: [ansible4.example.com]
ok: [ansible3.example.com]
ok: [ansible2.example.com]

TASK [Create 3000MiB partition] 
************************************************************************************************************************************************************************************************************
fatal: [ansible5.example.com]: FAILED! => {"changed": false, "err": "Error: 
Could not stat device /dev/sdb - No such file or directory.\n", "msg": 
"Error while getting device information with parted script: '/sbin/parted 
-s -m /dev/sdb -- unit 'KiB' print'", "out": "", "rc": 1}
fatal: [ansible3.example.com]: FAILED! => {"changed": false, "err": "Error: 
The location 3000MiB is outside of the device /dev/sdb.\n", "msg": "Error 
while running parted script: /sbin/parted -s -m -a optimal /dev/sdb -- unit 
KiB mkpart primary 1MiB 3000MiB", "out": "", "rc": 1}
changed: [ansible2.example.com]
changed: [ansible4.example.com]

TASK [Could not create 3000MiB partition] 
**************************************************************************************************************************************************************************************************
fatal: [ansible5.example.com]: FAILED! => {"msg": "The conditional check 
'device_info.disk.size < 3000' failed. The error was: error while 
evaluating conditional (device_info.disk.size < 3000): 'dict object' has no 
attribute 'disk'\n\nThe error appears to be in 
'/home/devops/ansible/partition.yml': line 25, column 10, but may\nbe 
elsewhere in the file depending on the exact syntax problem.\n\nThe 
offending line appears to be:\n\n      rescue:\n       - name: Could not 
create 3000MiB partition\n         ^ here\n"}
ok: [ansible3.example.com] => {
    "msg": "ansible3 does not have sufficient space"
}

TASK [Create 800MiB partition] 
*************************************************************************************************************************************************************************************************************
changed: [ansible3.example.com]

TASK [/dev/sdb  doesn't exist] 
*************************************************************************************************************************************************************************************************************
skipping: [ansible2.example.com]
ok: [ansible5.example.com] => {
    "msg": "/dev/sdb doesn't exist"
}
skipping: [ansible3.example.com]
skipping: [ansible4.example.com]

TASK [Create file system] 
******************************************************************************************************************************************************************************************************************
fatal: [ansible5.example.com]: FAILED! => {"changed": false, "msg": "Device 
/dev/sdb1 not found."}
ok: [ansible4.example.com]
ok: [ansible3.example.com]
ok: [ansible2.example.com]

TASK [Create mountpoint] 
*******************************************************************************************************************************************************************************************************************
ok: [ansible4.example.com]
ok: [ansible3.example.com]
ok: [ansible2.example.com]

TASK [Mount the file system] 
***************************************************************************************************************************************************************************************************************
changed: [ansible3.example.com]
changed: [ansible2.example.com]
changed: [ansible4.example.com]

PLAY RECAP 
*********************************************************************************************************************************************************************************************************************************
ansible2.example.com       : ok=6    changed=2    unreachable=0    
failed=0    skipped=1    rescued=0    ignored=0   
ansible3.example.com       : ok=7    changed=2    unreachable=0    
failed=0    skipped=1    rescued=1    ignored=0   
ansible4.example.com       : ok=6    changed=2    unreachable=0    
failed=0    skipped=1    rescued=0    ignored=0   
ansible5.example.com       : ok=2    changed=0    unreachable=0    
failed=2    skipped=1    rescued=1    ignored=0   


On Wednesday, June 10, 2020 at 2:50:45 AM UTC+10, Dick Visser wrote:
>
> On Tue, 9 Jun 2020 at 14:26, Rajesh <[email protected] <javascript:>> 
> wrote: 
> > 
> > Thanks again for your help. What needs to be done so that the below play 
> will print the message? 
> > 
> >  - name: No extra hard disk 
> >    debug: 
> >       msg: "No extra hard disk available" 
> >    when: ansible_devices.sdb is undefined 
>
> This should be moved all the way up, so that it is the first task. 
> Note that the majority of the subsequent tasks assume /dev/sdb is 
> available, while this is clearly not the case. 
> You should probably revise the playbook and add (more) conditionals. 
> For instance, one of the previous outputs you posted shows that the 
> playbook tried to create a filesystem on a device that is already 
> mounted. 
> The logic with 1200MB could also be improved. If the size is > 1200. 
> Create a 1200MB partition - that seems OK. 
> But when it's smaller than 1200, create a 800MB partition. What 
> happens if the device is less than 800MB? 
> Etc etc 
>
>
> -- 
> Dick Visser 
> Trust & Identity Service Operations Manager 
> GÉANT 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/26810bab-11fd-4945-a791-5f97c2915444o%40googlegroups.com.

Reply via email to