[root@ansible-53_26 ansible-env]# cat roles/environment/templates/redis.properties.j2
stie.yml 文件内容:
---
- hosts: hosts
user: root
gather_facts: False
roles:
- environment
现在我们让不同环境下的主机组,去执行一个相同的 role ,产生不同的配置文件
首先我们生成生产环境的配置文件:
[root@ansible-53_26 ansible-env]# ansible-playbook -i production/inventory site.yml
[PLAY [hosts] ******************************************************************
TASK: [environment | dispaly env variable] ************************************
ok: [wx-php-13.65] => {
"msg": "produciton"
}
TASK: [environment | Configure redis.properties j2] ***************************
changed: [wx-php-13.65]
PLAY RECAP ********************************************************************
wx-php-13.65 : ok=2 changed=1 unreachable=0 failed=0
[root@ansible-53_26 ansible-env]# ansible -i production/inventory -a "cat /tmp/redis.properties" all
wx-php-13.65 | success | rc=0 >>
redis.server.0.ip=10.66.141.157
redis.server.0.port=8000
redis.server.1.ip=10.66.141.157
redis.server.1.port=8001
然后我们在生成开发环境的配置文件:
[root@ansible-53_26 ansible-env]# ansible-playbook -i development/inventory site.yml
PLAY [hosts] ******************************************************************
TASK: [environment | dispaly env variable] ************************************
ok: [wx-php-48.224] => {
"msg": "development"
}
TASK: [environment | Configure redis.properties j2] ***************************
changed: [wx-php-48.224]
PLAY RECAP ********************************************************************
wx-php-48.224 : ok=2 changed=1 unreachable=0 failed=0
[root@ansible-53_26 ansible-env]# ansible -i development/inventory -a "cat /tmp/redis.properties" all
wx-php-48.224 | success | rc=0 >>
redis.server.0.ip=10.66.128.202
redis.server.0.port=8009
OK,目的达到了,收工下班,回家。
wepaio_Cail