2015-09-15 48 views
0

ansible版本1.9.1

猫files.yml

tasks: 
    - name: stat files 
    stat: path=~/{{ item }} 
    register: {{ item }}.stat 
    with_items: 
     - foo.zip 
     - bar.zip 

    - name: copy files 
    copy: src=~/{{ item }} dest=/tmp/{{ item }} 
    register: {{ item }}.result 
    when: {{ item }}.stat.stat.exists == False 
    with_items: 
     - foo.zip 
     - bar.zip 

- name: unzip files 
    shell: cd /tmp/ && unzip -o {{ item }} 
    when: {{ item }}.result|changed == True 
    with_items: 
    - foo.zip 
    - bar.zip 

错误:语法错误而载入YAML脚本Ansible:如何使用登记在循环

如果是这样,怎么样?

回答

1

首选的方法是使用synchronize模块。 从ansible文档(http://docs.ansible.com/ansible/synchronize_module.html#synopsis

This is a wrapper around rsync. Of course you could just use the command action to call rsync yourself, but you also have to add a fair number of boilerplate options and host facts. You still may need to call rsync directly via command or shell depending on your use case. The synchronize action is meant to do common things with rsync easily. It does not provide access to the full power of rsync, but does make most invocations easier to follow.

下面是一个例子:

- name: Sync files 
    synchronize: src=some/relative/path dest=/some/absolute/path 
+0

哦,非常感谢! – yuezhonghua