Screen command for Ansible - ansible

Is there anyway I can use the screen command with ansible?
Would be great if we can screen logs while installing applications to keep full logs.

You can use shell module and run screen with "bash -c", and add | tee command to write all output to file. Or use screen with parameter "-Logfile file"
ansible -i ${inventory} -m shell -a "screen -mdSA screenname bash -c 'ls -lah | tee /tmp/screenname-$(date +%Y-%m-%d).log.txt'"

Related

Shell Script - SSH using Screen

I am trying to develop a shell script / bash script with ssh and screen, using Ubuntu / Linux.
Could you help me to correct what is wrong here?
#!/bin/bash
ssh -i keypair.pem -t ubuntu#ec2-address "screen -S teste"; cd 'home'; cd 'Shell'; ./'AWS - teste.sh' "
It doesn't work.
You fell victim to shell quoting - which is tricky when using ssh. Your command does something alike
cd home/Shell; ./AWS - teste.sh
so I would try
ssh -i keypair.pem -t ubuntu#ec2-address screen -S teste "sh -c \"cd home/Shell; ./AWS - teste.sh\""

How to ssh to a server and get CPU and memory details?

I am writing a shell script where i want to ssh to a server and get the cpu and memory details data of that displayed as a result. I’m using the help of top command here.
Script line:
ssh -q user#host -n “cd; top -n 1 | egrep ‘Cpu|Mem|Swap’”
But the result is
TERM environment variable is not set.
I had checked the same in the server by entering set | grep TERM and got result as TERM=xterm
Please someone help me on this. Many thanks.
Try using the top -b flag:
ssh -q user#host -n "cd; top -bn 1 | egrep 'Cpu|Mem|Swap'"
This tells top to run non-interactively, and is intended for this sort of use.
top need an environment. You have to add the parameter -t to get the result:
ssh -t user#host -n "top -n 1 | egrep 'Cpu|Mem|Swap'"
Got it..!! Need to make a small modification for the below script line.
ssh -t user#host -n "top -n 1 | egrep 'Cpu|Mem|Swap'"
Instead of -t we need to give -tt. It worked for me.
To execute command top after ssh’ing. It requires a tty to run. Using -tt it will enable a force pseudo-tty allocation.
Thanks stony for providing me a close enough answer!! :)

Using shell expansion with Ansible

I'm trying to execute a remote command via Ansible which requires gathering the PID of the process:
ansible webserver -m shell -a 'jstack -l $(pgrep -f java)'
However it seems Ansible is not able to expand the shell command contained in parenthesis (tried as well with grave accent):
127.0.0.1 | FAILED | rc=1 >>
Usage:
jstack [-l] <pid>
Executing just the command in the expansion reveals that expansion does not take place:
ansible webserver -a 'echo $(pgrep -f java)'
192.168.0.1 | success | rc=0 >>
$(pgrep -f java)
You'll want to escape the $ dollar sign, like so:
ansible all -i inventories/prod/hosts -m shell -a "echo \$(pgrep -f java)"

Fish shell input redirection from subshell output

When I want to run Wireshark locally to display a packet capture running on another machine, this works on bash, using input redirection from the output of a subshell:
wireshark -k -i <(ssh user#machine "sudo dumpcap -P -w - -f '<filter>' -i eth0")
From what I could find, the syntax for similar behavior on the fish shell is the same but when I run that command on fish, I get the Wireshark output on the terminal but can't see the Wireshark window.
Is there something I'm missing?
What you're using there in bash is process substitution (the <() syntax). It is a bash specific syntax (although zsh adopted this same syntax along with its own =()).
fish does have process substitution under a different syntax ((process | psub)). For example:
wireshark -k -i (ssh user#machine "sudo dumpcap -P -w - -f '<filter>' -i eth0" | psub)
bash | equivalent in fish
----------- | ------------------
cat <(ls) | cat (ls|psub)
ls > >(cat) | N/A (need to find a way to use a pipe, e.g. ls|cat)
The fish equivalent of <() isn't well suited to this use case. Is there some reason you can't use this simpler and more portable formulation?
ssh user#machine "sudo dumpcap -P -w - -f '<filter>' -i eth0" | wireshark -k -i -

How can I start a screen session using a specific config file?

I would like to be able to start a screen session using a certain config file. I know that I can use -c and then the file path to the config file but if I do that, then the sh script I am using does not work. You can see the sh script below:
#!/bin/bash
cd /media/kiancross/Minecraft_Server/1.6.4
screen -d -m -S MinecraftServer ./start.sh
screen -r MinecraftServer
I would have thought that I can do the following code:
#!/bin/bash
cd /media/kiancross/Minecraft_Server/1.6.4
screen -d -m -S -c MinecraftServer $HOME/config_file/mcserver.config ./start.sh
screen -r MinecraftServer
But then I get a message saying:
There is no screen to be resumed matching MinecraftServer.
After then checking to see if there is a screen session running it says that there are no screen sessions running
No Sockets found in /var/run/screen/S-kiancross.
Does anybody know how I can do this so that I can use a custom config file?
The command should be:
screen -d -m -S MinecraftServer -c $HOME/config_file/mcserver.config ./start.sh
The name of the screen session goes after -S and the path of the config file goes after -c. You inserted -c before the screen name.

Resources