Ansible playbook不复制文件,不给出错误

时间:2014-08-07 03:31:25

标签: copy ansible ansible-playbook

我的ansible playbook只复制我指定的一个文件。该剧本运行完成,没有任何错误。如果我在剧本之外运行相同的命令,它将复制

我非常简单的剧本

---
- hosts: "dudes"
  vars:
    remote_user: root
tasks:
    - name: Installs logstash-forwarder rpm
      yum: name=logstash-forwarder state=present disable_gpg_check=yes

    - name: Add the certs init and etc files into place
      action: copy src=/etc/ansible/files/logstash-forwarder/logstash-forwarder.crt dest=/etc/pki/tls/certs/ owner=root group=root mode=644
      action: copy src=/etc/ansible/files/logstash-forwarder/logstash-forwarder-etc dest=/etc/logstash-forwarder owner=root group=root mode=644 force=yes
      action: copy src=/etc/ansible/files/logstash-forwarder/logstash-forwarder-etc-sysconfig dest=/etc/sysconfig/logstash-forwarder owner=root group=root mode=644 force=yes
      action: copy src=/etc/ansible/files/logstash-forwarder/logstash-forwarder-init-d dest=/etc/init.d/logstash-forwarder mode=0755

    - name: add logstash forwarder to chkconfig
      command: chkconfig --add logstash-forwarder
      notify:
            - start logstash-forwarder
handlers:
    - name: start logstash-forwarder
      service: name=logstash-forwarder enabled=yes state=started

我用

运行剧本
ansible-playbook -l doozy logstash-forwarder-dudes.yml 

它完成没有任何错误,说所有任务都没问题

实际复制的唯一文件是最后一个文件,它是init.d中的logstash-forwarder。 chkconfig添加似乎也可以工作

如果我在其中一个之前没有复制的文件上运行带有ansible -m(而不是playbook)的命令,它将复制它们而不会出错

ansible -m copy -a "src=/etc/ansible/files/logstash-forwarder/logstash-forwarder.crt dest=/etc/pki/tls/certs/ owner=root group=root mode=644" doozy

我不确定为什么这些文件最终没有被复制到我的剧本中的desination主机,但是可以通过调用ansible copy模块来工作吗?

1 个答案:

答案 0 :(得分:1)

Ansible任务中不能有多个动作。

将文件复制到四个任务中,它应该可以工作:

- name: Copy logstash-forwarder.crt
  copy: src=/etc/ansible/files/logstash-forwarder/logstash-forwarder.crt dest=/etc/pki/tls/certs/ owner=root group=root mode=644

- name: Copy /etc/logstash-forwarder
  copy: src=/etc/ansible/files/logstash-forwarder/logstash-forwarder-etc dest=/etc/logstash-forwarder owner=root group=root mode=644 force=yes

- name: Copy /etc/sysconfig/logstash-forwarder
  copy: src=/etc/ansible/files/logstash-forwarder/logstash-forwarder-etc-sysconfig dest=/etc/sysconfig/logstash-forwarder owner=root group=root mode=644 force=yes

- name: Copy /etc/init.d/logstash-forwarder
  copy: src=/etc/ansible/files/logstash-forwarder/logstash-forwarder-init-d dest=/etc/init.d/logstash-forwarder mode=0755

在代码中我还使用了Action Shorthand并将action: module ...符号替换为module: ...。它不会改变代码的工作方式,但却是调用模块的首选方式。