Cry*_*Neo 5 ansible ansible-2.x ansible-inventory
我需要根据用户的输入添加主机。现在我尝试使用 ansiblein-memory inventory模块add_host并prompt添加目标主机来执行剩余的任务。这是我的剧本的内容:
部署.yml
Run Code Online (Sandbox Code Playgroud)- 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
Run Code Online (Sandbox Code Playgroud)
我不知道为什么它会抛出这个错误:
ERROR! the field 'hosts' is required but was not set
Run Code Online (Sandbox Code Playgroud)
我怎样才能做我需要做的事情?
它仍然不起作用。这是我的剧本的内容:
部署.yml
Run Code Online (Sandbox Code Playgroud)- 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
Run Code Online (Sandbox Code Playgroud)
我不知道为什么它会抛出这个错误:
ERROR! The field 'hosts' has an invalid value, which includes an undefined variable. The error was: 'Server' is undefined
Run Code Online (Sandbox Code Playgroud)
以下是该剧本应如何工作的流程图:
Run Code Online (Sandbox Code Playgroud)+------------------+ +---------------+ +-----------------+ |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
Run Code Online (Sandbox Code Playgroud)
===旧答案===
用户输入存储在您name为每个变量提示中的属性使用的任何变量中。
您需要在以下情况下切换您的name和prompt值vars_prompt
还有 YAML 格式问题
例如:
- vars_prompt:
- name: "Server IP"
prompt: "Server"
private: no
Run Code Online (Sandbox Code Playgroud)
应该:
vars_prompt:
- name: "server"
prompt: "Server IP"
private: no
Run Code Online (Sandbox Code Playgroud)
{{ server }}然后您可以在任务中引用该变量
| 归档时间: |
|
| 查看次数: |
6146 次 |
| 最近记录: |