【问题标题】:How do you manage per env config file using ansible?您如何使用 ansible 管理每个 env 配置文件?
【发布时间】:2017-09-27 10:06:17
【问题描述】:

我正在使用 ansible 安装 Apache,目前我在 ansible 存储库中有多个 httpd.conf 文件(test/dev/staging/production),除了一些特定于环境的设置外,大部分内容都是相同的。

是否可以使用一个httpd.conf 模板文件,并在将httpd.conf 发送到远程服务器时修改该文件?

【问题讨论】:

    标签: ansible ansible-template


    【解决方案1】:

    是的,你可以。使用 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>
    

    【讨论】:

    • 你有完整的项目测试要分享吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 2020-09-04
    • 2019-06-19
    • 1970-01-01
    相关资源
    最近更新 更多