Ondrej Famera - top logo

Configuring iSCSI target with targetcli in RHEL/CentOS using ansible

With ultimate goal of setting up something more complex in clusters I have came across the qeustion of shared storage. The immediate answer was to use some iSCSI as a simple solution and as I wanted to stay at least a bit up to date with CentOS/RHEL approach this means targetcli. I have found several roles for this but as expected they were using direct shell commands with a sometimes questionable detection of current status. So the result was a quick look into doing this a more proper way. In the end the creation contains one ansible role taht is capable of setting up targecli-based iSCSI target(server) on CentOS/RHEL 7.2/7.7. Check link below for role:

  • OndrejHome.targetcli

How to use the OndrejHome.targetcli role

  • Step 1: Install the role from ansible galaxy as root using following command.
    $ ansible-galaxy install OndrejHome.targetcli
    
  • Step 2: Create the inventory file containing host(s) on which you want to setup an iSCSI target.
    [iscsi_targets]
    192.168.34.51
    

    ansible_hosts.txt

  • Step 3: Create the ansible playbook with role for creating iSCSI target.
    Note: The role is not capable of creating new partitions nor logical volumes(LVs) and it expects you have your storage ready on the futuru iSCSI target. If you would like to setup LVs from ansible you can use the lvol module. Below is example of creating iSCSI target with 2 LUNs made of LVs and allowing one initiator(client) to access them. Role creates objects in way that access to all LUNs is granted to all initiators created by ansible role.

    ---
    - hosts: iscsi_targets
      roles:
        - { role: OndrejHome.targetcli }
      vars:
        iscsi_targets:
          - wwn: "iqn.1994-05.com.redhat:target"
            disks:
              - path: /dev/c7vg/LV1
                name: test1
                type: block
              - path: /dev/c7vg/LV2
                name: test2
                type: block
            initiators:
              - 'iqn.1994-05.com.redhat:client1'
    

    ansible_playbook.yml

  • Step 4: Run the ansible and wait for iSCSI target to get created.
    $ ansible-playbook -i ansible_hosts.txt ansible_playbook.yml
    

Custom modules for handling targetcli

The role contains 4 custom modules which are wrapping around command targetcli on the target systems. These modules supports detection, creation and deletion of objects in targetcli iSCSI configuration (check their documentation for more information). If you feel creative enough, this can be used also to manage the existing iSCSI targets if you can supply the correct data to modules.

As always, feel free to create issues on github if you encounter any issues in the above role or if you would like some special feature be included in it. Thanks to Amar Bessalah that contacted me via email and pointed out that this post contains outdated data and syntax. Post was updated to reflect current syntax as of 2020-04-27. Original post was published here on 2016-07-17.

Last change .