如何判断ansible的unarchive模块创建了哪个目录?

时间:2016-06-01 18:42:24

标签: ansible tar ansible-playbook

我正在编写一本ansible playbook来自动安装一个软件。当我从网站下载tarball时:

software-package-release.tar.gz

当解开时,我留下目录

software-package-v2.15/

ansible有没有办法注册作为unarchive模块的一部分创建的目录?

我尝试了以下播放(我将nrpe-lkdsflkdjf文件放在那里,其中包含一个目录nrpe-2.15

- name: Extract
  unarchive
    src:/tmp/nrpe-lkdsflkdjf
    dest: /tmp/
    copy: no
  register: tar_reg

- name: debug tar_reg
  debug: var=tar_reg

这是调试的输出:

ok: [IP-here] => {
    "tar_reg": {
        "changed": true, 
        "check_results": {
            "cmd": "/bin/gtar -C \"/tmp/\" --diff -f \"/tmp/nrpe-lkdsjflkdsjf\"", 
            "err": "/bin/gtar: nrpe-2.15: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/SECURITY: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/README.Solaris: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/init-script.debian.in: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/nrpe.spec.in: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/LEGAL: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/subst.in: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/sample-config: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/sample-config/nrpe.xinetd.in: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/sample-config/nrpe.cfg.in: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/package: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/package/solaris: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/package/solaris/pkg: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/package/solaris/pkg/preinstall: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/package/solaris/pkg/nrpe.xml: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/package/solaris/pkg/i.config: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/package/solaris/pkg/postinstall: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/package/solaris/pkg/r.config: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/package/solaris/pkg/nrpe: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/package/solaris/Makefile.in: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/nrpe.spec: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/README: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/src: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/src/acl.c: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/src/check_nrpe.c: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/src/snprintf.c: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/src/Makefile.in: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/src/utils.c: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/src/nrpe.c: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/config.guess: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/install-sh: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/contrib: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/contrib/nrpe_check_control.c: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/contrib/README.nrpe_check_control: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/init-script.in: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/docs: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/docs/NRPE.pdf: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/docs/NRPE.odt: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/init-script.suse.in: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/configure: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/config.sub: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/Changelog: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/Makefile.in: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/README.SSL: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/configure.in: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/update-version: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/include: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/include/dh.h: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/include/common.h: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/include/utils.h: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/include/acl.h: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/include/config.h.in: Warning: Cannot stat: No such file or directory\n/bin/gtar: nrpe-2.15/include/nrpe.h: Warning: Cannot stat: No such file or directory\n", 
            "out": "", 
            "rc": 1, 
            "unarchived": false
        }, 
        "dest": "/tmp/", 
        "extract_results": {
            "cmd": "/bin/gtar -xf \"/tmp/nrpe-lkdsjflkdsjf\"", 
            "err": "", 
            "out": "", 
            "rc": 0
        }, 
        "gid": 0, 
        "group": "root", 
        "handler": "TarArchive", 
        "mode": "01777", 
        "owner": "root", 
        "size": 3072, 
        "src": "/tmp/nrpe-lkdsjflkdsjf", 
        "state": "directory", 
        "uid": 0
    }
}

2 个答案:

答案 0 :(得分:10)

unarchive module可以选择列出文件 - import pandas as pd DF1 = pd.DataFrame({'name': ['abc in first line of text', 'abc of second line of text', 'def on third line of text'], 'other': [10, 20, 30]}) DF2 = DF1[DF1.name.str.startswith('abc')] DF2['name'] = [x.split('abc')[1][4:].title() for x in DF2['name']] print(DF2) 。输出可以注册,通常数组中的第一个值是目录名。

以下对我有用:

list_files

- name: Extract files unarchive: src: /tmp/my_archive.tar.gz dest: /mydir remote_src: yes list_files: yes register: archive_contents 将包含您的顶级目录。

答案 1 :(得分:4)

更新:请参阅@ vinayak-thatte的answer以获取正确答案。

Unarchive模块不知道存档中的内容 - 它只是将所有存档内容放入dest文件夹。

如果你事先知道存档中有一个或哪个文件夹,你可以做这样的测试:

- name: Get dir 1 - fast - when you are sure that there is one and only one subfolder
  shell: tar tf file.tgz | head -1 | sed -e 's/\/.*//'
  changed_when: false
  register: tar_test

- debug: 'msg="Subfolder: {{tar_test.stdout}}"'

- name: Get dir 2 - slow - when you dont sure
  shell: tar tf file.tgz | sed -e 's/\/.*//' | sort | uniq
  changed_when: false
  register: tar_test

- debug: 'msg="Subfolder: {{tar_test.stdout}}"'
  when: tar_test.stdout_lines | count == 1

- debug: msg="No subfolder!"
  when: tar_test.stdout_lines | count > 1

第一种方法比第二种方法快,但它只检查一行存档文件列表 - 所以你必须确保里面只有一个子文件夹。
第二种方法检查存档中的所有路径并在存档“root”中收集不同的名称 - 因此,如果只有一个匹配(count == 1),则很可能是子文件夹(或存档中的单个文件: - /) ,否则档案“root”中有很多条目。