Search code examples
ansibleansible-awx

Take user input and make a new file in multiple lines


I want to take a user input from vars_prompt like:-

Enter names:- apple orange

and make a new file on a server with this output:-

apple
orange

how can i achieve this using lineinfile or blockinfile modules?


Solution

  • The playbook below with the input Enter names:- apple orange

    - hosts: localhost
      vars_prompt:
        - name: fruits
          prompt: "Enter names"
          private: no
      tasks:
        - file:
            path: /tmp/fruits
            state: absent
        - lineinfile:
            path: /tmp/fruits
            create: yes
            line: "{{ item }}"
          loop: "{{ fruits.split(' ') }}"
    

    gives

    $ cat /tmp/fruits 
    apple
    orange