How can I check if file has been downloaded in ans

2019-01-21 23:21发布

I am downloading the file with wget from ansible.

  - name: Download Solr
    shell: chdir={{project_root}}/solr wget http://mirror.mel.bkb.net.au/pub/apache/lucene/solr/4.7.0/solr-4.7.0.zip

but I only want to do that if zip file does not exist in that location. Currently the system is downloading it every time.

8条回答
疯言疯语
2楼-- · 2019-01-21 23:27

Many modules are already aware of the result and will be skipped if its already there, like file or geturl. Others like command have a creates option, which will skip this command if that file already exists (or doesn't exist, if you use the removes option).

So you should first check the available modules, if they are smart enough already. If not: I recommend the stats module. Advantage over the other solution: No "red errors but ignored" in the output.

- name: Check MySQL data directory existence
  stat: path=/var/lib/mysql-slave
  register: mysql_slave_data_dir

- name: Stop MySQL master to copy data directory
  service: name=mysql state=stopped
  sudo: yes 
  when: not mysql_slave_data_dir.stat.exists
查看更多
唯我独甜
3楼-- · 2019-01-21 23:31

There are at least two options here.

You can register a variable if the file exists, then use a when condition to execute the command on the condition that the file doesn't already exist:

- command: /usr/bin/test -e {{project_root}}/solr/solr-4.7.0.zip
  register: solr_zip
  ignore_errors: True
- name: Download Solr
  shell: chdir={{project_root}}/solr /usr/bin/wget http://mirror.mel.bkb.net.au/pub/apache/lucene/solr/4.7.0/solr-4.7.0.zip
  when: solr_zip|failed

You could also use the commands module with the creates option:

- name: Download Solr
  command: /usr/bin/wget http://mirror.mel.bkb.net.au/pub/apache/lucene/solr/4.7.0/solr-4.7.0.zip chdir={{project_root}}/solr  creates={{project_root}}/solr/solr-4.7.0.zip
查看更多
forever°为你锁心
4楼-- · 2019-01-21 23:31

This article might be useful

Out of it comes this example:

tasks:
  - shell: if [[ -f "/etc/monitrc" ]]; then /bin/true; else /bin/false; fi
    register: result
    ignore_errors: True

  - command: /bin/something
    when: result|failed

  - command: /bin/something_else
    when: result|success

  - command: /bin/still/something_else
    when: result|skipped
查看更多
Emotional °昔
5楼-- · 2019-01-21 23:45

Note: this answer covers general question of "How can i check the file existence in ansible", not a specific case of downloading file.

The problems with the previous answers using "command" or "shell" actions is that they won't work in --check mode. Actually, first action will be skipped, and next will error out on "when: solr_exists.rc != 0" condition (due to variable not being defined).

Since Ansible 1.3, there's more direct way to check for file existance - using "stat" module. It of course also works well as "local_action" to check a local file existence:

- local_action: stat path={{secrets_dir}}/secrets.yml
  register: secrets_exist

- fail: msg="Production credentials not found"
  when: secrets_exist.stat.exists == False
查看更多
在下西门庆
6楼-- · 2019-01-21 23:46

Use the creates argument

- name: Download Solr
  shell: creates={{working_directory}}/solr/solr-4.7.0.zip chdir={{working_directory}}/solr wget http://mirror.mel.bkb.net.au/pub/apache/lucene/solr/4.7.0/solr-4.7.0.zip
查看更多
何必那么认真
7楼-- · 2019-01-21 23:47

So basically you can do this checking by registering a variable from a command and checking its return code. (You can also do this by checking its stdout)

- name: playbook
  hosts: all
  user: <your-user>

  vars:
    project_root: /usr/local

  tasks:

    - name: Check if the solr zip exists.
      command: /usr/bin/test -e {{project_root}}/solr/solr-4.7.0.zip
      ignore_errors: True
      register: solr_exists

    - name: Download Solr
      shell: chdir={{project_root}}/solr wget http://mirror.mel.bkb.net.au/pub/apache/lucene/solr/4.7.0/solr-4.7.0.zip
      when: solr_exists.rc != 0

This basically says that if the /usr/bin/test -e {{project_root}}/solr/solr-4.7.0.zip command returns a code that is not 0, meaning it doesn't exist then execute the task Download Solr

Hope it helps.

查看更多
登录 后发表回答