我写的是一个安全代码。它执行两项任务,首先是将配置文件复制到组中的目标实例。其次是运行该配置文件来安装应用程序。
我正在以编程方式创建配置文件和库存,以便将相同的后缀添加到configfile name&库存中的组名:
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"
这是运行配置文件
的代码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
有人可以建议我使用什么来使我的代码更通用和有效吗?
答案 0 :(得分:0)
你可以使用像这样的组变量,
/**
* This example adds a new remarketing list audience to the account and
* retrieves the associated remarketing tag code.
*/
class AddAudience {
public static function runExample(AdWordsServices $adWordsServices,
AdWordsSession $session) {
$userListService =
$adWordsServices->get($session, AdwordsUserListService::class);
$conversionTrackerService =
$adWordsServices->get($session, ConversionTrackerService::class);
// Create a conversion type (tag).
$conversionType = new UserListConversionType();
$conversionType->setName('Mars cruise customers #' . uniqid());
// Create a basic user list.
$userList = new BasicUserList();
$userList->setName('Mars cruise customers #' . uniqid());
$userList->setConversionTypes([$conversionType]);
// Set additional settings (optional).
$userList->setDescription(
'A list of mars cruise customers in the last year');
$userList->setStatus(UserListMembershipStatus::OPEN);
$userList->setMembershipLifeSpan(365);
// Create a user list operation and add it to the list.
$operations = [];
$operation = new UserListOperation();
$operation->setOperand($userList);
$operation->setOperator(Operator::ADD);
$operations[] = $operation;
// Create the user list on the server.
$userList = $userListService->mutate($operations)->getValue()[0];
// Create the selector.
$selector = new Selector();
$selector->setFields(['Id']);
$selector->setPredicates(
[new Predicate('Id', PredicateOperator::IN,
[$userList->getConversionTypes()[0]->getId()])]);
// Retrieve the conversion tracker and print out its information.
$conversionTracker =
$conversionTrackerService->get($selector)->getEntries()[0];
printf("Audience with name '%s' and ID %d was added.\n",
$userList->getName(), $userList->getId());
printf("Conversion tracker snippet:\n%s\n",
$conversionTracker->getSnippet()); }
然后将第二个文件夹用于第二个应用程序
#File : "myapp1/group_vars/all/vars_file.yml"
# Application settings
app_name: "myapp1"
copy_file: "myapp1conf1"
.
.
# other variables used for myapp1
并在您的代码中修改如下,
#File : "myapp2/group_vars/all/vars_file.yml"
# Application settings
copy_file: "myapp2conf2"
.
.
# other variables used for myapp2
现在根据环境使用播放时间内的用户输入,如下所示
hosts: all
-tasks
-name: Copy file.role1 to host1
copy: src=/tmp/{{ copy_file }} dest=/tmp
-name : execute script
command: "/tmp/mainapp/update.sh -f {{ app_name }}"
答案 1 :(得分:0)
我用以下格式的python代码填充变量来解决问题:
[myapp]
host1 conf_file="myappconf1"
host2 conf_file="myappconf2"
然后在我的代码中,我将变量用于复制和运行配置文件任务。