我正在使用 ansible 安装 Apache,目前我httpd.conf
在 ansible 存储库中有多个文件(test/dev/staging/production),除了一些特定于环境的设置外,大部分内容都是相同的。
是否可以使用一个httpd.conf
模板文件,并在发送httpd.conf
到远程服务器时修改文件?
我正在使用 ansible 安装 Apache,目前我httpd.conf
在 ansible 存储库中有多个文件(test/dev/staging/production),除了一些特定于环境的设置外,大部分内容都是相同的。
是否可以使用一个httpd.conf
模板文件,并在发送httpd.conf
到远程服务器时修改文件?
是的你可以。使用 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>