I need to delete files and folders using a ansible playbook. I pass the file/foler paths as a variable to an Ansible playbook from a Groovy script.
Variables are in a properties file named delete.properties. I stored file/foler paths seperatly in a variables so I can change the paths as I need in future.
delete.properties:
delete_files=/home/new-user/myfolder/dltfolder1 /home/new-user/myfolder/dltfolder2 /home/new-user/myfolder/dltfolder3
Groovy script:
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 playbook:
---
- name: delete files
hosts: localhost
tasks:
- name: delete files
file:
path: "{{ delete_files }}"
state: absent
As a result of above codes, only the first file path in delete_files
(/home/new-user/myfolder/dltfolder1) variable in delete.properties file gets deleted.
I need to delete the other file/folder paths included in the delete_files
variable too.
CodePudding user response:
One solution would be to parse the properties file inside the Ansible playbook, with a ini
lookup, if you are indeed acting on localhost
, as you are showing it in your playbook:
- hosts: localhost
gather_facts: no
tasks:
- file:
path: "{{ item }}"
state: absent
loop: >-
{{
lookup(
'ini',
'delete_files type=properties file=delete.properties'
).split()
}}
CodePudding user response:
Put the path of the file into the extra vars. For example,
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"""
Then, given the tree
shell> tree /tmp/test
/tmp/test
├── f1
├── f2
└── f3
, the file
shell> cat /tmp/delete.properties
delete_files=/tmp/test/f1 /tmp/test/f2 /tmp/test/f3
, and the playbook
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, running in --check --diff mode
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