在Ansible中,可以使用with_sequence
模块和loop_control
模块来为一组主机创建动态增加的编号目录。以下是一个解决方法的代码示例:
- name: Create numbered directories on hosts
hosts: all
gather_facts: false
tasks:
- name: Create numbered directories
file:
path: "/path/to/directory{{ item }}"
state: directory
loop: "{{ range(1, number_of_directories + 1)|list }}"
loop_control:
loop_var: item
vars:
number_of_directories: 5
解释:
file
任务使用path
参数指定要创建的目录的路径。loop
参数使用range
函数生成一个从1到number_of_directories
的列表,然后通过list
过滤器将其转换为列表。loop_control
模块的loop_var
参数将每个循环的当前值命名为item
,这样我们可以在file
任务中引用它。vars
部分定义了一个名为number_of_directories
的变量,可以根据需要来设置。在上述示例中,将在每个主机上创建从/path/to/directory1
到/path/to/directory5
的五个目录。您可以根据需要调整number_of_directories
变量的值来创建不同数量的目录。