---
- name: Run anonkneti to get esn for "{{ item.ip }}"
  ansible.builtin.command: /opt/nfast/bin/anonkneti "{{ item.ip }}"
  register: anonkneti

- name: Output of anonkneti
  ansible.builtin.debug:
    var: anonkneti.stdout_lines

- name: Get the esn
  ansible.builtin.set_fact:
    esn: "{{ anonkneti.stdout_lines[0].split()[0] }}"

- name: "Adding client IPs to config for HSM {{ esn }}"
  ansible.builtin.script: "{{ role_path }}/files/add_ips.py --config-dir /opt/nfast/kmdata/hsm-{{ esn }}/config --ips {{ thales_client_ips }}"

- name: Push new config to the HSM and check that it was successful
  ansible.builtin.shell: |
    /opt/nfast/bin/cfg-pushnethsm -a {{ item.ip }} /opt/nfast/kmdata/hsm-{{ esn }}/config/config.new && \
    sleep 5 && \
    diff /opt/nfast/kmdata/hsm-{{ esn }}/config/config /opt/nfast/kmdata/hsm-{{ esn }}/config/config.new
  register: result
  until: result.rc == 0
  retries: 10
