我正在写一个ansible代码。它执行两个任务,首先是将配置文件分组复制到目标实例。其次是运行该配置文件来安装应用程序。
我正在以编程方式创建配置文件和清单,以便将相同的后缀添加到清单中的配置文件名和组名中:
Configfile name example : Equivalent group names:
myappconf1 [myapp1]
hostname
myappconf2 [myapp2]
hostname
这是我复制文件的代码
hosts: all
tasks:
name: Copy file.role1 to host1
copy: src=/tmp/myconf1 dest=/tmp
when:
- "'myapp1' in group_names"
name: Copy Config File two to to Ldap2
copy: src=/tmp/myconf2 dest=/tmp
when:
- "'myapp2' in group_names"
这是我运行conf文件的代码
hosts: myapp1
tasks:
- command: "/tmp/mainapp/update.sh -f myappconf1"
hosts: myapp2
tasks:
- command: "/tmp/mainapp/update.sh -f myappconf1"
但是根据用户输入,可以创建不确定数量的 conf 文件和组,所以我想以编程方式完成任务。所需的代码可能如下所示:
[task for copying file]
hosts: ~(myapp)
tasks:
- copy: copy the appropriate file to the host
example: copy myappconf4 to myapp4
- command: run the commmand with appropirate file
example: for myapp3, command: /tmp/mainapp/update.sh -f myappconf3
有人可以建议我用什么来使我的代码更加通用和高效吗?