首页 文章

Ansible远程模板

提问于
浏览
15

我有存储在项目存储库中的配置文件的模板 . 我想要做的是在从存储库克隆项目之后,使用Ansible的模板模块在远程服务器上使用该模板创建配置文件 .

查看template模块的文档,似乎 src 属性仅支持本地文件 .

我想避免使用我的Ansible playbook存储配置模板,因为我更有意义的是将这些项目特定模板保存在项目存储库中 .

我可以使用模板模块的替代方案吗?

1 回答

  • 21

    如果您的模板将在远程主机上,那么您有两个选项 .

    首先,您可以使用fetch模块,该模块与copy模块完全相反,可以在克隆远程主机上的存储库后恢复模板 .

    这个剧本可能看起来像:

    - name : clone repo on remote hosts
      git  :
        repo : {{ git_repo_src }}
        dest : {{ git_repo_dest }}
    
    - name     : fetch template from single remote host
      run_once : true
      fetch    :
        src             : {{ template_path }}/{{ template_file }}
        dest            : /tmp/{{ template_file }}
        flat            : yes
        fail_on_missing : yes
    
    - name     : template remote hosts
      template :
        src   : /tmp/{{ template_file }}
        dest  : {{ templated_file_dest }}
        owner : {{ templated_file_owner }}
        group : {{ templated_file_group }}
        mode  : {{ templated_file_mode }}
    

    获取任务使用run_once来确保它只是困扰从它运行的第一个主机复制模板 . 假设你游戏中的所有这些主机都获得相同的回购,那么这应该没问题,但如果你需要确保它从一个非常特定的主机复制,那么你可以将它与delegate_to结合起来 .

    或者,您可以让Ansible在本地克隆repo并直接使用它,例如:

    - name : clone repo on remote hosts
      git  :
        repo : {{ git_repo_src }}
        dest : {{ git_repo_dest }}
    
    - name       : clone repo on Ansible host
      hosts      : localhost
      connection : local
      git  :
        repo : {{ git_repo_src }}
        dest : {{ git_repo_local_dest }}
    
    - name     : template remote hosts
      template :
        src   : {{ template_local_src }}
        dest  : {{ templated_file_dest }}
        owner : {{ templated_file_owner }}
        group : {{ templated_file_group }}
        mode  : {{ templated_file_mode }}
    

相关问题