ansible playbook not printing the error message when disk is not defined

98 views
Skip to first unread message

Rajesh

unread,
Jun 7, 2020, 9:59:30 AM6/7/20
to Ansible Project
Hi all,

The below playbook is not printing an error message when the disk is not defined on a host. Could you please advise if I have made an mistake in the playbook. Thanks

- name: Create a partition
  hosts: all
  become: true
  become_user: root
  tasks:
   - name: Gather disk info
     parted:
        device: /dev/sdb
        unit: MB
     register: device_info

   - name: Print disk info
     debug:
       var: device_info

   - name: Create partition
     parted:
       device: /dev/sdb
       number: 1
       part_start: 1MB
       part_end: 1200MB
       unit: MB
       state: present
     when: device_info.disk.size > 1200

   - name: Print enough size is not available
     debug:
      msg: "Enough size is not available"
     when: device_info.disk.size < 1200
  
   - name: Create 800MB partition
     parted:
       device: /dev/sdb
       number: 1
       part_start: 1MB
       part_end: 800MB
       unit: MB
       state: present
     when: device_info.disk.size < 1200

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

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

   - name: mount File system
     mount:
       path: /exam
       src: /dev/sdb1
       fstype: ext4
       state: mounted

   - name: No extra hard disk
     debug:
      msg: "No extra hard disk available"
     when: ansible_devices.sdb is undefined


$ ansible-playbook partition.yml

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

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

TASK [Gather disk info] ********************************************************************************************************************************************************************************************************************
fatal: [ansible4.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 'MB' print'", "out": "", "rc": 1}
ok: [ansible3.example.com]
ok: [ansible2.example.com]

TASK [Print disk info] *********************************************************************************************************************************************************************************************************************
ok: [ansible2.example.com] => {
    "device_info": {
        "changed": false,
        "disk": {
            "dev": "/dev/sdb",
            "logical_block": 512,
            "model": "ATA VBOX HARDDISK",
            "physical_block": 512,
            "size": 12885.0,
            "table": "msdos",
            "unit": "mb"
        },
        "failed": false,
        "partitions": [
            {
                "begin": 1.05,
                "end": 1200.0,
                "flags": [],
                "fstype": "ext4",
                "name": "",
                "num": 1,
                "size": 1199.0,
                "unit": "mb"
            }
        ],
        "script": "unit 'MB' print"
    }
}
ok: [ansible3.example.com] => {
    "device_info": {
        "changed": false,
        "disk": {
            "dev": "/dev/sdb",
            "logical_block": 512,
            "model": "ATA VBOX HARDDISK",
            "physical_block": 512,
            "size": 1074.0,
            "table": "msdos",
            "unit": "mb"
        },
        "failed": false,
        "partitions": [
            {
                "begin": 1.05,
                "end": 800.0,
                "flags": [],
                "fstype": "ext4",
                "name": "",
                "num": 1,
                "size": 799.0,
                "unit": "mb"
            }
        ],
        "script": "unit 'MB' print"
    }
}

TASK [Create partition] ********************************************************************************************************************************************************************************************************************
skipping: [ansible3.example.com]
ok: [ansible2.example.com]

TASK [Print enough size is not available] **************************************************************************************************************************************************************************************************
skipping: [ansible2.example.com]
ok: [ansible3.example.com] => {
    "msg": "Enough size is not available"
}

TASK [Create 800MB partition] **************************************************************************************************************************************************************************************************************
skipping: [ansible2.example.com]
ok: [ansible3.example.com]

TASK [Create file system] ******************************************************************************************************************************************************************************************************************
fatal: [ansible3.example.com]: FAILED! => {"changed": false, "cmd": "/sbin/mkfs.ext4 -F /dev/sdb1", "msg": "mke2fs 1.44.6 (5-Mar-2019)\n/dev/sdb1 is mounted; will not make a filesystem here!", "rc": 1, "stderr": "mke2fs 1.44.6 (5-Mar-2019)\n/dev/sdb1 is mounted; will not make a filesystem here!\n", "stderr_lines": ["mke2fs 1.44.6 (5-Mar-2019)", "/dev/sdb1 is mounted; will not make a filesystem here!"], "stdout": "", "stdout_lines": []}
fatal: [ansible2.example.com]: FAILED! => {"changed": false, "cmd": "/sbin/mkfs.ext4 -F /dev/sdb1", "msg": "mke2fs 1.44.6 (5-Mar-2019)\n/dev/sdb1 is mounted; will not make a filesystem here!", "rc": 1, "stderr": "mke2fs 1.44.6 (5-Mar-2019)\n/dev/sdb1 is mounted; will not make a filesystem here!\n", "stderr_lines": ["mke2fs 1.44.6 (5-Mar-2019)", "/dev/sdb1 is mounted; will not make a filesystem here!"], "stdout": "", "stdout_lines": []}

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

Regards,
Rajesh

Rajesh

unread,
Jun 8, 2020, 9:21:53 PM6/8/20
to Ansible Project
Could someone please respond? Thanks

Dick Visser

unread,
Jun 9, 2020, 12:21:10 AM6/9/20
to ansible...@googlegroups.com
Your playbook IS printing an error when the disk is not defined. See below

^^^^here


--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e36f43d4-558b-44b4-8f7c-fd6e793f472co%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Rajesh

unread,
Jun 9, 2020, 6:20:20 AM6/9/20
to Ansible Project
Thanks for the reply. The playbook is not printing message as per the below task.

   - name: No extra hard disk
     debug:
      msg: "No extra hard disk available"
     when: ansible_devices.sdb is undefined

Thanks,
Rajesh
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Dick Visser

unread,
Jun 9, 2020, 7:35:07 AM6/9/20
to ansible...@googlegroups.com
On Tue, 9 Jun 2020 at 12:20, Rajesh <rajeshbr...@gmail.com> wrote:
>
> Thanks for the reply. The playbook is not printing message as per the below task.
>
> - name: No extra hard disk
> debug:
> msg: "No extra hard disk available"
> when: ansible_devices.sdb is undefined

This is also correct. As the previous failure was already fatal, the
playbook will stop executing commands for that host
(ansible4.example.com).
You can also tell from the play recap.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Rajesh

unread,
Jun 9, 2020, 8:26:10 AM6/9/20
to Ansible Project
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

Thanks,
Rajesh

Dick Visser

unread,
Jun 9, 2020, 12:50:45 PM6/9/20
to ansible...@googlegroups.com
On Tue, 9 Jun 2020 at 14:26, Rajesh <rajeshbr...@gmail.com> 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

Rajesh

unread,
Jun 9, 2020, 10:06:39 PM6/9/20
to Ansible Project
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  

Reply all
Reply to author
Forward
0 new messages