# Ansible changed\_when syntax help

**URL:** <https://forum.ansible.com/t/ansible-changed-when-syntax-help/36557>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [April 24, 2022, 6:37pm UTC](https://forum.ansible.com/t/ansible-changed-when-syntax-help/36557 "2022-04-24T18:37:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nitrous](https://avatars.discourse-cdn.com/v4/letter/n/dfb087/32.png) [@Nitrous](https://forum.ansible.com/u/Nitrous)\
**Post date:** [April 24, 2022, 6:37pm UTC](https://forum.ansible.com/t/ansible-changed-when-syntax-help/36557/1 "2022-04-24T18:37:27Z")

</div>

After installing a software on the remote machine, and registering the task called “register: InstallCSQS” , I did a debug message and got the following:

ok: [sqldisktest] =\> {  
“msg”: {  
“changed”: true,  
“failed”: false,  
“rc”: 0,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “Installing Qualys and Crowdstrike on SQLDISKTEST\r\nWindows Sensor installed successfully.\r\nQualys installed successfully.\r\n”,  
“stdout\_lines”: [  
“Installing Qualys and Crowdstrike on SQLDISKTEST”,  
“Windows Sensor installed successfully.”,  
“Qualys installed successfully.”  
]  
}  
}

But when I add the below condition to my task, it fails:

changed\_when: “‘Installing Qualys and Crowdstrike’ not in InstallCSQS.stdout”

error:

fatal: [sqldisktest]: FAILED! =\> {  
“changed”: true,  
“changed\_when\_result”: “The conditional check ‘‘Installing Qualys and Crowdstrike’ not in InstallCSQS.stdout’ failed. The error was: error while evaluating conditional (‘Installing Qualys and Crowdstrike’ not in InstallCSQS.stdout): ‘InstallCSQS’ is undefined”,  
“checksum”: “ad4eb17ea1feee3bf9fbbf567dba6ce162ce1f0c”,  
“dest”: “C:\Scripts\WindowsSensor.exe”,  
“operation”: “file\_copy”,  
“original\_basename”: “WindowsSensor.exe”,  
“size”: 52183848,  
“src”: “/etc/ansible/roles/InstallCSQS/files/WindowsSensor.exe”  
}

playbook below:

- name: Copy Crowdstrike Installer  
ansible.windows.win\_copy:  
src: /etc/ansible/roles/InstallCSQS/files/WindowsSensor.exe  
dest: C:\Scripts\WindowsSensor.exe  
register: InstallCSQS  
changed\_when: “‘Installing Qualys and Crowdstrike’ not in InstallCSQS.stdout”

- name: Copy Qualys Installer  
ansible.windows.win\_copy:  
src: /etc/ansible/roles/InstallCSQS/files/QualysCloudAgent.exe  
dest: C:\Scripts\QualysCloudAgent.exe  
register: InstallCSQS  
changed\_when: “‘Installing Qualys and Crowdstrike’ not in InstallCSQS.stdout”

# execute PS script, and register the result in the variable named script\_run

- name: Install Crowdstrike and Qualys  
ansible.builtin.script: /etc/ansible/roles/onprembaseline/files/Qualys\_Crowdstrike\_installation.ps1  
#register: script\_run  
register: InstallCSQS  
changed\_when: “‘Installing Qualys and Crowdstrike’ not in InstallCSQS.stdout”

# debugs the stdout\_lines property of the variable named InstallCSQS in above task

- debug:  
msg: “{{InstallCSQS}}”

- name: Remove Crowdstrike Installer  
ansible.windows.win\_file:  
path: C:\Scripts\WindowsSensor.exe  
state: absent  
register: InstallCSQS  
changed\_when: “‘Installing Qualys and Crowdstrike’ not in InstallCSQS.stdout”

- name: Remove Qualys Installer  
ansible.windows.win\_file:  
path: C:\Scripts\QualysCloudAgent.exe  
state: absent  
register: InstallCSQS  
changed\_when: “‘Installing Qualys and Crowdstrike’ not in InstallCSQS.stdout”

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [April 25, 2022, 4:15am UTC](https://forum.ansible.com/t/ansible-changed-when-syntax-help/36557/2 "2022-04-25T04:15:38Z")

</div>

‘register’ is a task level parameter, you should indent it one level less

---

<div class="post-metadata">

**Author:** ![Nitrous](https://avatars.discourse-cdn.com/v4/letter/n/dfb087/32.png) [@Nitrous](https://forum.ansible.com/u/Nitrous)\
**Post date:** [April 25, 2022, 1:16pm UTC](https://forum.ansible.com/t/ansible-changed-when-syntax-help/36557/3 "2022-04-25T13:16:46Z")

</div>

Thanks, Im pretty new to ansible, so please bear with me.

So in my playbook, it calls that role, “InstallCSQS” , and in that role itself, it has tasks in it, as per my original post.

Playbook looks like this:

- hosts: ‘{{ hostname }}’  
gather\_facts: no  
tasks:

- name: Include vars for vcenter  
include\_vars:  
file: /etc/ansible/roles/createvm/vars/main.yml  
name: vcenter  
vars:  
ansible\_become\_password: “{{ domain\_password }}”

- name: Include vars of server.yaml file  
include\_vars:  
file: /etc/ansible/servers/{{ hostname }}.yaml  
name: server

- name: Wait For Connection to Continue  
wait\_for\_connection:  
connect\_timeout: 30

- name: Set timezone  
win\_timezone:  
timezone: ‘{{ server.timezone }}’

- import\_role:  
name: InstallCSQS

Where would I use I use the register variable?

Thanks

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [April 25, 2022, 1:23pm UTC](https://forum.ansible.com/t/ansible-changed-when-syntax-help/36557/4 "2022-04-25T13:23:45Z")

</div>

> playbook below:
> 
> - name: Copy Crowdstrike Installer  
> ansible.windows.win\_copy:  
> src: /etc/ansible/roles/InstallCSQS/files/WindowsSensor.exe  
> dest: C:\Scripts\WindowsSensor.exe  
> register: InstallCSQS

This needs to be intended one level less

> ```
> changed\_when: &quot;&#39;Installing Qualys and Crowdstrike&#39; not in InstallCSQS\.stdout&quot;
> 
> ```
> 
> - name: Copy Qualys Installer  
> ansible.windows.win\_copy:  
> src: /etc/ansible/roles/InstallCSQS/files/QualysCloudAgent.exe  
> dest: C:\Scripts\QualysCloudAgent.exe  
> register: InstallCSQS

This needs to be intended one level less

> ```
> changed\_when: &quot;&#39;Installing Qualys and Crowdstrike&#39; not in InstallCSQS\.stdout&quot;
> 
> ```
> 
> # execute PS script, and register the result in the variable named script\_run  
> - name: Install Crowdstrike and Qualys  
> ansible.builtin.script: /etc/ansible/roles/onprembaseline/files/Qualys\_Crowdstrike\_installation.ps1  
> #register: script\_run  
> register: InstallCSQS  
> changed\_when: "'Installing Qualys and Crowdstrike' not in InstallCSQS.stdout"
> 
> # debugs the stdout\_lines property of the variable named InstallCSQS in above task  
> - debug:  
> msg: "{{InstallCSQS}}"
> 
> - name: Remove Crowdstrike Installer  
> ansible.windows.win\_file:  
> path: C:\Scripts\WindowsSensor.exe  
> state: absent  
> register: InstallCSQS

This needs to be intended one level less

> ```
> changed\_when: &quot;&#39;Installing Qualys and Crowdstrike&#39; not in InstallCSQS\.stdout&quot;
> 
> ```
> 
> - name: Remove Qualys Installer  
> ansible.windows.win\_file:  
> path: C:\Scripts\QualysCloudAgent.exe  
> state: absent  
> register: InstallCSQS

This needs to be intended one level less

---

<div class="post-metadata">

**Author:** ![Nitrous](https://avatars.discourse-cdn.com/v4/letter/n/dfb087/32.png) [@Nitrous](https://forum.ansible.com/u/Nitrous)\
**Post date:** [April 25, 2022, 1:30pm UTC](https://forum.ansible.com/t/ansible-changed-when-syntax-help/36557/5 "2022-04-25T13:30:39Z")

</div>

Thanks, when you say “indented one level less”, can you please tell me where it should be located?
