IBMi check if library already exists

I want to include this create library task in playbooks that will run against IBMi systems. However, the playbook fails if the library already exists, and I need to know how to skip the command if the library is already there. In a CL program I would use MONMSG, is there something equivalent in Ansible?

  • Name: create library

Ibmi_cl_command:

Cmd: ‘CRTLIB LIB(libname)’

Hi,

This is not how you should write Ansible code.

For example,

Ansible is a declarative language and you should use a native module group which provides idempotence:

  • name: Ensure 'hadoop’ group exists

group:

name: hadoop

state: present

This way you don't have to check anything. The same task will either create a new group (status changed) or report that the group already exists (status ok). The state after executing will be the same: group hadoop is present.

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/group_module.html

Unfortunately not every action you want to perform has been implemented as native modules, that’s why command module exists. But it should be used as a last resort.

What you are trying to achieve is possible using a custom python script, see references below;

https://www.ibm.com/support/pages/ansible-support-ibm-i

https://www.ibm.com/support/pages/mustgather-ansible-ibm-i

https://github.com/IBM/ansible-for-i/blob/devel/plugins/modules/ibmi_at.py

The point is because Ansible is idempotent you shouldn’t have to worry about skipping the play because if you have properly written your Ansible code, then it will have no effect it already exists, to skip a play in Ansible this is done using the conditional execution (when) see below for an example:

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_conditionals.html#the-when-statement

See this guide for a comprehensive explanation:

https://www.middlewareinventory.com/blog/ansible-command-examples/

This is the custom python script for creating a library using CL command CRTLIB

https://github.com/IBM/ansible-for-i/blob/devel/plugins/modules/ibmi_cl_command.py