Reputation: 435
My script is as follows:
#!/bin/bash
if lscpu | grep 'CPU max MHz' >/dev/null 2>&1; then
cpu_speed=$(lscpu | grep 'CPU max MHz' | cut -d':' -f2 | awk '{print $1}')
else
cpu_speed=$(lscpu | grep 'CPU MHz' | cut -d':' -f2 | awk '{print $1}')
fi
echo -ne "${cpu_speed}"
It adds newline in the output as follows:
"stdout": "\r\n4000.0000",
Even if my script file is empty, it still adds newline in stdout as follows:
"stdout": "\r\n"
My module is as follows:
- name: Get Processor Speed
script: files/get_cpu_speed.sh
register: cpu_speed_output
become: yes
So, as you can see it does sudo.
I was trying to find any references online if leading newline is common with ansible script command, but there is none.
So, what could be causing the leading line in my case?
Debug output is as follows:
ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=testuser -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/c4ee167dc2 -tt myserver1 '/bin/sh -c '"'"'sudo -H -S -p "[sudo via ansible, key=epollvusfsfrfsfsfsfmrxdcida] password: " -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-epollvusfsfrfsfsfsfmrxdcida; /home/testuser/.ansible/tmp/ansible-tmp-1557164925.73-152013190475693/get_cpu_speed.sh'"'"'"'"'"'"'"'"' && sleep 0'"'"''
Upvotes: 1
Views: 453
Reputation: 311605
I am unable to reproduce that behavior. If I run your code exactly as presented, I don't see any leading newline. In any case, there is an easy workaround available. Instead of referring to:
cpu_speed_output.stdout
You can refer to:
cpu_speed_output.stdout.strip()
The strip()
method will strip any leading or trailing whitespace.
Upvotes: 2