您如何使用 ansible 管理每个 env 配置文件?
How do you manage per env config file using ansible?
我正在使用 ansible 安装 Apache,目前我在 ansible 存储库中有多个 httpd.conf
文件(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>
我正在使用 ansible 安装 Apache,目前我在 ansible 存储库中有多个 httpd.conf
文件(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>