首页 > 解决方案 > 接受用户输入并在多行中创建一个新文件

问题描述

我想从 vars_prompt 获取用户输入,例如:-

Enter names:- apple orange

并使用此输出在服务器上创建一个新文件:-

apple
orange

我如何使用 lineinfile 或 blockinfile 模块来实现这一点?

标签: ansibleansible-awx

解决方案


下面的剧本与输入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(' ') }}"

$ cat /tmp/fruits 
apple
orange

推荐阅读