win_find - use result path

939 views
Skip to first unread message

Jeremie Levy

unread,
Jun 3, 2018, 8:55:44 AM6/3/18
to Ansible Project
Hi
I can't find the way to use the result of win_find module.
I need to modify the files from the register and encounter some errors:

---
- hosts: all
  tasks:

  - name: Find files in path based on regex pattern
    win_find:
      paths: D:\b*\conf
      patterns: 'wrapper.conf'
      recurse: yes
    register: wrapper

  - name: output wrapper
    debug:
      var: wrapper.files
  
  - name: output wrapper list
    debug:
      var: 'wrapper.files[{{item}}].path'
    with_sequence:
      - start=0 end='{{wrapper.matched -1 | int}}'
    when:  wrapper.matched | int  > 0
  
  - name: output wrapper list2
    debug:
      var: "{{ item.path }}"
    with_items: "{{wrapper.files}}"


output:
# ansible-playbook playbooks/add_logon_server.yml --limit server1.domain -vvv
ansible-playbook 2.5.4                                                                                                                                   
  config file = /ansible/scripts/ansible.cfg                                                                                           
  configured module search path = [u'/usr/local/lib/python2.7/dist-packages/ara/plugins/modules']                                                        
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible                                                                              
  executable location = /usr/bin/ansible-playbook                                                                                                        
  python version = 2.7.12 (default, Dec  4 2017, 14:50:18) [GCC 5.4.0 20160609]                                                                          
Using /ansible/scripts/ansible.cfg as config file                                                                                      
Parsed /ansible/scripts/inventory/windows.yml inventory source with yaml plugin                                                        

PLAYBOOK: add_logon_server.yml *************************************************************************************************************************************************************************************************************************************
1 plays in playbooks/add_logon_server.yml                                                               

PLAY [all] *************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************************************************************
task path: /ansible/scripts/playbooks/add_logon_server.yml:2                                            
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/windows/setup.ps1                                                              
<server1.domain> ESTABLISH WINRM CONNECTION FOR USER: ansible@DOMAIN on PORT 5986 TO server1.domain                 
EXEC (via pipeline wrapper)                                                               
ok: [server1.domain]                                               
META: ran handlers                                                              

TASK [Find files in path based on regex pattern] *************************************************************************************************************************************************************************************************************************************
task path: /ansible/scripts/playbooks/add_logon_server.yml:5                                            
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/windows/win_find.ps1                                                               
<server1.domain> ESTABLISH WINRM CONNECTION FOR USER: ansible@DOMAIN on PORT 5986 TO server1.domain                 
EXEC (via pipeline wrapper)                                                               
ok: [server1.domain] => {                                                
    "changed": false,                                                               
    "examined": 19,                                                                 
    "files": [                                                                     
        {                                                                           
            "attributes": "Archive",                                                                      
            "checksum": "e81c7933b5712ddabc9ae97c4ab66e45a3782d4e",                                                                    
            "creationtime": 1521642182.5360816,                                                                                      
            "extension": ".conf",                                                                            
            "filename": "wrapper.conf",                                                                
            "isarchive": true,                                                                       
            "isdir": false,                                                                          
            "ishidden": false,                                                                      
            "islnk": false,                                                                          
            "isreadonly": false,                                                                    
            "isshared": false,                                                                      
            "lastaccesstime": 1521642182.5360816,                                                               
            "lastwritetime": 1526457412.4193811,                                                               
            "owner": "BUILTIN\\Administrators",                                                                 
            "path": "D:\\buildagent_maint\\conf\\wrapper.conf",                                                                
            "size": 8220                                                                                                      
        },                                                                                                                    
        {                                                                                                                      
            "attributes": "Archive",                                                                                
            "checksum": "03bf7f5b203db5cbae7d849ae7023729fa98dc1f",                                                                
            "creationtime": 1521653624.4557838,                                                                                    
            "extension": ".conf",                                                                                                  
            "filename": "wrapper.conf",                                                                                            
            "isarchive": true,                                                                                                    
            "isdir": false,                                                                                                        
            "ishidden": false,                                                                                                    
            "islnk": false,                                                                                                        
            "isreadonly": false,                                                                                                  
            "isshared": false,                                                                                                    
            "lastaccesstime": 1521653624.4557838,                                                                                  
            "lastwritetime": 1527974543.8368027,                                                                                  
            "owner": "BUILTIN\\Administrators",                                                                                    
            "path": "D:\\buildagent_prod\\conf\\wrapper.conf",                                                                    
            "size": 8199                                                                                                          
        },                                                                                                                        
        {                                                                                                                          
            "attributes": "Archive",                                                                                              
            "checksum": "baba388f60f595f9016f0cc6ceb4848c949d91fe",                                                                
            "creationtime": 1521653637.1302552,                                                                                    
            "extension": ".conf",                                                                                                  
            "filename": "wrapper.conf",                                                                                            
            "isarchive": true,                                                                                                    
            "isdir": false,                                                                                                        
            "ishidden": false,                                                                                                    
            "islnk": false,                                                                                                        
            "isreadonly": false,                                                                                                  
            "isshared": false,                                                                                                    
            "lastaccesstime": 1521653637.1302552,                                                                                  
            "lastwritetime": 1527974731.1164503,                                                                                  
            "owner": "BUILTIN\\Administrators",                                                                                    
            "path": "D:\\buildagent_prod_2\\conf\\wrapper.conf",                                                                  
            "size": 8192                                                                                                          
        }                                                                                                                          
    ],                                                                                                                            
    "matched": 3                                                                                                                  
}                                                                                                                                  

