我有此任务,应将随机目录下载文件创建到随机目录 但是我在regexp上总是出错:
---
- name: check if exsist
hosts: localhost
gather_facts: false
tasks:
- debug:
msg: "####################### This is start #######################"
- name: Handle the error
block:
- name: ansible create directory with mode setting example
vars:
name_dir: "{{ 1000 | random | to_uuid }}"
file:
path: "/home/ec2-user/backup/{{ name_dir }}/"
state: directory
- name: Download from s3
vars:
obj_name: "{{ 'http://www.foo.bbb/foo/xxxx.zip' | regex_replace('^(?:\/\/|[^\/]+)*\/', '') }}"
file_name: "{{ 'http://www.foo.bbb/foo/xxxx.zip' | regex_search('[^\/]+$', '') }}"
local_action:
module: aws_s3
bucket: pack
object: "{{ obj_name }}"
dest: "/home/ec2-user/backup/{{ name_dir }}/{{ file_name }}"
mode: get
- debug:
msg: "################################ {{ obj_name }} {{ file_name }} ################################## {{ name_dir }} #####"
rescue:
- debug:
msg: 'I caught an error, can do stuff here to fix it, :-)'
这是错误:
TASK [Download from s3] *********************************************************************************************************************************************************************************************************************
task path: /home/ec2-user/backup/test7.yml:18
Thursday 07 May 2020 16:11:59 +0000 (0:00:00.297) 0:00:00.381 **********
fatal: [localhost]: FAILED! =>
msg: 'An unhandled exception occurred while templating ''{{ ''http://www.foo.bbb/foo/xxxx.zip'' | regex_search(''[^/]+$'', '''') }}''. Error was a <class ''ansible.errors.AnsibleFilterError''>, original message: Unknown argument'
但有线的事情是,这里工作正常:
---
- name: check if exsist
hosts: localhost
gather_facts: false
tasks:
- debug:
var: "{{ '
obj_name: "{{ 'http://www.foo.bbb/foo/xxxx.zip' | regex_replace('^(?:\/\/|[^\/]+)*\/', '') }}"