I want to know how I can write the equivalent of the following command in an ansible playbook.
echo \
"deb [arch=arm64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Check out > and | fold operators:
- name: Print a message
shell: >
echo "deb [arch=arm64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" |
tee /etc/apt/sources.list.d/docker.list > /dev/null
register: results
Here is the o/p of the the content in the file:
cat /etc/apt/sources.list.d/docker.list
deb [arch=arm64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu bionic stable
Related
I have created an image to run docker container with chrome. Below is my code. My dockerfile does compile into image. But whenever I try to run container from image I get error "Bootstrap.sh file not found" Although file is present in my FileSystem snapshot inside image. You can check screenshot.
Please help me resolve this issue I am new to docker.
FROM ubuntu:16.04
RUN apt-get update && apt-get clean && apt-get install -y \
x11vnc \
xvfb \
fluxbox \
wmctrl \
wget \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list \
&& apt-get update && apt-get -y install google-chrome-stable
RUN useradd apps \
&& mkdir -p /home/apps \
&& chown -v -R apps:apps /home/apps
COPY bootstrap.sh /
CMD '/bootstrap.sh'
BootStrap.sh file code:
#!/bin/bash
# Based on: http://www.richud.com/wiki/Ubuntu_Fluxbox_GUI_with_x11vnc_and_Xvfb
main() {
log_i "Starting xvfb virtual display..."
launch_xvfb
log_i "Starting window manager..."
launch_window_manager
log_i "Starting VNC server..."
run_vnc_server
}
launch_xvfb() {
local xvfbLockFilePath="/tmp/.X1-lock"
if [ -f "${xvfbLockFilePath}" ]
then
log_i "Removing xvfb lock file '${xvfbLockFilePath}'..."
if ! rm -v "${xvfbLockFilePath}"
then
log_e "Failed to remove xvfb lock file"
exit 1
fi
fi
# Set defaults if the user did not specify envs.
export DISPLAY=${XVFB_DISPLAY:-:1}
local screen=${XVFB_SCREEN:-0}
local resolution=${XVFB_RESOLUTION:-1280x960x24}
local timeout=${XVFB_TIMEOUT:-5}
# Start and wait for either Xvfb to be fully up or we hit the timeout.
Xvfb ${DISPLAY} -screen ${screen} ${resolution} &
local loopCount=0
until xdpyinfo -display ${DISPLAY} > /dev/null 2>&1
do
loopCount=$((loopCount+1))
sleep 1
if [ ${loopCount} -gt ${timeout} ]
then
log_e "xvfb failed to start"
exit 1
fi
done
}
launch_window_manager() {
local timeout=${XVFB_TIMEOUT:-5}
# Start and wait for either fluxbox to be fully up or we hit the timeout.
fluxbox &
local loopCount=0
until wmctrl -m > /dev/null 2>&1
do
loopCount=$((loopCount+1))
sleep 1
if [ ${loopCount} -gt ${timeout} ]
then
log_e "fluxbox failed to start"
exit 1
fi
done
}
run_vnc_server() {
local passwordArgument='-nopw'
if [ -n "${VNC_SERVER_PASSWORD}" ]
then
local passwordFilePath="${HOME}/.x11vnc.pass"
if ! x11vnc -storepasswd "${VNC_SERVER_PASSWORD}" "${passwordFilePath}"
then
log_e "Failed to store x11vnc password"
exit 1
fi
passwordArgument=-"-rfbauth ${passwordFilePath}"
log_i "The VNC server will ask for a password"
else
log_w "The VNC server will NOT ask for a password"
fi
x11vnc -display ${DISPLAY} -forever ${passwordArgument} &
wait $!
}
log_i() {
log "[INFO] ${#}"
}
log_w() {
log "[WARN] ${#}"
}
log_e() {
log "[ERROR] ${#}"
}
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ${#}"
}
control_c() {
echo ""
exit
}
trap control_c SIGINT SIGTERM SIGHUP
main
exit
Snapshot of Error:
Proof bootstrap.sh file is present inside my docker image
What you need to do is give the file the correct permissions. In your Dockerfile if you can add the line RUN chmod +x /bootstrap.sh right before you run CMD.
Dockerfile
FROM ubuntu:16.04
COPY bootstrap.sh /
RUN apt-get update && apt-get clean && apt-get install -y \
x11vnc \
xvfb \
fluxbox \
wmctrl \
wget \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list \
&& apt-get update && apt-get -y install google-chrome-stable
RUN useradd apps \
&& mkdir -p /home/apps \
&& chown -v -R apps:apps /home/apps
RUN chmod +x /bootstrap.sh
CMD '/bootstrap.sh'
You can try this:
CMD sh /bootstrap.sh
DockerFile
FROM ubuntu:16.04
COPY bootstrap.sh /
RUN apt-get update && apt-get clean && apt-get install -y \
x11vnc \
xvfb \
fluxbox \
wmctrl \
wget \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list \
&& apt-get update && apt-get -y install google-chrome-stable
RUN useradd apps \
&& mkdir -p /home/apps \
&& chown -v -R apps:apps /home/apps
CMD sh /bootstrap.sh
I am trying to deploy an ruby on rails app through centos ec2 instance by using userdata at instance startup, the specify image is an centos 7 image. below is my userdata section. However, the userdata is not working and even the log file was not created when I piped each command output to an log. Does anyone have any insight on this or how to troubleshoot ec2 userdata? I have looked at the cloud-init.log file in /var/logs too but unable to make out which command actually ran and which one did not, I have included the last few lines from the log below as well. any help is appreciated thanks!
#!/bin/bash
sudo yum update -y | tee -a log.txt
sudo gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 | tee -a /home/ec2-user/log.txt
curl -sSL https://get.rvm.io | sudo bash -s stable | tee -a /home/ec2-user/log.txt
sudo usermod -a -G rvm `whoami` | tee -a /home/ec2-user/log.txt
if sudo grep -q secure_path /etc/sudoers; then sudo sh -c "echo export rvmsudo_secure_path=1 >> /etc/profile.d/rvm_secure_path.sh" && echo Environment variable installed; fi | & tee -a log.txt
sudo su ec2-user
rvm install ruby | tee -a /home/ec2-user/log.txt
rvm --default use ruby | tee -a /home/ec2-user/log.txt
curl -sL https://rpm.nodesource.com/setup_11.x | sudo -E bash - | tee -a /home/ec2-user/log.txt
sudo yum install -y nodejs | tee -a /home/ec2-user/log.txt
sudo yum -y install ImageMagick-devel | tee -a /home/ec2-user/log.txt
sudo yum -y upgrade | tee -a /home/ec2-user/log.txt
sudo yum -y install git | tee -a /home/ec2-user/log.txt
sudo yum -y install java-1.8.0-openjdk | tee -a log.txt
export JAVA_HOME="/usr/lib/jvm/jre-1.8.0-openjdk" | tee -a /home/ec2-user/log.txt
export PATH=$JAVA_HOME/bin:$PATH | tee -a /home/ec2-user/log.txt
sudo yum install postgresql postgresql-server postgresql-devel postgresql-contrib postgresql-docs | tee -a /home/ec2-user/log.txt
rails new app-name -m https://raw.githubusercontent.com/projectblacklight/spotlight/master/template.rb | tee -a /home/ec2-user/log.txt
cd app-name
rake db:migrate | tee -a /home/ec2-user/log.txt
solr_wrapper | tee -a /home/ec2-user/log.txt
rails server | tee -a /home/ec2-user/log.txt
the cloud-init.log
2018-12-10 16:24:43,554 - util.py[DEBUG]: Cloud-init v. 0.7.9 finished at Mon, 10 Dec 2018 21:24:43 +0000. Datasource DataSourceEc2. Up 735.81 seconds
2018-12-10 16:24:43,554 - util.py[DEBUG]: Writing to /var/lib/cloud/instance/boot-finished - wb: [420] 52 bytes
2018-12-10 16:24:43,555 - util.py[DEBUG]: Restoring selinux mode for /var/lib/cloud/instances/i-0b323a8331354129b/boot-finished (recursive=False)
2018-12-10 16:24:43,555 - util.py[DEBUG]: Restoring selinux mode for /var/lib/cloud/instances/i-0b323a8331354129b/boot-finished (recursive=False)
2018-12-10 16:24:43,556 - handlers.py[DEBUG]: finish: modules-final/config-final-message: SUCCESS: config-final-message ran successfully
2018-12-10 16:24:43,556 - main.py[DEBUG]: Ran 9 modules with 1 failures
2018-12-10 16:24:43,565 - util.py[DEBUG]: Creating symbolic link from '/run/cloud-init/result.json' => '../../var/lib/cloud/data/result.json'
2018-12-10 16:24:43,566 - util.py[DEBUG]: Reading from /proc/uptime (quiet=False)
2018-12-10 16:24:43,566 - util.py[DEBUG]: Read 14 bytes from /proc/uptime
2018-12-10 16:24:43,566 - util.py[DEBUG]: cloud-init mode 'modules' took 687.571 seconds (687.57)
2018-12-10 16:24:43,566 - handlers.py[DEBUG]: finish: modules-final: FAIL: running modules for final
CloudFormation? Did you remember to Base64 encode the userdata?
EventCollectionLc:
Type: 'AWS::AutoScaling::LaunchConfiguration'
Properties:
UserData: !Base64
'Fn::Sub': |
#!/bin/bash
What does tee do and is it possible to run this command in a alternative way with suppressing the output?
#!/bin/bash
echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" | sudo tee /etc/apt/sources.list.d/docker.list
The command
echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" | sudo tee /etc/apt/sources.list.d/docker.list
is an indirect way of saying:
echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" > /etc/apt/sources.list.d/docker.list
as root.
You see the output on the terminal because of the tee command.
You can rewrite it as below, to suppress the output, while running as non-root user:
echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" | sudo sh -c "cat > /etc/apt/sources.list.d/docker.list"
See also:
How do I use sudo to redirect output to a location I don't have permission to write to?
https://askubuntu.com/questions/230476/when-using-sudo-with-redirection-i-get-permission-denied
We can install MongoDB by placing the below code in shell script file
if [ ! -f /usr/bin/mongod ]; then<br>
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10<br>
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list<br>
sudo apt-get -y update
sudo apt-get install -y mongodb-org<br>
else
echo "mongo db already installed. Skipping..."
fi
I just change it a little bit and tested on Ubuntu 18.04 LTS.
#!/bin/bash
if [ ! -f /usr/bin/mongod ]
then
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
sudo apt-get update -y
sudo apt-get install mongodb-org -y
sudo mkdir -p /data/db
sudo chown -R $USER /data/db
sudo chmod -R go+w /data/db
else
echo "mongo db already installed. Skipping..."
fi
mongod
I have a script that needs to know what is the release name of a debian system (example: trusty, sid, wheezy etc). I know that I can find out if I am on a Debian based system by looking for /etc/debian_version, but:
cat /etc/debian_version
cat /etc/issue
on Debian Stable produces:
root#07156660e2cd:/# cat /etc/debian_version
7.8
root#07156660e2cd:/# cat /etc/issue
Debian GNU/Linux 7 \n \l
on Ubuntu produces:
```root#a81e3f32b147:/# cat /etc/issue
Ubuntu 14.04.2 LTS \n \l
root#a81e3f32b147:/# cat /etc/debian_version
jessie/sid
```
How I get Ubuntu's codename (in this case 'Trusty')? I don't want to have to maintain a dictionary of release versions to names please. Is there a way in the system to find this information out?
Thanks!
Use lsb_release:
lsb_release -c
#svlasov answer returns a little too much
lsb_release -sc returns just the code name
example
$ lsb_release -sc
utopic
In docker images I can't use uname or another things. So I am using:
DEBIAN_RELEASE=$(awk -F'[" ]' '/VERSION=/{print $3}' /etc/os-release | tr -cd '[[:alnum:]]._-' )
or not depending on /etc/os-release
awk -F'[" ]' '/VERSION=/{print $3}' /etc/*-release | head -1 |tr -cd '[[:alnum:]]._-'
PS:
For docker images based on Debian I am using:
RUN export DEBIAN_FRONTEND=noninteractive && \
export DEBIAN_RELEASE=$(awk -F'[" ]' '/VERSION=/{print $3}' /etc/os-release | tr -cd '[[:alnum:]]._-' ) &&
echo "remove main from /etc/apt/sources.list" && \
sed -i '/main/d' /etc/apt/sources.list && \
echo "remove contrib from /etc/apt/sources.list" && \
sed -i '/contrib/d' /etc/apt/sources.list && \
echo "remove non-free from /etc/apt/sources.list" && \
sed -i '/non-free/d' /etc/apt/sources.list && \
echo "deb http://httpredir.debian.org/debian ${DEBIAN_RELEASE} main contrib non-free" >> /etc/apt/sources.list && \
echo "deb http://httpredir.debian.org/debian ${DEBIAN_RELEASE}-updates main contrib non-free" >> /etc/apt/sources.list && \
echo "deb http://security.debian.org ${DEBIAN_RELEASE}/updates main contrib non-free" >> /etc/apt/sources.list && \
set -x &&\
apt-get update