Ansible: DELL UnityXTでボリューム作ってRHEL8にLVM作ってマウントするまで

- name: install packages for iscsi
  package:
    name: '{{ item }}'
    state: present
  with_items:
    - iscsi-initiator-utils

- name: edit /etc/iscsi/iscsid.conf
  lineinfile:
    dest: /etc/iscsi/iscsid.conf
    regexp: "{{ item.regexp }}"
    line: "{{ item.line }}"
  with_items:
    - { regexp: '^node.startup\s+=', line: 'node.startup = automatic ' }
    - { regexp: '^node.session.timeo.replacement_timeout\s+=' , line: 'node.session.timeo.replacement_timeout = 60' }
    - { regexp: '^node.session.iscsi.InitialR2T\s+=' , line: 'node.session.iscsi.InitialR2T = Yes' }
    - { regexp: '^node.session.iscsi.ImmediateData\s+=' , line: 'node.session.iscsi.ImmediateData = No' }
  notify: restart iscsid

- name: enable iscsid service
  systemd:
    name: iscsid
    enabled: yes

- name: install packages for multipathd
  package:
    name: '{{ item }}'
    state: present
  with_items:
    - device-mapper-multipath

- name: check /etc/multipath.conf
  stat:
    path: "/etc/multipath.conf"
  register: chk_mpath

- name: execution command -- mpathconf --enable --with_multipathd y
  shell: mpathconf --enable --with_multipathd y
  when: chk_mpath.stat.exists == false

- name: Create Host with IP Address
  dellemc.unity.host:
    unispherehost: "{{unity.unispherehost}}"
    username: "{{unity.username}}"
    password: "{{unity_password}}"
    validate_certs: "{{unity.validate_certs}}" 
    host_name: "{{ host.name }}"
    host_os: "{{ host.os }}"
    network_address: "{{ item }}"
    network_address_state: present-in-host
    state: present
  with_items: "{{ host.network_address }}"
  delegate_to: localhost

- name: Get InitiatorName on Linux Host
  shell: cat /etc/iscsi/initiatorname.iscsi | sed 's/.*=//' 
  register: initiatorname
  when: host.os=="Linux"
  changed_when: false

- name: Add Initiator from initiatorname.iscsi on Linux Host 
  dellemc.unity.host:
    unispherehost: "{{unity.unispherehost}}"
    username: "{{unity.username}}"
    password: "{{unity_password}}"
    validate_certs: "{{unity.validate_certs}}" 
    host_name: "{{ host.name }}"
    initiators: "{{ initiatorname.stdout }}"
    initiator_state: present-in-host
    state: present
  when: host.os=="Linux"
  delegate_to: localhost

- name: check iscsi sessions
  shell: iscsiadm -m session | grep '{{ item }}'
  changed_when: false
  ignore_errors: yes
  with_items: '{{ unity.iscsi_ipaddresses }}'
  register: check_iscsi_sessions_result

- name: discovery iscsi targets
  shell: iscsiadm -m discovery -t sendtargets -p '{{ item }}'
  with_items: '{{ unity.iscsi_ipaddresses }}'
  when: check_iscsi_sessions_result.failed is true

- name: login iscsi targets
  shell: iscsiadm -m node --login -p '{{ item }}'
  with_items: '{{ unity.iscsi_ipaddresses }}'
  when: check_iscsi_sessions_result.failed is true

- name: Create Volumes on Unity
  dellemc.unity.volume:
    unispherehost: "{{unity.unispherehost}}"
    username: "{{unity.username}}"
    password: "{{unity_password}}"
    validate_certs: "{{unity.validate_certs}}" 
    vol_name: "{{ item.name }}"
    pool_name: "{{unity.defaultpool}}"
    size: "{{ item.size }}"
    cap_unit: "{{ item.cap_unit }}"
    host_name: "{{host.name}}"
    mapping_state: mapped
    state: present
  with_items: '{{ volumes }}'
  register: create_vol_result
  delegate_to: localhost

- name: get device path
  shell: multipath -ll | grep -i '{{ wwn }}' | awk '{print $1}'
  vars:
    wwn : "{{ create_vol_result | json_query(query) | json_query('[0]') | replace(':','') }}"
    query: "results[?volume_details.name=='{{item.name}}'].volume_details.wwn"
  with_items: '{{ volumes }}'
  register: device_path

- name: create vggroups
  lvg:
    vg: '{{ item.vg }}'
    pvs: "{{ '/dev/mapper/' ~ device_path_name }}"
    pesize: '{{ item.pesize }}'
  vars:
    device_path_name : "{{ device_path | json_query(query) | json_query('[0]') }}"
    query: "results[?item.name=='{{item.name}}'].stdout"
  with_items: '{{ volumes }}'

- name: create lvs
  lvol: 
    vg: "{{ item.0.vg }}"
    lv: "{{ item.1.name }}"
    size: "{{ item.1.size }}"
  with_subelements:
    - "{{ volumes }}"
    - lv

- name: create filesystems
  community.general.filesystem:
    fstype: "{{ item.1.fstype }}"
    dev: "{{ '/dev/' ~  item.0.vg ~ '/' ~ item.1.name }}"
  with_subelements:
    - "{{ volumes }}"
    - lv

- name: create mount points
  file:
    path: '{{ item.1.mountpath }}'
    state: directory
    owner: root
    group: root
    mode: 0777
  with_subelements:
    - "{{ volumes }}"
    - lv

- name: mount volumes
  mount:
    path: '{{ item.1.mountpath }}'
    src: "{{ '/dev/' ~  item.0.vg ~ '/' ~ item.1.name }}"
    fstype: "{{ item.1.fstype }}"
    opts:  "{{ item.1.mopts }}"
    state: mounted
  with_subelements:
    - "{{ volumes }}"
    - lv