【问题标题】:Deleting multiple files and folders using Ansible使用 Ansible 删除多个文件和文件夹
【发布时间】:2022-12-06 01:23:04
【问题描述】:

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

删除.properties:

delete_files=/home/new-user/myfolder/dltfolder1 /home/new-user/myfolder/dltfolder2 /home/new-user/myfolder/dltfolder3

常规脚本:

     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_files中的第一个文件路径(/home/new-user/myfolder/dltfolder1) 中的变量删除.properties文件被删除。

我还需要删除 delete_files 变量中包含的其他文件/文件夹路径。

【问题讨论】:

  • 您至少有两个问题:首先(从 Ansible 的角度来看):文件模块将无法在没有循环的情况下删除文件。其次(但可能对您来说更重要):--extra-vars"dete_files=${deleteParams.delete_files}" 将呈现为 --extra-vars"delete_files=a b c",并且最终将变为 delete_files == 'a',因为 k=v 无法处理这些空间。
  • 愚蠢的问题:为什么要将其作为extra-vars 传递?为什么不从 Ansible 剧本中读取文件?

标签: jenkins groovy ansible


【解决方案1】:

一种解决方案是解析特性Ansible playbook 中的文件,带有 ini lookup,如果您确实在 localhost 上操作,就像您在 playbook 中显示的那样:

- hosts: localhost
  gather_facts: no

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

【讨论】:

    【解决方案2】:

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

             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 pb.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() }}"
    

    给,运行--检查--差异模式

    shell> ansible-playbook pb.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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多