Ansible changed_when syntax help

111 views
Skip to first unread message

Nitrous

unread,
Apr 24, 2022, 2:37:27 PM4/24/22
to Ansible Project
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"

Dick Visser

unread,
Apr 25, 2022, 12:15:54 AM4/25/22
to ansible...@googlegroups.com
 'register' is a task level parameter, you should indent it one level less

Sent from a mobile device

From: ansible...@googlegroups.com <ansible...@googlegroups.com> on behalf of Nitrous <wassama...@gmail.com>
Sent: Sunday, April 24, 2022 8:37:27 PM
To: Ansible Project <ansible...@googlegroups.com>
Subject: [ansible-project] Ansible changed_when syntax help
 
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8590cd8b-29a8-4793-9171-bd43d3900acen%40googlegroups.com.

Nitrous

unread,
Apr 25, 2022, 9:16:46 AM4/25/22
to Ansible Project
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

Dick Visser

unread,
Apr 25, 2022, 9:24:03 AM4/25/22
to ansible...@googlegroups.com
On 2022-04-24 (Sun) 20:37, Nitrous wrote:
> 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



--
Dick Visser
GÉANT

OpenPGP_signature

Nitrous

unread,
Apr 25, 2022, 9:30:39 AM4/25/22
to Ansible Project
Thanks, when you say "indented one level less", can you please tell me where it should be located?
Reply all
Reply to author
Forward
0 new messages