Get specifics from Ansible output

First of all, this is what I’m trying to do. I want to check first if certain directories are in fact directories and not symlinks. If they happen to be directories I want to rename them to {{ item }}.old and then after that task I want to run a task that will replace the dirs with symlinks.
My approach to do this now is to collect the stat information from a list of directories. Then I want to iterate through the information collected by Ansible and if the item is a dir (sym.stat.isdir == True) then I want to move it. After that task is complete I’ll run a new task that will create the symlinks to replace the dirs.

But I have a problem with the iteration process. A single directory is no problem, but it becomes unclear for me when a list of directories with the stat module is used. I assume this is possible, what’s the use of this information if you can’t use it :slight_smile:

So this is what I’ve already tried.

The following task:

  • name: check if home dirs are symlinks
    stat: path=/home/keesj/{{ item }}
    with_items:

  • Documents

  • Music

  • Pictures

  • Videos
    register: sym
    tags: sym

  • debug: var=sym.results

tags: sym

Has as output:

[root@defiant Ansible]# ansible-playbook -i hosts site.yml -t sym

PLAY [192.168.124.126] ********************************************************

GATHERING FACTS ***************************************************************
Enter passphrase for key ‘/root/.ssh/id_rsa’:
ok: [192.168.124.126]

TASK: [common | check if home dirs are symlinks] ******************************
ok: [192.168.124.126] => (item=Documents)
ok: [192.168.124.126] => (item=Music)
ok: [192.168.124.126] => (item=Pictures)
ok: [192.168.124.126] => (item=Videos)

TASK: [common | debug var=sym.results] ****************************************
ok: [192.168.124.126] => {
“var”: {
“sym.results”: [
{
“changed”: false,
“invocation”: {
“module_args”: “path=/home/keesj/Documents”,
“module_complex_args”: {},
“module_name”: “stat”
},
“item”: “Documents”,
“stat”: {
“atime”: 1448615699.9801893,
“ctime”: 1448462998.0985308,
“dev”: 64768,
“exists”: true,
“gid”: 1000,
“gr_name”: “keesj”,
“inode”: 1449767,
“isblk”: false,
“ischr”: false,
“isdir”: true,
“isfifo”: false,
“isgid”: false,
“islnk”: false,
“isreg”: false,
“issock”: false,
“isuid”: false,
“mode”: “0755”,
“mtime”: 1448462998.0985308,
“nlink”: 2,
“path”: “/home/keesj/Documents”,
“pw_name”: “keesj”,
“rgrp”: true,
“roth”: true,
“rusr”: true,
“size”: 4096,
“uid”: 1000,
“wgrp”: false,
“woth”: false,
“wusr”: true,
“xgrp”: true,
“xoth”: true,
“xusr”: true
}
},
{
“changed”: false,
“invocation”: {
“module_args”: “path=/home/keesj/Music”,
“module_complex_args”: {},
“module_name”: “stat”
},
“item”: “Music”,
“stat”: {
“atime”: 1448615700.0871918,
“ctime”: 1448462998.0985308,
“dev”: 64768,
“exists”: true,
“gid”: 1000,
“gr_name”: “keesj”,
“inode”: 1449768,
“isblk”: false,
“ischr”: false,
“isdir”: true,
“isfifo”: false,
“isgid”: false,
“islnk”: false,
“isreg”: false,
“issock”: false,
“isuid”: false,
“mode”: “0755”,
“mtime”: 1448462998.0985308,
“nlink”: 2,
“path”: “/home/keesj/Music”,
“pw_name”: “keesj”,
“rgrp”: true,
“roth”: true,
“rusr”: true,
“size”: 4096,
“uid”: 1000,
“wgrp”: false,
“woth”: false,
“wusr”: true,
“xgrp”: true,
“xoth”: true,
“xusr”: true
}
},
{
“changed”: false,
“invocation”: {
“module_args”: “path=/home/keesj/Pictures”,
“module_complex_args”: {},
“module_name”: “stat”
},
“item”: “Pictures”,
“stat”: {
“atime”: 1448615700.1171927,
“ctime”: 1448462998.0985308,
“dev”: 64768,
“exists”: true,
“gid”: 1000,
“gr_name”: “keesj”,
“inode”: 1449769,
“isblk”: false,
“ischr”: false,
“isdir”: true,
“isfifo”: false,
“isgid”: false,
“islnk”: false,
“isreg”: false,
“issock”: false,
“isuid”: false,
“mode”: “0755”,
“mtime”: 1448462998.0985308,
“nlink”: 2,
“path”: “/home/keesj/Pictures”,
“pw_name”: “keesj”,
“rgrp”: true,
“roth”: true,
“rusr”: true,
“size”: 4096,
“uid”: 1000,
“wgrp”: false,
“woth”: false,
“wusr”: true,
“xgrp”: true,
“xoth”: true,
“xusr”: true
}
},
{
“changed”: false,
“invocation”: {
“module_args”: “path=/home/keesj/Videos”,
“module_complex_args”: {},
“module_name”: “stat”
},
“item”: “Videos”,
“stat”: {
“atime”: 1448615700.1201928,
“ctime”: 1448462998.0985308,
“dev”: 64768,
“exists”: true,
“gid”: 1000,
“gr_name”: “keesj”,
“inode”: 1449770,
“isblk”: false,
“ischr”: false,
“isdir”: true,
“isfifo”: false,
“isgid”: false,
“islnk”: false,
“isreg”: false,
“issock”: false,
“isuid”: false,
“mode”: “0755”,
“mtime”: 1448462998.0985308,
“nlink”: 2,
“path”: “/home/keesj/Videos”,
“pw_name”: “keesj”,
“rgrp”: true,
“roth”: true,
“rusr”: true,
“size”: 4096,
“uid”: 1000,
“wgrp”: false,
“woth”: false,
“wusr”: true,
“xgrp”: true,
“xoth”: true,
“xusr”: true
}
}
]
}
}

PLAY RECAP ********************************************************************
192.168.124.126 : ok=3 changed=0 unreachable=0 failed=0

It would be nice if these items could be read with a with_items: and then iterate through it. But nothing I haven’t been successful in finding a way to do that.

This may be something? But again, I’m stuck, tried a lot of things to get some info out of it, but nothing. Also can’t find any documentation about it for Ansible.

  • debug: var=sym.items

tags: sym

[root@defiant Ansible]# ansible-playbook -i hosts site.yml -t sym

PLAY [192.168.124.126] ********************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.124.126]

TASK: [common | check if home dirs are symlinks] ******************************
ok: [192.168.124.126] => (item=Documents)
ok: [192.168.124.126] => (item=Music)
ok: [192.168.124.126] => (item=Pictures)
ok: [192.168.124.126] => (item=Videos)

TASK: [common | debug var=sym.items] ******************************************
ok: [192.168.124.126] => {
“var”: {
“sym.items”: “<built-in method items of dict object at 0x7ff5fb9cf398>”
}
}

Any suggestions?