【问题标题】:Adding paths to config file in most efficent way via Ansible通过 Ansible 以最有效的方式将路径添加到配置文件
【发布时间】:2019-11-29 09:44:22
【问题描述】:

我编写了一个负责更改主管配置文件的任务。情况是,在某些服务器上,我们有多个运行工作程序的应用程序,因此有时需要将多个路径添加到 supervisor.confinclude 部分。

目前我在/roles/supervisor/tasks/main.yml/写了这个任务:

- name: Add apps paths in include section
  lineinfile:
    dest: /etc/supervisor/supervisord.conf
    regex: '^files ='
    line: 'files = /etc/supervisor/conf.d/*.conf /home/app/{{ app_name }}/releases/app/shared/supervisor/*.conf /home/dev/{{ app_name2 }}/releases/dev/shared/supervisor/*.conf'
  when: ansible_hostname = 'ser-db-10'
  notify: restart supervisor
  tags: multi_workers

...并添加到/roles/supervisor/defaults/main.yml/ this:

app_name: bla
app_name2: blabla

它有效,但我不喜欢在line 中硬编码两个应用程序路径,也许我还应该添加变量来代替ser-db-10

我想知道如何重建此任务以使其更加独立。

我的意思是,如果有 4 个应用,则添加 4 个路径,如果有 2 个应用,则添加 2 个路径。

最有效的方法是什么?

【问题讨论】:

    标签: regex variables ansible supervisord


    【解决方案1】:

    line参数如何拼凑为例,下图

    - hosts: test_01
      vars:
        app_name1: A
        app_name2: B
        my_conf:
          test_01:
            lines:
              - '/etc/*.conf'
              - '/etc/{{ app_name1 }}/*.conf'
              - '/etc/{{ app_name2 }}/*.conf'
    
      tasks:
        - debug:
            msg: "files = {{ my_conf[inventory_hostname].lines|join(' ') }}"
    

    给予

    "msg": "files = /etc/*.conf /etc/A/*.conf /etc/B/*.conf"
    

    使用适当的字典 my_conf 下面的任务应该可以完成这项工作

    - name: Add apps paths in include section
      lineinfile:
        dest: /etc/supervisor/supervisord.conf
        regex: '^files ='
        line: "files = {{ my_conf[inventory_hostname].lines|join(' ') }}"
      notify: restart supervisor
      tags: multi_workers
    

    (未测试)

    【讨论】:

    • 谢谢!我会测试它,让你知道它是否有效。
    • 当作为 playbook.yml 执行时,它可以正常工作,但我尝试将此解决方案转换为角色但没有成功。我试图在hosts: test_01 部分之前添加- name: Check existing apps on server,但我收到错误The offending line appears to be: - vars:,所以我想不能那样做。你知道如何做到这一点吗?
    • 我不知道你想完成什么。最好的选择是提出一个新问题。 Create a Minimal, Reproducible Example。在较短的版本(例如/etc/{{ app_name }}/*.conf 也能胜任。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 2014-10-20
    • 2014-03-14
    • 1970-01-01
    相关资源
    最近更新 更多