TASK [output wrapper] *************************************************************************************************************************************************************************************************************************************
task path: /ansible/scripts/playbooks/add_logon_server.yml:12                                                      
ok: [server1.domain] => {                                             
    "wrapper.files": [                                                                                                              
        {                                                                                                                            
            "attributes": "Archive",                                                                                                
            "checksum": "e81c7933b5712ddabc9ae97c4ab66e45a3782d4e",                                                                  
            "creationtime": 1521642182.5360816,                                                                                      
            "extension": ".conf",                                                                                                    
            "filename": "wrapper.conf",                                                                                              
            "isarchive": true,                                        
            "isdir": false,                                                                                                          
            "ishidden": false,                                                                                                      
            "islnk": false,                                                                                                          
            "isreadonly": false,                                                                                                    
            "isshared": false,                                                                                                      
            "lastaccesstime": 1521642182.5360816,                                                                                    
            "lastwritetime": 1526457412.4193811,                                                                                    
            "owner": "BUILTIN\\Administrators",                                                                                      
            "path": "D:\\buildagent_maint\\conf\\wrapper.conf",                                                                      
            "size": 8220                                                                                                            
        },                                                                                                                          
        {                                                                                                                            
            "attributes": "Archive",                                                                                                
            "checksum": "03bf7f5b203db5cbae7d849ae7023729fa98dc1f",                                                                  
            "creationtime": 1521653624.4557838,                                                                                      
            "extension": ".conf",                                                                                                    
            "filename": "wrapper.conf",                                                                                              
            "isarchive": true,                                                                                                      
            "isdir": false,                                                                                                          
            "ishidden": false,                                                                                                      
            "islnk": false,                                                                                                          
            "isreadonly": false,
            "isshared": false,
            "lastaccesstime": 1521653624.4557838,
            "lastwritetime": 1527974543.8368027,
            "owner": "BUILTIN\\Administrators",
            "path": "D:\\buildagent_prod\\conf\\wrapper.conf",
            "size": 8199
        },
        {
            "attributes": "Archive",
            "checksum": "baba388f60f595f9016f0cc6ceb4848c949d91fe",
            "creationtime": 1521653637.1302552,
            "extension": ".conf",
            "filename": "wrapper.conf",
            "isarchive": true,
            "isdir": false,
            "ishidden": false,
            "islnk": false,
            "isreadonly": false,
            "isshared": false,
            "lastaccesstime": 1521653637.1302552,
            "lastwritetime": 1527974731.1164503,
            "owner": "BUILTIN\\Administrators",
            "path": "D:\\buildagent_prod_2\\conf\\wrapper.conf",
            "size": 8192
        }
    ]
}

TASK [output wrapper list] *************************************************************************************************************************************************************************************************************************************
task path: /ansible/scripts/playbooks/add_logon_server.yml:16
ok: [server1.domain] => (item=None) => {
    "wrapper.files[0].path": "D:\\buildagent_maint\\conf\\wrapper.conf"
}
ok: [server1.domain] => (item=None) => {
    "wrapper.files[1].path": "D:\\buildagent_prod\\conf\\wrapper.conf"
}
ok: [server1.domain] => (item=None) => {
    "wrapper.files[2].path": "D:\\buildagent_prod_2\\conf\\wrapper.conf"
}

TASK [output wrapper list2] *************************************************************************************************************************************************************************************************************************************
task path: /ansible/scripts/playbooks/add_logon_server.yml:23
The full traceback is:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/ansible/executor/task_executor.py", line 104, in run
    item_results = self._run_loop(items)
  File "/usr/lib/python2.7/dist-packages/ansible/executor/task_executor.py", line 329, in _run_loop
    res = self._execute(variables=task_vars)
  File "/usr/lib/python2.7/dist-packages/ansible/executor/task_executor.py", line 561, in _execute
    result = self._handler.run(task_vars=variables)
  File "/usr/lib/python2.7/dist-packages/ansible/plugins/action/debug.py", line 62, in run
    results = self._templar.template("{{" + results + "}}", convert_bare=True, fail_on_undefined=True)
  File "/usr/lib/python2.7/dist-packages/ansible/template/__init__.py", line 481, in template
    disable_lookups=disable_lookups,
  File "/usr/lib/python2.7/dist-packages/ansible/template/__init__.py", line 691, in do_template
    data = _escape_backslashes(data, myenv)
  File "/usr/lib/python2.7/dist-packages/ansible/template/__init__.py", line 125, in _escape_backslashes
    for token in jinja_env.lex(d2):
  File "/usr/local/lib/python2.7/dist-packages/jinja2/lexer.py", line 739, in tokeniter
    name, filename)
TemplateSyntaxError: unexpected char u'\\' at 4
  line 1

fatal: [server1.domain]: FAILED! => {
    "msg": "Unexpected failure during module execution.",
    "stdout": ""
}
        to retry, use: --limit @/ansible/scripts/playbooks/add_logon_server.retry

PLAY RECAP *************************************************************************************************************************************************************************************************************************************
server1.domain : ok=4    changed=0    unreachable=0    failed=1


Can somoeone point out my issue here?

Thanks!

Kai Stian Olstad

unread,
Jun 3, 2018, 10:16:07 AM6/3/18
to ansible...@googlegroups.com
var: takes the name of a variable, not the content of one.

So the correct syntax would be.
- name: output wrapper list2
debug:
var: item.path
with_items: "{{wrapper.files}}"


An alternative is to use debugs msg.

- name: output wrapper list2
debug:
msg: "The file name is {{ item.path }}"
with_items: "{{wrapper.files}}"


--
Kai Stian Olstad

Jeremie Levy

unread,
Jun 3, 2018, 12:15:44 PM6/3/18
to ansible...@googlegroups.com
It worked like a charm, i wasn't aware of that, thank you for your help!



--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/BHiA_hApkKY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/cfc323b670ca2f9f5986c682720b0af9%40olstad.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages