Ansible Playbook Error – Unexpected Parameter Type in Action

ansibleansible-playbook

I have my playbook yml script below:

---
- name: Download files from FTP
  hosts: localhost
  gather_facts: false

  vars_prompt:
    - name: target_host
      prompt: "Target Host IP: "
    - name: auth_method
      prompt: "Authentication method (password/key): "

  tasks:
    - name: Gather target host credentials
      when: auth_method == 'password'
      vars_prompt:
        - name: ssh_user
          prompt: "SSH Username: "
        - name: ssh_pass
          prompt: "SSH Password: "
          private: true

    - name: Gather key path for target host
      when: auth_method == 'key'
      vars_prompt:
        - name: ssh_user
          prompt: "SSH Username: "
        - name: key_path
          prompt: "Path to SSH Key: "

    - name: Gather FTP credentials
      vars_prompt:
        - name: ftp_username
          prompt: "FTP Username: "
        - name: ftp_password
          prompt: "FTP Password: "
          private: true

- name: Download files from FTP on target host
  hosts: "{{ target_host }}"
  gather_facts: false
  remote_user: "{{ ssh_user }}"
  become: true
  become_user: root
  become_method: sudo

  tasks:
    - name: Create upgrade directory if not exist
      file:
        path: /root/upgrade
        state: directory

    - name: Download file from FTP
      get_url:
        url: "ftp://something.com/{{ file_path }}"
        dest: "/root/upgrade/{{ file_path }}"
        force: yes
        timeout: 30
        remote_src: yes
        validate_certs: false
        url_username: "{{ ftp_username }}"
        url_password: "{{ ftp_password }}"
      register: download_result
      ignore_errors: true

    - name: Check if file downloaded successfully
      debug:
        msg: "File '{{ file_path }}' downloaded successfully."
      when: download_result is success

    - name: Display error message if file download failed
      debug:
        msg: "Failed to download file '{{ file_path }}'. Please check the file path or FTP credentials."
      when: download_result is failed and file_path != 'q'

    - name: Exit playbook if 'q' is pressed
      meta: end_play
      when: file_path == 'q'

I have checked the indentation and everything seems to be correct from my understanding. But it shows the below error when I try to run it:

root@DESKTOP-VLCQSJO:/home/ridhoswasta/tassta-automation# ansible-playbook download-files.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match
'all'
ERROR! unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>

The error appears to be in '/home/ridhoswasta/tassta-automation/download-files.yml': line 13, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
    - name: Gather target host credentials
      ^ here
root@DESKTOP-VL

Any advice what could be wrong?
Thanks!

Best Answer

vars_prompt can be used in a play only. No tasks, blocks, or roles. See Playbook Keywords. In a task, you can use pause instead. For example,

shell> cat pb.yml
- hosts: localhost

  vars_prompt:

    - name: target_host
      prompt: Target Host IP
      private: false
    - name: auth_method
      prompt: Authentication method (password/key)
      private: false

  vars:

    ssh_user: "{{ ssh_user_out.user_input }}"
    ssh_pass: "{{ ssh_pass_out.user_input }}"
    
  tasks:

    - block:
        - pause:
            prompt: SSH Username
          register: ssh_user_out
        - pause:
            prompt: SSH Password
            echo: false
          register: ssh_pass_out
        - debug:
            msg: |
              target_host: {{ target_host }}
              auth_method: {{ auth_method }}
              ssh_user: {{ ssh_user }}
              ssh_pass: {{ ssh_pass }}
      when: auth_method == 'password'

gives

shell> ansible-playbook pb.yml 
Target Host IP: 10.1.0.10
Authentication method (password/key): password

PLAY [localhost] ******************************************************************************

TASK [pause] **********************************************************************************
[pause]
SSH Username:
admin^Mok: [localhost]

TASK [pause] **********************************************************************************
[pause]
SSH Password (output is hidden):
ok: [localhost]

TASK [debug] **********************************************************************************
ok: [localhost] => 
  msg: |-
    target_host: 10.1.0.10
    auth_method: password
    ssh_user: admin
    ssh_pass: 1234

PLAY RECAP ************************************************************************************
localhost: ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0