同时创建多个目录的yml,
- hosts: testhost
vars_files:
- /tmp/vars.yml
tasks:
- name: use disk name make dir
command: mkdir -p "/tmp/{{ dict_1.a }}/{{ dict_1.b }}/{{ dict_1.c }}"
"/tmp/{{ dict_2['a'] }}/{{ dict_2['b'] }}/{{ dict_2['c'] }}"
"/tmp/{{ list_1[0] }}/{{ list_1[1] }}/{{ list_1[2] }}"
"/tmp/{{ list_2[0] }}/{{ list_2[1] }}/{{ list_2[2] }}"
额外变量文件 vars.yml
---
dict_1: #yml 注释可以随便折腾
a: AA #缩进必须用空格
b: BB
c: CC
dict_2: { a: D1, b: D1, c: D1 } #字典也可以这样
list_1:
- 11
- 22
- 33
list_2: [ L1, L2, L3 ] #列表也可以这样
结果:
root@svntest:/tmp# ll total 40 drwxrwxrwx 6 root root 20480 May 17 14:14 ./ drwxr-xr-x 24 root root 4096 Dec 18 2013 ../ drwxr-xr-x 3 root root 4096 May 17 14:14 11/ drwxr-xr-x 3 root root 4096 May 17 14:14 AA/ drwxr-xr-x 3 root root 4096 May 17 14:14 D1/ drwxr-xr-x 3 root root 4096 May 17 14:14 L1/
判断apache2进程是否存在,不存在就启动
---
- hosts: testhost
tasks:
- shell: netstat -tunlp | grep ":80 " | wc -l
register: result
# - debug: msg="MSG {{ result }} END"
- command: /etc/init.d/apache2 start
when: result.stdout == "0"
- hosts: testc
tasks:
- debug: msg="EEE {{ hostvars.testhost.result.stdout }}" #访问 testhost 的result变量
使用 /etc/ansible/hosts 中的相关主机
[test] testhost centostest
相关 yml 调用
- debug: msg="{{ groups.test[0] }}"
附:jinja2讲法
转载于:https://blog.51cto.com/abian/1774317