在顶级playbook中选择group_var变量文件

时间:2017-05-05 10:33:23

标签: ansible ansible-2.x ansible-inventory

我在k=0 for i in dirs1: for j in dirs2: if i==j: with open(os.path.join(path1,os.path.basename(i)), 'r') as f1: with open(os.path.join(path2,os.path.basename(j)), 'r') as f2: k+=1 print('TC'+str(k)+ ' started...') print(os.path.join(path1,os.path.basename(i)), '\t',os.path.join(path2,os.path.basename(j))) out_file.flush() name1=os.path.basename(i) name2=os.path.basename(j) if name1==name2: diff = difflib.ndiff(f1.readlines(), f2.readlines()) out_file.flush() testPassed=None #print lines with difference for line in diff: if line.startswith('-') and not(line.startswith('- <ID') or line.startswith('-Created :') or line.startswith('-Name=') or line.startswith('- <Information') or line.startswith('- ,,Date:') or line.startswith('- ,,,')): testPassed=True out_file.write(line) out_file.flush() elif line.startswith('+') and not(line.startswith('+ <ID') or line.startswith('+Created :') or line.startswith('+Name=') or line.startswith('+ <Information') or line.startswith('+ ,,Date:') or line.startswith('+ ,,,')): testPassed=True out_file.write(line) out_file.flush() if not testPassed ==True: print('\tTC'+str(k)+' Passed for: %s\n' %i) else: print('\t\t***TC'+str(k)+' Failed for: %s ***\n' %i) 中定义了所有变量,我的剧本如下:

group_vars/all/vars_file.yml

现在我有3个环境dev / staging / production。根据我用来更改group_vars下的vars_file.yml的环境,然后运行ansible-play。

有什么方法可以保留3个文件,例如&#34; group_vars / dev&#34; ,&#34; group_vars / staging&#34;,&#34; group_vars / production&#34;并在我的主site.yml中指定它

我有3个库存文件,如下所示,根据ansible-play期间的环境,我指定了库存文件名

---
# Top level play site.yml
- hosts: webclient

  roles:
    - common
    - nginx
    - nvm
    - deploy_web_client

- hosts: appserver
  roles:
    - common
    - gradle
    - tomcat
    - deploy_sb_war

1 个答案:

答案 0 :(得分:4)

不使用保存在单个目录中的库存文件,而是使用单独目录中的库存文件,并将group_vars放在每个目录中。

.
├── dev
│   ├── group_vars
│   │   └── all
│   │       └── vars_file.yml
│   └── inventory
├── production
│   ├── group_vars
│   │   └── all
│   │       └── vars_file.yml
│   └── inventory
└── staging
    ├── group_vars
    │   └── all
    │       └── vars_file.yml
    └── inventory

然后指向ansible-playbook电话中的目录:

ansible-playbook -i dev <the_rest>