如何在计数之前添加一个字符串并使用ansible创建列表?
How to add a string before count and create list using ansible?
我需要创建 10 个用户,每个用户有 5 个文件夹。
首先我尝试创建如下用户
- set_fact:
user_lists: []
- name: creating list of users
set_fact:
user_lists: "user-{{ user_lists + [ item] }}"
with_sequence: count=10
我希望用户能够像 user-1 、user-2 ..user-10 这样创建。
但它会抛出如下错误
fatal: [localhost]: FAILED! => {"msg": "Unexpected templating type
error occurred on (user-{{ user_lists + [item] }}): coercing to
Unicode: need string or buffer, list found"}
如何解决?
参见with_sequience。例如下面的剧本
hosts: localhost
tasks:
- set_fact:
user_lists: []
- name: creating list of users
set_fact:
user_lists: "{{ user_lists + [ 'user-' ~ item ] }}"
with_sequence: start=1 end=10
- debug:
var: user_lists
给出(删节)
"user_lists": [
"user-1",
"user-2",
"user-3",
"user-4",
"user-5",
"user-6",
"user-7",
"user-8",
"user-9",
"user-10"
]
我需要创建 10 个用户,每个用户有 5 个文件夹。
首先我尝试创建如下用户
- set_fact:
user_lists: []
- name: creating list of users
set_fact:
user_lists: "user-{{ user_lists + [ item] }}"
with_sequence: count=10
我希望用户能够像 user-1 、user-2 ..user-10 这样创建。 但它会抛出如下错误
fatal: [localhost]: FAILED! => {"msg": "Unexpected templating type error occurred on (user-{{ user_lists + [item] }}): coercing to Unicode: need string or buffer, list found"}
如何解决?
参见with_sequience。例如下面的剧本
hosts: localhost
tasks:
- set_fact:
user_lists: []
- name: creating list of users
set_fact:
user_lists: "{{ user_lists + [ 'user-' ~ item ] }}"
with_sequence: start=1 end=10
- debug:
var: user_lists
给出(删节)
"user_lists": [
"user-1",
"user-2",
"user-3",
"user-4",
"user-5",
"user-6",
"user-7",
"user-8",
"user-9",
"user-10"
]