Ansible-从列表中选择多个随机名称
我使用的是Ansible 2.10,并具有14个名称列表的变量。从此列表中,我想分配一个名称的随机数(从9-14),然后将其写入文本文件。我正在为如何做到这一点而苦苦挣扎。到目前为止,请有
in default.yml:
randomNames: 'chad', 'bob', 'alice'.....
- name: Define random number
set_fact:
numberNames: "{{ range(9,14) }} | random }}"
- name: Add random names to file
lineinfile:
src: {{ randomNames }}
dest: namesList.txt
loop:
"{{ numberNames }}"
一个错误:但是我遇到了“传递给'loop'的无效数据,它需要列表。我知道这不是做到这一点的正确方法,但是有人可以帮助我弄清楚什么是最好的 我应该补充的方法
,我还需要一个名称:
bob
alice
ted
....
I am using ansible 2.10 and have a variable with a list of 14 names. From this list, I want to assign a random number (from 9-14) of names and write this to a text file. I'm struggling with how to do this in ansible. So far, have this:
in default.yml:
randomNames: 'chad', 'bob', 'alice'.....
- name: Define random number
set_fact:
numberNames: "{{ range(9,14) }} | random }}"
- name: Add random names to file
lineinfile:
src: {{ randomNames }}
dest: namesList.txt
loop:
"{{ numberNames }}"
but I get an error "Invalid data passed to 'loop', it requires a list. I know this is isn't the correct way to do this, but can someone help me figure out what is the best way to do this?
I should add I also need the names, one per line:
bob
alice
ted
....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有多个问题。首先,这不是列表:
那只是一个字符串。如果您想要一个列表,请写:
或:(
我已经删除了报价,因为它们不是必需的,但是您可以编写
'chad'
,而不是chad
,等,如果您愿意)。其次,此任务...
...没有产生列表;它返回一个数字。因此,在随后的任务中,当您编写时:
您正在尝试循环浏览
10
之类的内容。最后,
lineInfile
模块没有src
参数。如果您的目标是将
n
随机名称写入文件,则可能需要>过滤器,将一个随机列表随机:
There are multiple problems here. First, this isn't a list:
That's just a string. If you want a list, write:
Or:
(I've dropped the quotes, because they're not necessary, but you can write
'chad'
instead ofchad
, etc, if you prefer).Second, this task...
...does not produce a list; it returns a single number. So in the subsequent task, when you write:
You're trying to loop over something like the value
10
.Lastly, the
lineinfile
module doesn't have asrc
parameter.If your goal is to write
N
random names to a file, you might wantthe
shuffle
filter, which randomizes a list: