2017-09-27 97 views
0

我使用ansible來安裝Apache,目前我有多個httpd.conf文件(測試/ dev/staging/production)在一個完好的存儲庫中,除了一些環境特定的設置外,大多數內容是相同的。如何使用ansible管理每個env配置文件?

是否可以使用一個httpd.conf模板文件,並在發送httpd.conf到遠程服務器時修改文件?

回答

0

是的,你可以。用jinja2和group_vars。

所以,你在你的模板做什麼/文件夾中創建一個文件中像這樣的:

templates/http.conf.j2 

說你在那裏有這樣的事情:

NameVirtualHost *:80 

<VirtualHost *:80> 
    ServerName {{ subdomain }}.{{ domain }} 
    ServerAlias www.{{ subdomain }}.{{ domain }} 
</VirtualHost> 

您的佈局應該是這樣的:

├── group_vars 
│   ├── all 
│   │   └── config 
│   ├── dev 
│   │   └── config 
│   └── test 
│    └── config 
├── inventory 
│   ├── dev 
│   │   └── hosts 
│   └── test 
│    └── hosts 
├── site.yml 
└── templates 
    └── http.conf.j2 

group_vars/all你會有domain: "example.com"

group_vars/dev你會subdomain: dev

group_vars/test你會subdomain: test

在你的任務,你有你的ansible模板命令即

- hosts: all 
    tasks: 
    - name: Copy http conf 
     template: 
     dest: /etc/apache2/http.conf 
     src: templates/http.conf.j2 
     owner: root 
     group: root 

並運行你的劇本是這樣:

ansible-playbook -i inventory/test site.yml 

該文件應該在主機上看起來像這樣:

<VirtualHost *:80> 
    ServerName test.example.com 
    ServerAlias www.test.example.com 
</VirtualHost>