任务仅在所需目录文件夹不可用时运行,否则它将跳过该任务

时间:2019-04-07 13:49:05

标签: ansible

我需要使用ansible安装sqoop并移至/ usr / lib,但仅当sqoop目录在/ tmp和/ usr / lib中不可用时

找到下面的代码

---  
- name: Download Sqoop_1.4.7 Download on temp folder
  get_url:
    url: https://www-eu.apache.org/dist/sqoop/1.4.7/sqoop-1.4.7.tar.gz
    dest: /tmp
    mode: 0777

- name: Extract Sqoop-1.4.7 into /tmp
  unarchive:
    remote_src: yes
    src: /tmp/sqoop-1.4.7.tar.gz
    dest: /tmp

- name: "check Sqoop direcotry is present in /tmp"
  stat: path=/tmp/sqoop-1.4.7
  register: p

- name: Move sqoop-1.4 directory
  when: not p.stat.exists
  command: mv /tmp/sqoop-1.4.7 /tmp/sqoop
  become: yes
  become_user: root

- name: "check Sqoop direcotry is present in /usr/lib/"
  shell: test "$(ls /usr/lib/sqoop)"
  register: sqooppresent
  changed_when: false

- name: Move sqoop-1.4 to /usr/lib/
  when: "sqooppresent.rc != 0"
  command: mv /tmp/sqoop /usr/lib/
  become: yes
  become_user: gaurav 

1 个答案:

答案 0 :(得分:0)

您可以使用stat模块来确定路径/usr/lib/sqoop是否存在。

https://raymii.org/s/tutorials/Ansible_-_Only_if_a_file_exists_or_does_not_exist.html

- name: check if sqoop is present in /usr/lib/
  stat: 
    path: /usr/lib/sqoop
  register: sqooppresent

- name: Move sqoop-1.4 to /usr/lib/
  when: not sqooppresent.stat.exists
  command: mv /tmp/sqoop /usr/lib/
  become: yes
  become_user: gaurav