Ansible 复制目录的最佳实践

标签 ansible ansible-playbook

在我的剧本中,我有

- name: Grab h5bp/server-configs-nginx
  git:  repo=https://github.com/h5bp/server-configs-nginx.git
        dest=/tmp/server-configs-nginx
        version="3db5d61f81d7229d12b89e0355629249a49ee4ac"
        force=yes

- name: Copy over h5bp configuration
  command: cp -r /tmp/server-configs-nginx/{{ item }} /etc/nginx/{{ item }}
  with_items:
    - "mime.types"
    - "h5bp/"

这会在 ansible-lint 中引发警告:
[ANSIBLE0006] cp used in place of copy module
/Users/austinpray/Dropbox/DEV/opensauce/bedrock-ansible/roles/nginx/tasks/main.yml:0
Task/Handler: Copy over h5bp configuration

所以这就提出了一个问题:有没有更好的方法来使用 ansible 模块而不是命令来做到这一点?

最佳答案

目前,command是您的最佳选择。没有远程到远程的选项。这是一个关于它的线程:How to move/rename a file using an Ansible task on a remote system

您还有其他几个选择:

  • 您可以使用 file建立符号链接(symbolic link)的模块(通过设置 srcpathstate=link
  • 您可以查看 Ansible 服务器上的 repo,然后使用 copy .这是部署代码的更常见模型。
  • 您可以继续使用 command但用 stat 包裹它有条件的,所以它只覆盖一次。如果您使用 notify,这将特别有用。重启 nginx。

  • 最后,看起来您可能正在执行“由 git 部署”。这并不总是最好的选择,特别是如果你不“拥有”那个 repo。但这可能很好——只是有点代码味道。

    关于Ansible 复制目录的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25576871/

    相关文章:

    vagrant - 未找到 group_vars/all 中定义的 Ansible 变量

    Ansible:获取组中的主机数

    Ansible:删除主机

    ansible - 在ansible寄存器变量中搜索字符串列表

    regex - Ansible 列表变量和选择过滤器以忽略匹配项

    elasticsearch - ansible Elasticsearch 安装版本不匹配失败

    ubuntu-12.04 - 在ansible playbook中使用sudo权限执行任务

    ansible lineinfile 如何添加具有多个目标的多行?

    ssh - ansible ssh 权限被拒绝

    ansible - 我们可以在 ansible-playbook 中禁用流水线,但在 ansible.cfg 中有它吗?