ansible - 接受用户输入并在多行中创建一个新文件
问题描述
我想从 vars_prompt 获取用户输入,例如:-
Enter names:- apple orange
并使用此输出在服务器上创建一个新文件:-
apple
orange
我如何使用 lineinfile 或 blockinfile 模块来实现这一点?
解决方案
下面的剧本与输入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
推荐阅读
- reactjs - 使用库时,类型“元素”不可分配给类型 ReactChild[]
- amazon-cloudfront - Cloudfront 缓存行为应用不正确
- html - 如何很好地设置 html 样式?
- javascript - 事件循环是否与 JS 主线程在同一线程上运行?
- javascript - Modal通过点击调用,但立即消失(Next.js)
- swift - 快速替换 PDF 中的图像。如何获取页面中的图像位置?
- laravel - 仅当用户是管理员时 Laravel Fortify 登录
- python - 创建表并从 CSV 文件复制数据
- windows - 使用 winautomation 进程本身在 winautomation 中暂停/播放进程
- laravel - 如何对忽略数组中的当前 id 的数组应用 laravel 唯一验证?