Issuew when doing a When conditional with win_copy

10 views
Skip to first unread message

Tom Donnly

unread,
Sep 18, 2019, 12:04:31 PM9/18/19
to Ansible Development
Hello all,
    I am trying to do a conditional based on the output of a win_shell.  When I run this it's as if it doesn't see the conditional and copies to to all inventory not just what is coming back as True.  Any ideas?

---
hostsall
  tasks:
  - nameRun multi-lined shell commands
    win_shell|
      if (Test-Path “HKLM:\Software\Microsoft\Microsoft SQL Server\Instance Names\SQL”) { write-host “True” } Else { write-host “False” }
    registerresults
  - nameProcess win_shell output
    set_fact:
      output"{{ results.stdout }}" 
  -  win_copy:
       srcfiles/
       destc:\support
       Whenansible_facts['results.stdout'] == "True"

Matt Martz

unread,
Sep 18, 2019, 12:47:43 PM9/18/19
to Tom Donnly, Ansible Development
Your `when` statement, needs to be indented to the same level as `win_copy`, not the same level as `dest`. Also it needs to be the lowercased `when`

--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-deve...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-devel/f26fe725-e8c7-40f7-92dd-6a2cba3d32ec%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

TD

unread,
Sep 18, 2019, 1:07:28 PM9/18/19
to Ansible Development
Matt, I now see it is attempting it but I get and erroron win_copy:{
    "msg": "The conditional check 'ansible_facts['results.stdout'] == \"True\"' failed. The error was: error while evaluating conditional (ansible_facts['results.stdout'] == \"True\"): 'dict object' has no attribute 'results.stdout'\n\nThe error appears to be in '/var/lib/awx/projects/_8__test_project/Win_Feature_SQL.yml': line 11, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n      output: \"{{ results.stdout }}\"\n  -  win_copy:\n     ^ here\n",
    "_ansible_no_log": false
}

I do see my results from win_shell though:

{
    "changed": false,
    "ansible_facts": {
        "output": "True\n"
    },
    "_ansible_no_log": false
}




On Wednesday, September 18, 2019 at 12:47:43 PM UTC-4, Matt Martz wrote:
Your `when` statement, needs to be indented to the same level as `win_copy`, not the same level as `dest`. Also it needs to be the lowercased `when`

On Wed, Sep 18, 2019 at 11:04 AM Tom Donnly <tom....@gmail.com> wrote:
Hello all,
    I am trying to do a conditional based on the output of a win_shell.  When I run this it's as if it doesn't see the conditional and copies to to all inventory not just what is coming back as True.  Any ideas?

---
hostsall
  tasks:
  - nameRun multi-lined shell commands
    win_shell|
      if (Test-Path “HKLM:\Software\Microsoft\Microsoft SQL Server\Instance Names\SQL”) { write-host “True” } Else { write-host “False” }
    registerresults
  - nameProcess win_shell output
    set_fact:
      output"{{ results.stdout }}" 
  -  win_copy:
       srcfiles/
       destc:\support
       Whenansible_facts['results.stdout'] == "True"

--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansibl...@googlegroups.com.

Matt Martz

unread,
Sep 18, 2019, 1:18:43 PM9/18/19
to TD, Ansible Development
`results.stdout` is not a literal variable name.  Instead what you are likely looking for is:

when: results.stdout|trim == 'True'

or since you use `set_fact` in between, it could also be:

when: output|trim == 'True'

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-deve...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-devel/558e8115-f593-481a-a660-6ad035f9c7bf%40googlegroups.com.

TD

unread,
Sep 18, 2019, 1:52:23 PM9/18/19
to Ansible Development
That worked, Thanks for the help Matt.  Much appreciated


On Wednesday, September 18, 2019 at 1:18:43 PM UTC-4, Matt Martz wrote:
`results.stdout` is not a literal variable name.  Instead what you are likely looking for is:

when: results.stdout|trim == 'True'

or since you use `set_fact` in between, it could also be:

when: output|trim == 'True'

Reply all
Reply to author
Forward
0 new messages