【发布时间】:2014-10-09 04:29:36
【问题描述】:
我是 ansible 的新手,所以如果我的问题有点基本,请多多包涵。
场景:
我有几组远程主机,例如 [EPCs] [Clients] 和 [Testers] 我能够按照我想要的方式配置它们。
问题:
我需要编写一个剧本,当它运行时,它会在运行时向用户询问库存。 例如,当运行 playbook 时,应按以下方式提示用户: “输入您要配置的 EPC 数量” “输入您要配置的客户端数量” "输入您要配置的测试人员数量"
应该发生什么:
例如,现在用户分别输入 2,5 和 8。 现在 playbook 应该只处理组 [EPCs] 中的前 2 个节点、组 [Clients] 中的前 5 个节点和组 [Testers] 中的前 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)
任何帮助将不胜感激。谢谢!
【问题讨论】:
标签: variables ansible roles ansible-playbook inventory