ansible - 如何在 ansible playbook 中从用户输入中添加主机?
问题描述
我需要从用户的输入中添加一个主机。现在我正在尝试使用 ansiblein-memory inventory
模块add_host
并prompt
添加目标主机来执行剩余的任务。这是我的剧本的内容:
部署.yml
- name: Adding the host server hosts: localhost - vars_prompt: - name: "Server IP" prompt: "Server" private: no - name: "Username (default: Ubuntu)" prompt: "User" default: "Ubuntu" private: no - name: "Password" prompt: "Passwd" private: yes encrypt: "sha512_crypt" - name: "Identity file path" prompt: "IdFile" private: no when: Passwd is undefined tasks: - name: Add host server add_host: name: "{{ Server }}" ansible_ssh_user: "{{ User }}" ansible_ssh_private_key_file: "{{ IdFile }}" when: IdFile is defined - name: Add host server add_host: name: "{{ Server }}" ansible_ssh_user: "{{ User }}" ansible_ssh_pass: "{{ Passwd }}" when: Passwd is defined - hosts: "{{ Server }}" tasks: - name: Copy the script file to the server copy: src: script.sh dest: "{{ ansible_env.HOME }}/folder/" mode: 755 force: yes attr: - +x
当我用这个命令运行这个剧本时$ ansible-playbook Deploy.yml
,输出是:
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [Adding the host server] ***********************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************
ok: [localhost]
Server: <server-ip>
User [Ubuntu]:
Passwd:
IdFile: <path/to/id/file>
ERROR! the field 'hosts' is required but was not set
我不知道为什么会抛出这个错误:
ERROR! the field 'hosts' is required but was not set
我该如何做我需要做的事情?
更新:
它仍然无法正常工作。这是我的剧本的内容:
部署.yml
- name: Adding the host server hosts: localhost vars_prompt: - name: "Server" prompt: "Server IP" private: no - name: "User" prompt: "Username" default: "Ubuntu" private: no - name: "Passwd" prompt: "Password" private: yes encrypt: "sha512_crypt" - name: "IdFile" prompt: "Identity file path" private: no when: Passwd is undefined tasks: - name: Add host server add_host: name: "{{ Server }}" ansible_ssh_user: "{{ User }}" ansible_ssh_private_key_file: "{{ IdFile }}" when: IdFile is defined - name: Add host server add_host: name: "{{ Server }}" ansible_ssh_user: "{{ User }}" ansible_ssh_pass: "{{ Passwd }}" when: IdFile is undefined - hosts: "{{ Server }}" tasks: - name: Copy the script file to the server copy: src: script.sh dest: "{{ ansible_env.HOME }}/folder/" mode: 755 force: yes attr: - +x
当我用这个命令运行这个剧本时$ ansible-playbook Deploy.yml
,输出是:
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [Adding the host server] ***********************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************
ok: [localhost]
Server IP: <server-ip>
Username [Ubuntu]:
Password:
Identity file path: <path/to/id/file>
ERROR! the field 'hosts' is required but was not set
我不知道为什么会抛出这个错误:
ERROR! The field 'hosts' has an invalid value, which includes an undefined variable. The error was: 'Server' is undefined
这是剧本应该如何工作的流程图:
+------------------+ +---------------+ +-----------------+ |Use ansible to run| |Get host IP fom| |Get ssh User from| | this playbook +---->+ user's input +---->+ user's input | +------------------+ +---------------+ +--------+--------+ | v +------------+--------+ |Get ssh password from| | user's input | +------------+--------+ | v +---------------+ ************************* |Add a host with| Yes | Did the user inputted | v----------+ password +<---+| a password? | +----------------+ +---------------+ ***************+********* ||Run some tasks|| |No ||in recently || v ||added host || +---------------+ +------------+--------+ +----------------+ |Add a host with| |Get ssh identity file| ^----------+ identity file +<------+ from user's input | +---------------+ +---------------------+
解决方案
好的,我已经更新了我的答案以适应您问题的变化,但由于历史原因留下了原始答案。
为了解决您看到的替换错误,这会导致您的第二次播放中出现空主机列表,我将改为使用库存组。
第二部戏还有另外两个语法错误
- 文件模式需要是八进制(即
0700
) - 该属性无效。我的假设是您正在尝试使文件可执行,因此请修复文件模式并删除该属性。
这是一个更新的剧本:
- name: Adding the host server
hosts: localhost
vars_prompt:
- name: "Server"
prompt: "Server IP"
private: no
- name: "User"
prompt: "Username"
default: "Ubuntu"
private: no
- name: "Passwd"
prompt: "Password"
private: yes
encrypt: "sha512_crypt"
- name: "IdFile"
prompt: "Identity file path"
private: no
when: Passwd is undefined
tasks:
- name: Add host server
add_host:
name: "{{ Server }}"
ansible_ssh_user: "{{ User }}"
ansible_ssh_private_key_file: "{{ IdFile }}"
group: added_hosts
when: IdFile is defined
- name: Add host server
add_host:
name: "{{ Server }}"
ansible_ssh_user: "{{ User }}"
ansible_ssh_pass: "{{ Passwd }}"
group: added_hosts
when: IdFile is undefined
- hosts: added_hosts
tasks:
- name: Copy the script file to the server
copy:
src: script.sh
dest: "{{ ansible_env.HOME }}/folder/"
mode: 0755
force: yes
=== 旧答案 ===
用户输入存储在您为name
每个变量提示中的属性使用的任何变量中。
您需要在下切换您的name
和prompt
值vars_prompt
还有 YAML 格式问题
例如:
- vars_prompt:
- name: "Server IP"
prompt: "Server"
private: no
应该:
vars_prompt:
- name: "server"
prompt: "Server IP"
private: no
然后你可以在你的任务中引用{{ server }}
变量
推荐阅读
- c - 如何使用 _mm_clflushopt 函数编译程序?错误:内联失败
- r - 如何在数据框中行追加具有相同列名的列
- unity3d - 从 Unity 中的另一个脚本访问 public bool
- r - 如何在R中关闭打开的Excel文件
- ios - 为 iOS 运行 Nativescript OCR 插件时的问题
- php - php头像图片和真实图片显示
- c# - GroupBy 多列并查找重复项
- redux - NgRx(或 Redux)实际上在哪里存储数据?
- winforms - 在 Windows 窗体上,如何根据多对多关系填充数据网格?
- javascript - 代理仅在尝试返回不同值时通过 get 方法返回目标