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