register output of shell command

I have what I thought was an easy problem to solve. A client has several different set ups of Debian 10 hosts. We are using ansible to install osqueryd and need to install the osqueryd.service file. I am trying to register the first result of the command:

“locate osqueryd.service.”

The error I am getting is:
“Invalid variable name is ‘register’ specified: 'osqueryd-service-file”

Here is my ansible code:

  • name: Locate osqueryd.service file
    command: “locate osqueryd.service”
    register: osqueryd-service-file

  • name: Print osqueryd.service file
    debug:
    msg: “osqueryd service file is osqueryd-service-file.results.1.stdout”

I don’t even get to the debug statement. Why won’t it let me register the results of the locate command?

Hi,

Try replacing the hyphens with underscores are per documentation below.

https://docs.ansible.com/archive/ansible/2.4/playbooks_variables.html#what-makes-a-valid-variable-name

Regards

Well, don’t I feel stupid about the hyphens. Thanks.

When I print out the results of the service file though, I get the following:

msg: “Osqueryd service file is /work/roles/wazuh/files/osqueryd.serice\n/usr/lib/systemd/system/osqueryd.service”

How do I get the results of the latter half - just /usr/lib/systemd/system/osqueryd.service?

Well, don’t I feel stupid about the hyphens. Thanks.

When I print out the results of the service file though, I get the following:

msg: “Osqueryd service file is /work/roles/wazuh/files/osqueryd.serice\n/usr/lib/systemd/system/osqueryd.service”

How do I get the results of the latter half - just /usr/lib/systemd/system/osqueryd.service?

According to your initial post you seem to construct that string yourself, using a variable.
Don’t use msg in the debug task but just var.

In general parsing output of ‘locate’ seems fragile and I would consider that more archeology than configuration management…

Hello,

Besides replacing the hyphens for underscore (as was already mentioned), aren’t you missing the {{}} in your msg statement?
msg: “osqueryd service file is {{ osqueryd-service-file.results.1.stdout}}”

Alex