Getting : error while evaluating conditional (my_result.stat.exists): 'bool object' has no attribute 'stat' while expcting to be dictionary

35 views
Skip to first unread message

Meir Yanovich

unread,
May 5, 2020, 6:26:00 AM5/5/20
to Ansible Project
i have this ansible simple playbook :
where "/home/ec2-user/test1.txt" exists on disk 

   
 ---
   
- name: check if exsist
      hosts
: localhost
      gather_facts
: false
      tasks
:
   
       
- name: Get project name
          shell
: "echo 'hhhhhhhh'"
         
register: name_var
   
   
       
- name: Check if thetest.log exists in path
          stat
:
            path
: "/home/ec2-user/test1.txt1"
         
register: log_result_1
   
       
- name: Check if TheTest.log exists in p_home
          stat
:
            path
: "/home/ec2-user/test1.txt"
         
register: log_result_2
         
when: name_var.stdout is match("hhhhhhhh")
   
       
- set_fact:
            my_result
: "{{ log_result_1.stat.exists or (log_result_2 is not skipped and log_result_2.stat.exists) }}"
   
       
- debug:
            msg
: "#################################### result_value ######################################"
       
- debug:
           
var: my_result
   
   
       
- name: Create temporary log folder
          file
:
            path
: "/home/ec2-user/log_tmp"
            state
: directory
         
when: my_result.stat.exists







I expect that the my_result will have the value of the right register in this case log_result_2

but I'm getting this error:

 
  ansible-playbook 2.9.7
      config file
= /etc/ansible/ansible.cfg
      configured
