我对ansible很新,所以如果我的问题有点基础,请耐心等待。
情景:
我有一些远程主机,例如[EPC] [客户端]和[测试人员] 我能够按照我想要的方式配置它们。
问题:
我需要编写一个剧本,在运行时,会在运行时向用户询问库存。 作为运行剧本的示例,应该以下列方式提示用户: “输入您要配置的EPC数量” “输入您要配置的客户端数量” “输入您要配置的测试人员数量”
会发生什么:
现在用户分别输入2,5和8。 现在,剧本应仅解决组[EPC]中的前2个节点,组[客户端]中的前5个节点以及组[测试人员]中的前7个节点。 我不想创建大量的子组,例如,如果我有20个EPC,那么我不想为我的EPC定义20个组,我想要一些动态库存,它应该自动配置根据用户在运行时输入的机器使用 vars_prompt 选项或类似的东西
让我发布我的剧本的部分内容,以便更好地了解将要发生的事情:
---
- hosts: epcs # Now this is the part where I need a lot of flexibility
vars_prompt:
name: "what is your name?"
quest: "what is your quest?"
gather_facts: no
tasks:
- name: Check if path exists
stat: path=/home/khan/Desktop/tobefetched/file1.txt
register: st
- name: It exists
debug: msg='Path existence verified!'
when: st.stat.exists
- name: It doesn't exist
debug: msg="Path does not exist"
when: st.stat.exists == false
- name: Copy file2 if it exists
fetch: src=/home/khan/Desktop/tobefetched/file2.txt dest=/home/khan/Desktop/fetched/ flat=yes
when: st.stat.exists
- name: Run remotescript.sh and save the output of script to output.txt on the Desktop
shell: cd /home/imran/Desktop; ./remotescript.sh > output.txt
- name: Find and replace a word in a file placed on the remote node using variables
shell: cd /home/imran/Desktop/tobefetched; sed -i 's/{{name}}/{{quest}}/g' file1.txt
tags:
- replace
@gli我尝试了你的解决方案,我的库存中有一个名为test的组,其中包含两个节点。当我输入 0..1 时,我得到:
TASK: [echo sequence] *********************************************************
changed: [vm2] => (item=some_prefix0)
changed: [vm1] => (item=some_prefix0)
changed: [vm1] => (item=some_prefix1)
changed: [vm2] => (item=some_prefix1)
同样,当我输入 1..2 时,我得到:
TASK: [echo sequence] *********************************************************
changed: [vm2] => (item=some_prefix1)
changed: [vm1] => (item=some_prefix1)
changed: [vm2] => (item=some_prefix2)
changed: [vm1] => (item=some_prefix2)
同样,当我输入 4..5 时(库存中甚至没有节点,我得到:
TASK: [echo sequence] *********************************************************
changed: [vm1] => (item=some_prefix4)
changed: [vm2] => (item=some_prefix4)
changed: [vm1] => (item=some_prefix5)
changed: [vm2] => (item=some_prefix5)
任何帮助都会非常感激。谢谢!
答案 0 :(得分:11)
您应该使用vars_prompt来获取用户的信息,add_host以便动态更新主机,并使用with_sequence for循环:
$ cat aaa.yml
---
- hosts: localhost
gather_facts: False
vars_prompt:
- name: range
prompt: Enter range of EPCs (e.g. 1..5)
private: False
default: "1"
pre_tasks:
- name: Set node id variables
set_fact:
start: "{{ range.split('..')[0] }}"
stop: "{{ range.split('..')[-1] }}"
- name: "Add hosts:"
add_host: name="host_{{item}}" groups=just_created
with_sequence: "start={{start}} end={{stop}} "
- hosts: just_created
gather_facts: False
tasks:
- name: echo sequence
shell: echo "cmd"
输出将是:
$ ansible-playbook aaa.yml -i' localhost,'
Enter range of EPCs (e.g. 1..5) [1]: 0..1
PLAY [localhost] **************************************************************
TASK: [Set node id variables] *************************************************
ok: [localhost]
TASK: [Add hosts:] ************************************************************
ok: [localhost] => (item=0)
ok: [localhost] => (item=1)
PLAY [just_created] ***********************************************************
TASK: [echo sequence] *********************************************************
fatal: [host_0] => SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue
fatal: [host_1] => SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue
FATAL: all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit @/Users/gli/aaa.retry
host_0 : ok=0 changed=0 unreachable=1 failed=0
host_1 : ok=0 changed=0 unreachable=1 failed=0
localhost : ok=2 changed=0 unreachable=0 failed=0
在这里,由于host_0和host_1无法访问,它失败了,因为它可以正常工作。
不过,我使用了更强大的概念"节点范围"。如果你不需要它,那么让#34; start = 0"并且只要求"停止"提示中的值。答案 1 :(得分:2)
我认为您无法在运行时定义广告资源。您可以做的一件事是,在Ansible上编写一个包装器脚本,它首先会提示用户输入主机,然后动态构建一个ansible-playbook命令。
我更喜欢使用python执行此操作,但您可以使用您选择的任何语言。
$ cat ansible_wrapper.py
import ConfigParser
import os
nodes = ''
inv = {}
hosts = raw_input("Enter hosts: ")
hosts = hosts.split(",")
config = ConfigParser.ConfigParser(allow_no_value=True)
config.readfp(open('hosts'))
sections = config.sections()
for i in range(len(sections)):
inv[sections[i]] = hosts[i]
for key, value in inv.items():
for i in range(int(value)):
nodes = nodes + config.items(key)[i][0] + ";"
command = 'ansible-playbook -i hosts myplaybook.yml -e "nodes=%s"' % (nodes)
print "Running command: ", command
os.system(command)
注意:我尝试仅使用python2.7
运行此脚本