Search code examples
ansiblejinja2ansible-2.xansible-template

Using templates and vars_prompt, conflicting action statements


I am using ansible templates to create a file in which I need a variable introduced through prompt so I try to do the following:

- name: task1
     template:
        src: template.yaml.j2
        dest: ~/mypath/file.yaml
        owner: ubuntu
        group: ubuntu
        mode: 0644
     vars_prompt:
        - name: myvariable
          prompt: "Introduce your variable"
          private: yes

That code fragment is written in the /tasks/main.yml file inside an ansible role

When I execute the main playbook I get the following error:

ERROR! conflicting action statements: template, vars_prompt

Maybe that options are not compatbile but I tried to include a single var without prompt (using vars instead of vars_prompt) and it's working correctly

Is there any options to use the prompt var for templates??


Solution

  • vars_prompt is a keyword of the play, i.e. it must be placed in the playbook, on the same level as hosts, tasks or roles.

    Later you can access and use the varaible filled by user input.
    In your example, the data entered by the user is then in the variable myvariable, so you can use it with "{{ myvariable }}" for example.

    vars_prompt must not be defined inside a task.

    More about vars_prompt and interactive input in the Ansible Docs.