Ansible-从列表中选择多个随机名称

发布于 2025-02-03 05:13:43 字数 577 浏览 3 评论 0原文

我使用的是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑咖 2025-02-10 05:13:43

这里有多个问题。首先,这不是列表:

randomNames: 'chad', 'bob', 'alice'

那只是一个字符串。如果您想要一个列表,请写:

randomNames: [chad, bob, alice]

或:(

randomNames:
  - chad
  - bob
  - alice

我已经删除了报价,因为它们不是必需的,但是您可以编写'chad',而不是chad,等,如果您愿意)。


其次,此任务...

- 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 }}"

您正在尝试循环浏览10之类的内容。


最后,lineInfile模块没有src参数。

如果您的目标是将n随机名称写入文件,则可能需要
>过滤器,将一个随机列表随机:

- hosts: localhost
  gather_facts: false
  vars:
    "randomNames": [
      "Bob", "Alice", "James", "Robert", "John", "Michael", "David", "William", "Richard",
      "Joseph", "Thomas", "Charles", "Christopher", "Daniel", "Matthew", "Anthony", "Mark",
      "Donald", "Steven", "Paul", "Andrew", "Joshua", "Mary", "Patricia", "Jennifer",
      "Linda", "Elizabeth", "Barbara", "Susan", "Jessica", "Sarah", "Karen", "Lisa",
      "Nancy", "Betty", "Margaret", "Sandra", "Ashley", "Kimberly", "Emily", "Donna",
      "Michelle"]

  tasks:
    - name: Define random number
      set_fact:
        numberNames: "{{ range(9,14) | random }}"

    - name: Add a random name to a file
      lineinfile:
        path: namesList.txt
        line: "{{ item }}"
      loop: "{{ (randomNames|shuffle)[:numberNames|int] }}"

There are multiple problems here. First, this isn't a list:

randomNames: 'chad', 'bob', 'alice'

That's just a string. If you want a list, write:

randomNames: [chad, bob, alice]

Or:

randomNames:
  - chad
  - bob
  - alice

(I've dropped the quotes, because they're not necessary, but you can write 'chad' instead of chad, etc, if you prefer).


Second, this task...

- name: Define random number
  set_fact:
    numberNames:  "{{ range(9,14) }} | random }}"

...does not produce a list; it returns a single number. So in the subsequent task, when you write:

- name: Add random names to file
  lineinfile:
    src: {{ randomNames }}
    dest: namesList.txt
  loop: "{{ numberNames }}"

You're trying to loop over something like the value 10.


Lastly, the lineinfile module doesn't have a src parameter.

If your goal is to write N random names to a file, you might want
the shuffle filter, which randomizes a list:

- hosts: localhost
  gather_facts: false
  vars:
    "randomNames": [
      "Bob", "Alice", "James", "Robert", "John", "Michael", "David", "William", "Richard",
      "Joseph", "Thomas", "Charles", "Christopher", "Daniel", "Matthew", "Anthony", "Mark",
      "Donald", "Steven", "Paul", "Andrew", "Joshua", "Mary", "Patricia", "Jennifer",
      "Linda", "Elizabeth", "Barbara", "Susan", "Jessica", "Sarah", "Karen", "Lisa",
      "Nancy", "Betty", "Margaret", "Sandra", "Ashley", "Kimberly", "Emily", "Donna",
      "Michelle"]

  tasks:
    - name: Define random number
      set_fact:
        numberNames: "{{ range(9,14) | random }}"

    - name: Add a random name to a file
      lineinfile:
        path: namesList.txt
        line: "{{ item }}"
      loop: "{{ (randomNames|shuffle)[:numberNames|int] }}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文