module search path = ['/home/ec2-user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
      ansible python
module location = /home/ec2-user/venv_ansible_new/lib64/python3.7/site-packages/ansible
      executable location
= /home/ec2-user/venv_ansible_new/bin/ansible-playbook
      python version
= 3.7.6 (default, Feb 26 2020, 20:54:15) [GCC 7.3.1 20180712 (Red Hat 7.3.1-6)]
   
Using /etc/ansible/ansible.cfg as config file
   
[WARNING]: No inventory was parsed, only implicit localhost is available
   
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
   
    PLAYBOOK
: test3.yml *************************************************************************************************************************************************************************************************************************
   
1 plays in test3.yml
   
    PLAY
[check if exsist] **********************************************************************************************************************************************************************************************************************
    META
: ran handlers
   
    TASK
[Get project name] *********************************************************************************************************************************************************************************************************************
    task path
: /home/ec2-user/test3.yml:7
   
Tuesday 05 May 2020  09:56:22 +0000 (0:00:00.070)       0:00:00.070 ***********
    changed
: [localhost] => changed=true
      cmd
: echo 'hhhhhhhh'
      delta
: '0:00:00.002594'
     
end: '2020-05-05 09:56:22.684370'
      rc
: 0
      start
: '2020-05-05 09:56:22.681776'
      stderr
: ''
      stderr_lines
: <omitted>
      stdout
: hhhhhhhh
      stdout_lines
: <omitted>
   
    TASK
[Check if thetest.log exists in path] **************************************************************************************************************************************************************************************************
    task path
: /home/ec2-user/test3.yml:12
   
Tuesday 05 May 2020  09:56:22 +0000 (0:00:00.256)       0:00:00.327 ***********
    ok
: [localhost] => changed=false
      stat
:
        exists
: false
   
    TASK
[Check if TheTest.log exists in p_home] ************************************************************************************************************************************************************************************************
    task path
: /home/ec2-user/test3.yml:17
   
Tuesday 05 May 2020  09:56:22 +0000 (0:00:00.250)       0:00:00.577 ***********
    ok
: [localhost] => changed=false
      stat
:
        atime
: 1588660417.6106787
        attr_flags
: ''
        attributes
: []
        block_size
: 4096
        blocks
: 8
        charset
: us-ascii
        checksum
: 75bae7e3ee07809acd2477a8427b5b32e605a6b4
        ctime
: 1588660417.6106787
        dev
: 66305
        device_type
: 0
        executable
: false
        exists
: true
        gid
: 1000
        gr_name
: ec2-user
        inode
: 77678
        isblk
: false
        ischr
: false
        isdir
: false
        isfifo
: false
        isgid
: false
        islnk
: false
        isreg
: true
        issock
: false
        isuid
: false
        mimetype
: text/plain
        mode
: '0664'
        mtime
: 1588660417.6106787
        nlink
: 1
        path
: /home/ec2-user/test1.txt
        pw_name
: ec2-user
        readable
: true
        rgrp
: true
        roth
: true
        rusr
: true
        size
: 18
        uid
: 1000
        version
: '1015088889'
        wgrp
: true
        woth
: false
        writeable
: true
        wusr
: true
        xgrp
: false
        xoth
: false
        xusr
: false
   
    TASK
[set_fact] *****************************************************************************************************************************************************************************************************************************
    task path
: /home/ec2-user/test3.yml:23
   
Tuesday 05 May 2020  09:56:23 +0000 (0:00:00.188)       0:00:00.765 ***********
    ok
: [localhost] => changed=false
      ansible_facts
:
        my_result
: true
   
    TASK
[debug] ********************************************************************************************************************************************************************************************************************************
    task path
: /home/ec2-user/test3.yml:26
   
Tuesday 05 May 2020  09:56:23 +0000 (0:00:00.030)       0:00:00.796 ***********
    ok
: [localhost] =>
      msg
: '#################################### result_value ######################################'
   
    TASK
[debug] ********************************************************************************************************************************************************************************************************************************
    task path
: /home/ec2-user/test3.yml:28
   
Tuesday 05 May 2020  09:56:23 +0000 (0:00:00.011)       0:00:00.808 ***********
    ok
: [localhost] =>
      my_result
: true
   
    TASK
[Create temporary log folder] **********************************************************************************************************************************************************************************************************
    task path
: /home/ec2-user/test3.yml:32
   
Tuesday 05 May 2020  09:56:23 +0000 (0:00:00.010)       0:00:00.819 ***********
    fatal
: [localhost]: FAILED! =>
      msg
: |-
       
The conditional check 'my_result.stat.exists' failed. The error was: error while evaluating conditional (my_result.stat.exists): 'bool object' has no attribute 'stat'
   
       
The error appears to be in '/home/ec2-user/test3.yml': line 32, column 7, but may
        be elsewhere
in the file depending on the exact syntax problem.
   
       
The offending line appears to be:
   
   
           
- name: Create temporary log folder
             
^ here



Stefan Hornburg (Racke)

unread,
May 5, 2020, 6:42:41 AM5/5/20
to ansible...@googlegroups.com
On 5/5/20 12:26 PM, Meir Yanovich wrote:
> i have this ansible simple playbook :
> where "/home/ec2-user/test1.txt" exists on disk 
>
>    
> |
>  ---
>     -name:check ifexsist
>       hosts:localhost
>       gather_facts:false
>       tasks:
>    
>         -name:Getproject name
>           shell:"echo 'hhhhhhhh'"
>           register:name_var
>    
>    
>         -name:Checkifthetest.log exists inpath
>           stat:
>             path:"/home/ec2-user/test1.txt1"
>           register:log_result_1
>    
>         -name:CheckifTheTest.log exists inp_home
>           stat:
>             path:"/home/ec2-user/test1.txt"
>           register:log_result_2
>           when:name_var.stdout ismatch("hhhhhhhh")
>    
>         -set_fact:
>             my_result:"{{ log_result_1.stat.exists or (log_result_2 is not skipped and log_result_2.stat.exists) }}"
>    
>         -debug:
>             msg:"#################################### result_value ######################################"
>         -debug:
>             var:my_result
>    
>    
>         -name:Createtemporary log folder
>           file:
>             path:"/home/ec2-user/log_tmp"
>             state:directory
>           when:my_result.stat.exists
>
> |
>
>
>
>
>
>
> I expect that the my_result will have the value of the right register in this case log_result_2

No the output is a boolean, variables are evaluated in your expression.

Regards
Racke

>
> but I'm getting this error:
>
>  
> |
>   ansible-playbook 2.9.7
>       config file =/etc/ansible/ansible.cfg
>       configured modulesearch path =['/home/ec2-user/.ansible/plugins/modules','/usr/share/ansible/plugins/modules']
>       ansible python modulelocation =/home/ec2-user/venv_ansible_new/lib64/python3.7/site-packages/ansible
>       executable location =/home/ec2-user/venv_ansible_new/bin/ansible-playbook
>       python version =3.7.6(default,Feb262020,20:54:15)[GCC 7.3.120180712(RedHat7.3.1-6)]
>     Using/etc/ansible/ansible.cfg asconfig file
>     [WARNING]:Noinventory was parsed,only implicitlocalhost isavailable
>     [WARNING]:provided hosts list isempty,only localhost isavailable.Notethat the implicitlocalhost does notmatch 'all'
>    
>     PLAYBOOK:test3.yml
> *************************************************************************************************************************************************************************************************************************
>     1plays intest3.yml
>    
>     PLAY [check
> ifexsist]**********************************************************************************************************************************************************************************************************************
>     META:ran handlers
>    
>     TASK [Getproject
> name]*********************************************************************************************************************************************************************************************************************
>     task path:/home/ec2-user/test3.yml:7
>     Tuesday05May2020 09:56:22+0000(0:00:00.070)      0:00:00.070***********
>     changed:[localhost]=>changed=true
>       cmd:echo 'hhhhhhhh'
>       delta:'0:00:00.002594'
>       end:'2020-05-05 09:56:22.684370'
>       rc:0
>       start:'2020-05-05 09:56:22.681776'
>       stderr:''
>       stderr_lines:<omitted>
>       stdout:hhhhhhhh
>       stdout_lines:<omitted>
>    
>     TASK [Checkifthetest.log exists
> inpath]**************************************************************************************************************************************************************************************************
>     task path:/home/ec2-user/test3.yml:12
>     Tuesday05May2020 09:56:22+0000(0:00:00.256)      0:00:00.327***********
>     ok:[localhost]=>changed=false
>       stat:
>         exists:false
>    
>     TASK [CheckifTheTest.log exists
> inp_home]************************************************************************************************************************************************************************************************
>     TASK [Createtemporary log
> folder]**********************************************************************************************************************************************************************************************************
>     task path:/home/ec2-user/test3.yml:32
>     Tuesday05May2020 09:56:23+0000(0:00:00.010)      0:00:00.819***********
>     fatal:[localhost]:FAILED!=>
>       msg:|-
>         Theconditional check 'my_result.stat.exists'failed.Theerror was:error whileevaluating conditional
> (my_result.stat.exists):'bool object'has noattribute 'stat'
>    
>         Theerror appears to be in'/home/ec2-user/test3.yml':line 32,column 7,but may
>         be elsewhere inthe file depending on the exact syntax problem.
>    
>         Theoffending line appears to be:
>    
>    
>             -name:Createtemporary log folder
>               ^here
>
> |
>
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/11ca92bf-0a19-486a-abac-875e5589bd21%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/11ca92bf-0a19-486a-abac-875e5589bd21%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc
Reply all
Reply to author
Forward
0 new messages