Ansible changed_when syntax help

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”

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

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

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: "'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

This needs to be intended one level less

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

This needs to be intended one level less

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

This needs to be intended one level less

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