ansible-role-for-splunk
                                
                                 ansible-role-for-splunk copied to clipboard
                                
                                    ansible-role-for-splunk copied to clipboard
                            
                            
                            
                        Running configure_shc_captain.yml multiple times yields different results
Ansible tasks should be idempotent. If the search head cluster already had a captain this task should be able to figure that out and skip.
Need to run the show shcluster-status command and if captain already elected do not run the init captain command again.
That is what I do now
- name: Check If Captain is Already Elected
  hosts:
    - shc
  run_once: true
  tasks:
  - name: Get SHC Captain Status
    shell: "/opt/splunk/bin/splunk show shcluster-status -auth {{ splunk_admin_username }}:{{ splunk_admin_password }} | grep label | head -n1 | cut -d ':' -f2 | xargs"
    become: true
    become_user: "{{ splunk_nix_user }}"
    register: shc_response
    no_log: false
    until: shc_response.stdout != "" and shc_response.rc == 0
    retries: 10
    delay: 5
    ignore_errors: yes
  - name: Setting SHC Captain Fact
    set_fact: 
      shc_captain: "{{ shc_response.stdout }}"
It could take a few minutes after restart for this to detect if a captain has been elected already.
Yeah I also ran into timing issues with that. I ended up checking if server.conf shclustering has the id option using btools to work out if the bootstrap captain command is already run.