Search code examples
ansibleansible-2.xansible-inventory

How declare two prompt variables for hosts in ansible playbook


How declare two prompt variables for hosts in ansible playbook, I tried below Playbook but without luck. ............................................................................................................................................................ Thank you in advance.

---
- name: MD5 File Check 
  gather_facts: false
  hosts: "{{ cluster_host_1 }}", "{{cluster_host_2 }}" 
  hosts: localhost
  remote_user: sv_operator
  vars_prompt:
    - name: "file_1"
      prompt: "File name"
      private: no
    - name: "cluster_host_1"
      prompt: "Enter 1st Host name"
      private: no
    - name: "cluster_host_2"
      prompt: "Enter 2nd Host Name"
      private: no
  tasks:
    - stat:
        path: "/tmp/{{ file_1 }}"
        checksum_algorithm: sha256
      register: output
      delegate_to: "{{ cluster_host_1 }}" 
    - debug:
        msg: "{{ output.stat.checksum }}"
    - stat:
        path: "/tmp/{{ file_2 }}"
        checksum_algorithm: sha256
      register: output_
      delegate_to: "{{ cluster_host_2 }}"
    - debug:
        msg: "{{ output_.stat.checksum }}"

Solution

  • Given the remote hosts and /tmp/file1

    shell> ssh admin@test_01 sha256 /tmp/file1
    SHA256 (/tmp/file1) = e2611a1fac7fc2ab99d2e792ad84f34e66740d6a3d77b97b4da39a3758357da0
    
    shell> ssh admin@test_02 sha256 /tmp/file1
    SHA256 (/tmp/file1) = 109f60103192b5c8f4e33c26b4f9c7b94489bf8de0325497b7f5a0668dc1a402
    

    The playbook below

    shell> cat playbook.yml
    - name: SHA256 File Check 
      hosts: localhost
      gather_facts: false
      vars_prompt:
        - name: "file_1"
          prompt: "File name"
          private: no
        - name: "cluster_host_1"
          prompt: "Enter 1st Host name"
          private: no
        - name: "cluster_host_2"
          prompt: "Enter 2nd Host Name"
          private: no
      tasks:
        - stat:
            path: "/tmp/{{ file_1 }}"
            checksum_algorithm: sha256
          register: output
          delegate_to: "{{ cluster_host_1 }}" 
        - debug:
            msg: "{{ output.stat.checksum }}"
        - stat:
            path: "/tmp/{{ file_1 }}"
            checksum_algorithm: sha256
          register: output_
          delegate_to: "{{ cluster_host_2 }}"
        - debug:
            msg: "{{ output_.stat.checksum }}"
    

    works as expected

    shell> ansible-playbook playbook.yml
    File name: file1
    Enter 1st Host name: test_01
    Enter 2nd Host Name: test_02
    
    PLAY [SHA256 File Check] ****
    
    TASK [stat] ****
    ok: [localhost -> test_01]
    
    TASK [debug] ****
    ok: [localhost] => 
      msg: e2611a1fac7fc2ab99d2e792ad84f34e66740d6a3d77b97b4da39a3758357da0
    
    TASK [stat] ****
    ok: [localhost -> test_02]
    
    TASK [debug] ****
    ok: [localhost] => 
      msg: 109f60103192b5c8f4e33c26b4f9c7b94489bf8de0325497b7f5a0668dc1a402
    
    PLAY RECAP ****
    localhost: ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0