如何根据用户输入在 ansible 剧本中添加主机?
How add hosts from user's input in a ansible playbook?
我需要根据用户的输入添加主机。现在我正在尝试使用 ansible in-memory inventory
、add_host
模块和 prompt
添加目标主机以执行剩余的任务。这是我剧本的内容:
Deploy.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
我怎样才能做我需要做的事?
更新:
还是不行。这是我剧本的内容:
Deploy.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
Here is a flowchart of how the playbook should works:
+------------------+ +---------------+ +-----------------+
|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
属性使用的任何变量中。
您需要在 vars_prompt
下切换 name
和 prompt
值
还有 YAML 格式问题
例如:
- vars_prompt:
- name: "Server IP"
prompt: "Server"
private: no
应该是:
vars_prompt:
- name: "server"
prompt: "Server IP"
private: no
然后你可以在你的任务中引用{{ server }}
变量
你的 ansible 脚本有问题。
vars_prompt:
从 vars_prompt
行中删除 -
它将正常工作。
我在我的本地服务器上试过同样的脚本工作正常。
- 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: Passwd is defined
- name: Create a file
shell: touch newfile
delegate_to: "{{ Server }}"
在上次任务中更新你的任务运行吧。
- name: Create a file
shell: touch newfile
delegate_to: "{{ Server }}"
我需要根据用户的输入添加主机。现在我正在尝试使用 ansible in-memory inventory
、add_host
模块和 prompt
添加目标主机以执行剩余的任务。这是我剧本的内容:
Deploy.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
我怎样才能做我需要做的事?
更新:
还是不行。这是我剧本的内容:
Deploy.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
Here is a flowchart of how the playbook should works:
+------------------+ +---------------+ +-----------------+ |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
属性使用的任何变量中。
您需要在 vars_prompt
name
和 prompt
值
还有 YAML 格式问题
例如:
- vars_prompt:
- name: "Server IP"
prompt: "Server"
private: no
应该是:
vars_prompt:
- name: "server"
prompt: "Server IP"
private: no
然后你可以在你的任务中引用{{ server }}
变量
你的 ansible 脚本有问题。
vars_prompt:
从 vars_prompt
行中删除 -
它将正常工作。
我在我的本地服务器上试过同样的脚本工作正常。
- 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: Passwd is defined
- name: Create a file
shell: touch newfile
delegate_to: "{{ Server }}"
在上次任务中更新你的任务运行吧。
- name: Create a file
shell: touch newfile
delegate_to: "{{ Server }}"