groovy 使用Ansible删除多个文件和文件夹

neekobn8  于 2022-12-11  发布在  其他
关注(0)|答案(2)|浏览(355)

我需要使用Ansible剧本删除文件和文件夹。我将文件/文件夹路径作为变量从Groovy脚本传递给Ansible剧本。
变量在一个名为 delete.propertiesproperties 文件中。我将文件/文件夹路径分别存储在一个变量中,以便将来需要时可以更改路径。

  • 删除.属性 *:
delete_files=/home/new-user/myfolder/dltfolder1 /home/new-user/myfolder/dltfolder2 /home/new-user/myfolder/dltfolder3

Groovy脚本:

stage("Read variable"){
      steps{
        script{
         def propertifile = readFile(properti file path)
         deleteParams = new Properties()
         deleteParams.load(new StringReader(propertifile))
        }
     }
  }
    stage("Delete files/folders"){
      steps{
        script{
         sh script: """cd ansible code path && \
         export ANSIBLE_HOST_KEY_CHECKING=False && \
         ansible-playbook delete.yml \ 
         --extra-vars"dete_files=${deleteParams.delete_files}" --user user"""
        }
     }
  }

Ansible行动手册:

---
- name: delete files
  hosts: localhost
  tasks:
   - name: delete files
     file:
      path: "{{ delete_files }}"
      state: absent

由于上述代码,只有 delete.properties 文件中delete_files(*/home/new-user/myfolder/dltfolder 1 *)变量中的第一个文件路径被删除。
我还需要删除delete_files变量中包含的其他文件/文件夹路径。

utugiqy6

utugiqy61#

将文件的路径放入额外的变量中。例如,

sh script: """cd ansible code path && \
         export ANSIBLE_HOST_KEY_CHECKING=False && \
         ansible-playbook delete.yml \ 
         --extra-vars "dete_files=/tmp/delete.properties" --user user"""

然后,鉴于树

shell> tree /tmp/test
/tmp/test
├── f1
├── f2
└── f3

,文件

shell> cat /tmp/delete.properties 
delete_files=/tmp/test/f1 /tmp/test/f2 /tmp/test/f3

,以及行动手册

shell> cat delete.yml
- hosts: localhost

  vars:

    delete_files: "{{ lookup('ini',
                             'delete_files',
                             file=dete_files,
                             type='properties') }}"

  tasks:

    - debug:
        var: delete_files

    - name: delete files
      file:
        path: "{{ item }}"
        state: absent
      loop: "{{ delete_files.split() }}"

gives,在 --check --diff 模式下运行

shell> ansible-playbook delete.yml --extra-vars "dete_files=/tmp/delete.properties" -CD

PLAY [localhost] *****************************************************************************

TASK [debug] *********************************************************************************
ok: [localhost] => 
  delete_files: /tmp/test/f1 /tmp/test/f2 /tmp/test/f3

TASK [delete files] **************************************************************************
--- before
+++ after
@@ -1,5 +1,2 @@
 path: /tmp/test/f1
-path_content:
-  directories: []
-  files: []
-state: directory
+state: absent

changed: [localhost] => (item=/tmp/test/f1)
--- before
+++ after
@@ -1,5 +1,2 @@
 path: /tmp/test/f2
-path_content:
-  directories: []
-  files: []
-state: directory
+state: absent

changed: [localhost] => (item=/tmp/test/f2)
--- before
+++ after
@@ -1,5 +1,2 @@
 path: /tmp/test/f3
-path_content:
-  directories: []
-  files: []
-state: directory
+state: absent

changed: [localhost] => (item=/tmp/test/f3)

PLAY RECAP ***********************************************************************************
localhost: ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
rqqzpn5f

rqqzpn5f2#

一种解决方案是解析Ansible剧本中的 properties 文件,如果你确实在localhost上操作,就像剧本中显示的那样,使用ini lookup

- hosts: localhost
  gather_facts: no

  tasks:
    - file:
        path: "{{ item }}"
        state: absent
      loop: >-
        {{
          lookup(
            'ini',
            'delete_files type=properties file=delete.properties'
          ).split()
        }}

相关问题