How to modify a list of dictionaries with Ansible
For the below scenario, what we'are trying to solve to do?
- Looking for directories and change their permissions
- Change the permission non-recursive
- Change just the folder that match with the specific permission
In case we are using it in a task and we want to include tags to run just the specific block. Should be define the tasks inside a block as I did below. Otherwise, you just need to remove it from the block.
With the tasks below we just get the list of directories defined in the task - name: "Fix :: Get the list of directories"
option paths, and save the values in the found_directories variable, which will be read and showd in the next task, - name: "Fix :: Dump the list of directories"
, this last one is just to dump the values of the directory and it current permission mode. In the last task - name: "Fix :: Adjusting the directories permission"
the ideia is just change the permission of the directories that we found and saved in found_directories
, but just when the item.mode
the permission of that directory is equal the value defined in the var config_match_mode
.
Variables that should be defined:
- config_match_mode = Just change permissions when the current permission match with the value defined in this variable
- config_home_mode = New directory permission
- name: "Fix :: Permission issue"
block:
# Get the list of directories inside path...
- name: "Fix :: Get the list of directories"
find:
paths: "/home/"
#excludes: "some-dir-to-exclude"
recurse: no
file_type: directory
register: found_directories
- name: "Fix :: Dump the list of directories"
debug:
msg:
- "Directory: {{ item.path }}"
- "Mode: {{ item.mode }}"
with_items: "{{ found_directories.files }}"
- name: "Fix :: Adjusting the directories permission"
file:
path: "{{ item.path }}"
mode: "{{ config_home_mode }}"
become: true
with_items: "{{ found_directories.files }}"
when: item.mode == config_match_mode
tags: home_perm
ua# $ ls -la /tmp/home/
total 28
drwxrwxr-x 5 lpereira lpereira 4096 Jun 24 14:22 .
drwxrwxrwt 39 root root 12288 Jun 24 14:31 ..
drwxrw-rw- 2 luiz luiz 4096 Jun 24 14:22 luiz
drwxrwxr-x 2 paulo paulo 4096 Jun 24 14:22 paulo
drwxrwxrwx 2 pedro pedro 4096 Jun 24 14:22 pedro
$ ap playbooks/check-perm-config.yml
PLAY [Change permissions] ***********************************************************************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Get the list of directories] **************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [DUMP List of directories] *****************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'path': '/tmp/home/luiz', 'mode': '0766', 'isdir': True, 'ischr': False, 'isblk': False, 'isreg': False, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 1004, 'gid': 1005, 'size': 4096, 'inode': 29123031, 'dev': 64769, 'nlink': 2, 'atime': 1656077470.6332905, 'mtime': 1656076947.7835858, 'ctime': 1656077436.8711748, 'gr_name': 'luiz', 'pw_name': 'luiz', 'wusr': True, 'rusr': True, 'xusr': True, 'wgrp': True, 'rgrp': True, 'xgrp': False, 'woth': True, 'roth': True, 'xoth': False, 'isuid': False, 'isgid': False}) => {
"msg": [
"/tmp/home/luiz"
]
}
ok: [localhost] => (item={'path': '/tmp/home/pedro', 'mode': '0777', 'isdir': True, 'ischr': False, 'isblk': False, 'isreg': False, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 1002, 'gid': 1004, 'size': 4096, 'inode': 29123032, 'dev': 64769, 'nlink': 2, 'atime': 1656077466.5815005, 'mtime': 1656076950.2316906, 'ctime': 1656077427.8197353, 'gr_name': 'pedro', 'pw_name': 'pedro', 'wusr': True, 'rusr': True, 'xusr': True, 'wgrp': True, 'rgrp': True, 'xgrp': True, 'woth': True, 'roth': True, 'xoth': True, 'isuid': False, 'isgid': False}) => {
"msg": [
"/tmp/home/pedro"
]
}
ok: [localhost] => (item={'path': '/tmp/home/paulo', 'mode': '0775', 'isdir': True, 'ischr': False, 'isblk': False, 'isreg': False, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 1006, 'gid': 1006, 'size': 4096, 'inode': 29131223, 'dev': 64769, 'nlink': 2, 'atime': 1656077467.35346, 'mtime': 1656076952.7197974, 'ctime': 1656077459.489878, 'gr_name': 'paulo', 'pw_name': 'paulo', 'wusr': True, 'rusr': True, 'xusr': True, 'wgrp': True, 'rgrp': True, 'xgrp': True, 'woth': False, 'roth': True, 'xoth': True, 'isuid': False, 'isgid': False}) => {
"msg": [
"/tmp/home/paulo"
]
}
TASK [Change the directories permission] ********************************************************************************************************************************************************************************************
changed: [localhost] => (item={'path': '/tmp/home/luiz', 'mode': '0766', 'isdir': True, 'ischr': False, 'isblk': False, 'isreg': False, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 1004, 'gid': 1005, 'size': 4096, 'inode': 29123031, 'dev': 64769, 'nlink': 2, 'atime': 1656077470.6332905, 'mtime': 1656076947.7835858, 'ctime': 1656077436.8711748, 'gr_name': 'luiz', 'pw_name': 'luiz', 'wusr': True, 'rusr': True, 'xusr': True, 'wgrp': True, 'rgrp': True, 'xgrp': False, 'woth': True, 'roth': True, 'xoth': False, 'isuid': False, 'isgid': False})
changed: [localhost] => (item={'path': '/tmp/home/pedro', 'mode': '0777', 'isdir': True, 'ischr': False, 'isblk': False, 'isreg': False, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 1002, 'gid': 1004, 'size': 4096, 'inode': 29123032, 'dev': 64769, 'nlink': 2, 'atime': 1656077466.5815005, 'mtime': 1656076950.2316906, 'ctime': 1656077427.8197353, 'gr_name': 'pedro', 'pw_name': 'pedro', 'wusr': True, 'rusr': True, 'xusr': True, 'wgrp': True, 'rgrp': True, 'xgrp': True, 'woth': True, 'roth': True, 'xoth': True, 'isuid': False, 'isgid': False})
changed: [localhost] => (item={'path': '/tmp/home/paulo', 'mode': '0775', 'isdir': True, 'ischr': False, 'isblk': False, 'isreg': False, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 1006, 'gid': 1006, 'size': 4096, 'inode': 29131223, 'dev': 64769, 'nlink': 2, 'atime': 1656077467.35346, 'mtime': 1656076952.7197974, 'ctime': 1656077459.489878, 'gr_name': 'paulo', 'pw_name': 'paulo', 'wusr': True, 'rusr': True, 'xusr': True, 'wgrp': True, 'rgrp': True, 'xgrp': True, 'woth': False, 'roth': True, 'xoth': True, 'isuid': False, 'isgid': False})
PLAY RECAP **************************************************************************************************************************************************************************************************************************
localhost : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$ ls -la /tmp/home/
total 28
drwxrwxr-x 5 lpereira lpereira 4096 Jun 24 14:37 .
drwxrwxrwt 39 root root 12288 Jun 24 14:38 ..
drwxr-x--- 2 luiz luiz 4096 Jun 24 14:22 luiz
drwxr-x--- 2 paulo paulo 4096 Jun 24 14:22 paulo
drwxr-x--- 2 pedro pedro 4096 Jun 24 14:22 ped
No comments