Not able to run ansible playbook , getting error with passing input file and taking input from user using assert module

Hello All,

I am trying to pass a file (VAULT-PASS.YML) using Ansible built in module vars and taking input from user and then comparing it using assert module , If the input matches with the content given in file vault-pass.yml then it will prompt success message and execute application deployment and service file which will run in a pod in Kubernetes.

- name: Execute Task
  hosts: localhost
  become: true
  tasks:
  - name: Load Secrets
    ansible.builtin.include_vars:
      file: /home/xyz/vault-pass.yml
  - name: Prompt for Credentials
    vars_prompt:
      - name: entered_username
        prompt: "Enter the username"
        private: no
      - name: entered_password
        prompt: "Enter the password"
        private: yes

  - name: Verify Credentials
    assert:
      that:
        - entered_username == "secret_username"
        - entered_password == "secret_password"
      fail_msg: "Invalid credentials. Deployment aborted."
      success_msg: "Valid credentials. Proceeding with deployment."

  - name: Include Kubernetes Deployment
    include_tasks: basicapp-deployment.yml
    when: entered_username == secret_username and entered_password == secret_password

  - name: Include Kubernetes Service
    include_tasks: basic-service.yml
    when: entered_username == secret_username and entered_password == secret_password

Getting below error
ERROR! unexpected parameter type in action: <class ‘ansible.parsing.yaml.objects.AnsibleSequence’>

The error appears to be in ‘/home/jatin/execute.yml’: line 5, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

tasks:

  • name: Prompt for Credentials
    ^ here
Below is my vault-pass.yml file content 

secret_username:abcd
secret_password:Welcome@12345

@jatin821 Welcome to community!

The vars_prompt is the play level keyword not module.

Therefore, we should vars_prompt as follows:

- name: Execute Task
  hosts: localhost
  become: true

  vars_prompt:
    - name: entered_username
      prompt: "Enter the username"
      private: false
    - name: entered_password
      prompt: "Enter the password"
      private: true

  tasks:
    - name: Load Secrets
      ansible.builtin.include_vars:
        file: vault-pass.yml

    - name: Verify Credentials
      ansible.builtin.assert:
        that:
          - entered_username == secret_username
          - entered_password == secret_password
        fail_msg: "Invalid credentials. Deployment aborted."
        success_msg: "Valid credentials. Proceeding with deployment."

There are two other points I noticed.

  1. It does not need quotation for variable name in that option.
# corret
        - entered_username == secret_username
        - entered_password == secret_password
  1. It needs a space after colon in vault-pass.yml
# correct
secret_username: abcd
secret_password: Welcome@12345
6 Likes

Thank you Akira for the feedback, I will try making those changes and execute the playbook.

1 Like