How can we run sudo rpm commands using ansible - jenkins

In my ansible role, I have a task where I am trying to install Jenkins rpm. The rpm is located in /tmp directory on target host. Also, I have permission to run the command, "sudo /bin/rpm -Uvh /tmp/jenkins-2.107.1.2-1.1.noarch.rpm" which work when I tried to run it on the server directly. But running the same command via command module it fails.
Task :
- name: run the rpm command on masters
command: sudo /bin/rpm -Uvh jenkins-2.107.1.2-1.1.noarch.rpm
args:
chdir: /tmp
Error:
fatal: [xxxx. xxx.com]: FAILED! => {"changed": false, "cmd": "'sudo rpm' -Uvh 'jenkins-2.107.1.2-1.1.noarch.rpm'", "msg": "[Errno 2] No such file or directory", "rc": 2}
Permission on target host :
User XXXX may run the following commands on target_Host_Name:
(root) NOPASSWD: /usr/sbin/service jenkins stop, /usr/sbin/service jenkins start, /bin/rpm -Uvh jenkins*.noarch.rpm

You could use the ansible yum module with become: true rather than the command module with sudo for example:
- name: install jenkins rpm from a local file
yum:
name: /tmp/jenkins-2.107.1.2-1.1.noarch.rpm
state: present
become: true
Note: the error [Errno 2] No such file or directory is explicit, the file /tmp/jenkins-2.107.1.2-1.1.noarch.rpm does not exist. You need to ensure that the file exists before attempting to install the rpm.
For additional information on privilege escalation in ansible refer to: https://docs.ansible.com/ansible/latest/user_guide/become.html

Related

Why is this Todo app build failing in Jenkins when deploying on AWS Linux using Docker file in WSL2?

So I was trying to deploy a simple CD pipeline using docker by ssh’ing into my AWS Linux EC2 instance in the WSL2 terminal. The job is failing every time returning the following error:
Started by user Navdeep Singh Running as SYSTEM Building on the
built-in node in workspace /var/lib/jenkins/workspace/todo-dev
[todo-dev] $ /bin/sh -xe /tmp/jenkins6737039323529850559.sh + cd
/home/ubuntu/project/django-todo /tmp/jenkins6737039323529850559.sh:
2: cd: can’t cd to /home/ubuntu/project/django-todo Build step
‘Execute shell’ marked build as failure Finished: FAILURE
DockerFile contents:
FROM python:3 RUN pip install django==3.2
COPY . .
RUN python manage.py migrate
CMD [“python”,“manage.py”,“runserver”,“0.0.0.0:8000”]
Everything goes fine. This error cd: can’t cd to /home/ubuntu/project/django-todo Build step ‘Execute shell’ marked build as failure Finished: FAILURE is not an actual.
Your agent Node is not online.
To fix the problem, find commands on your jenkins web page after an agent setup. You need to run those commands from your terminal. See the screenshot for more details.
Make sure that your jenkins public IP and node agent public IP are the same. If an error occurs, you need to run some commands on the terminal. This is not a real error.
this issue follow this step which i give you
For Agent--->
change your ip here(44.203.138.174:8080) to your EC2 ip
1.curl -sO http://44.203.138.174:8080/jnlpJars/agent.jar
2.java -jar agent.jar -jnlpUrl http://44.203.138.174:8080/manage/computer/todo%2Dagent/jenkins-agent.jnlp -secret beb62de0f81bfd06e4cd81d1b896d85d38f82b87b21ef8baef3389e651c9f72c -workDir "/home/ubuntu"
For JOb --->
sudo vi /etc/sudoers
then add this command below root access in sudoers file
jenkins ALL=(ALL) NOPASSWD: ALL
3.then goto the ubuntu directory using cd .. then run this codes
grep ^ubuntu /etc/group
id jenkins
sudo adduser jenkins ubuntu
grep ^ubuntu /etc/group
4.restart the jenkins relogin
sudo systemctl stop jenkins
then you good to go

Ansible Docker containers dynamic inventory on MacOS

I have started a Docker container using the following command:
docker run tomcat:latest
Then I created a file named docker.yml with the following contents:
plugin: community.docker.docker_containers
docker_host: unix://var/run/docker.sock
Finally I try to obtain a list of the currently running Docker containers using:
ansible-inventory -i docker.yml --list
However instead of a list of running containers, I only get the following result:
[WARNING]: * Failed to parse docker.yml with yaml plugin: Plugin configuration YAML file, not YAML inventory
[WARNING]: * Failed to parse docker.yml with constructed plugin: Incorrect plugin name in file: community.docker.docker_containers
[WARNING]: Unable to parse docker.yml as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped"
]
}
}
Have I misunderstood the Ansible Docker containers dynamic inventory or am I doing something wrong?
I suspect I had a case of system in disarray and this was the cure:
I retained my current Python installation located at ~/Library/Python/3.9/.
Attempted to uninstall Ansible using pip:
pip uninstall ansible
Manually removed all things Ansible:
sudo rm -r /etc/ansible
sudo rm -r -/.ansible
sudo rm -r /usr/local/lib/python3.9/site-packages/ansible*
sudo rm /usr/local/bin/ansible*
Performed a fresh installation of Ansible:
pip install ez_setup
pip install --user ansible
Installed Ansible Docker collection prerequisite:
pip install docker
Installed Ansible Docker collection:
ansible-galaxy collection install community.docker
After the above, the Ansible Docker container dynamic inventory works as expected and without errors.

Install AWX 17.1 and Docker-Compose 1.29.2 for CentOS7

My Server:
Virtual hardware: 2 vCPU / 4 Go
OS: CentOS Linux release 7.9.2009
Docker version 20.10.9
Docker Compose version 1.29.2 --> 2 installation methods
Ansible 2.9.25-1
libselinux-python3
python3-pip / pip 21.3.1
Python 2.7.5
Python 3.6.8
2 installation methods of Docker Compose:
by download from github:
# cp docker-compose-Linux-x86_64-1.29.2 /usr/local/bin/docker-compose
by PIP:
# pip3 install docker-compose --proxy "http://user:pass#proxy:port"
I try to install AWX 17.1.0 to my server.
If I use method 2 then the AWX installation works.
Otherwise if I use method 1 then AWX installation fails :
[root#myserver ~]# cp docker-compose-Linux-x86_64-1.29.2 /usr/local/bin/docker-compose
[root#myserver ~]# cd /awx-17.1.0/installer/
[root#myserver installer]# ansible-playbook -i inventory install.yml
(...)
TASK [local_docker : Remove AWX containers before migrating postgres so that the old postgres container does not get used] ***
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Unable to load docker-compose. Try `pip install docker-compose`. Error: Traceback (most recent call last):\n File \"/tmp/ansible_docker_compose_payload_ad5z56ir/ansible_docker_compose_payload.zip/ansible/modules/cloud/docker/docker_compose.py\", line 482, in <module>\nModuleNotFoundError: No module named 'compose'\n"}
...ignoring
TASK [local_docker : Run migrations in task container] *************************
changed: [localhost]
TASK [local_docker : Start the containers] *************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Unable to load docker-compose. Try `pip install docker-compose`. Error: Traceback (most recent call last):\n File \"/tmp/ansible_docker_compose_payload_1h9rln6h/ansible_docker_compose_payload.zip/ansible/modules/cloud/docker/docker_compose.py\", line 482, in <module>\nModuleNotFoundError: No module named 'compose'\n"}
(...)
I am comparing the files between the 2 methods:
By method 1 (github), 1 file only:
/usr/local/bin/docker-compose --> size : 12737304
By method 2 (pip):
/usr/local/bin/docker-compose --> size : 215
/usr/local/lib/python3.6/site-packages/compose/*
/usr/local/lib/python3.6/site-packages/docker_compose-1.29.2.dist-info/*
I don't understand why such a difference. I think this is the cause of my installation problem.
Therefore, I am looking for the way to install Docker Compose from Github as if the installation is done as per PIP.

"logname: no login name" inside Docker container when running dpkg -i

I need to install an SDK package inside an Ubuntu 18.04 Docker container, but am constantly running into this problem:
theuser#e9fa4f39e0f0:/src/spinnaker$ sudo dpkg -i libspinnaker_2.2.0.48_arm64.deb
(Reading database ... 52013 files and directories currently installed.)
Preparing to unpack libspinnaker_2.2.0.48_arm64.deb ...
Unpacking libspinnaker (2.2.0.48) over (2.2.0.48) ...
logname: no login name
dpkg: warning: old libspinnaker package post-removal script subprocess returned error exit status 1
dpkg: trying script from the new package instead ...
logname: no login name
dpkg: error processing archive libspinnaker_2.2.0.48_arm64.deb (--install):
new libspinnaker package post-removal script subprocess returned error exit status 1
logname: no login name
dpkg: error while cleaning up:
new libspinnaker package post-removal script subprocess returned error exit status 1
Errors were encountered while processing:
libspinnaker_2.2.0.48_arm64.deb
I've tried all manner of workarounds, setting USER, SUDO_USER, LOGNAME, running the container with the "-u" switch to my uid/gid and all get the same logname error. Is there a work around for this?
I had the same problem with the latest spinnaker api release.
The issue is that postinst call logname to find out where your home directory is, to install some config files. In the docker build context, there is no logged in user.
My egregious hack was to overwrite the logname executable with "echo root".
e.g.:
# Install spinnaker sdk https://www.flir.com/support-center/iis/machine-vision/downloads/spinnaker-sdk-and-firmware-download/
COPY external/spinnaker/* spinnaker/
# Pre-answer the apt install prompts
COPY spinnaker.dat .
RUN cat spinnaker.dat >> /var/cache/debconf/config.dat
# Fake out logname (no login context in docker build)
RUN echo "echo root" > /usr/bin/logname
# Install other postinst dependencies
RUN DEBIAN_FRONTEND=noninteractive apt install -y iputils-ping wget
RUN DEBIAN_FRONTEND=noninteractive apt install -y --no-install-recommends ./spinnaker/lib*.deb && rm -rv spinnaker
The contents of spinnaker.dat (to avoid being prompted from the preinst script) are:
Name: libspinnaker/accepted-flir-eula
Template: libspinnaker/accepted-flir-eula
Value: true
Owners: libspinnaker
Flags: seen
Name: libspinnaker/error-flir-eula
Template: libspinnaker/error-flir-eula
Owners: libspinnaker
Name: libspinnaker/present-flir-eula
Template: libspinnaker/present-flir-eula
Value:
Owners: libspinnaker
Flags: seen

Error while running (docker_image) module ansible Playbook

I have my docker images in nexus. When i am trying to run that playbook i am getting error
Playbook Code
---
- hosts: localhost
become: True
tasks:
- name: pull an image
docker_image:
name: 10.220.110.10:8083/halosys:f5a950f
source: pull
...
ERROR while running my playbook
TASK [pull an image] **************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (docker_image) module: source Supported parameters include: api_version, archive_path, buildargs, cacert_path, cert_path, container_limits, debug, docker_host, dockerfile, force, http_timeout, key_path, load_path, name, nocache, path, pull, push, repository, rm, ssl_version, state, tag, timeout, tls, tls_hostname, tls_verify, use_tls"}
Please help to fix this error
Option source was added in Ansible 2.8 - see documentation. The error message states it is missing, so you're using older version. To fix this you need to either update your Ansible installation to at least version 2.8 or check the documentation for your version.
For example, Ansible 2.7 documentation states that to pull an image you need to do:
- name: pull an image
docker_image:
name: 10.220.110.10:8083/halosys:f5a950f
To check what version of Ansible you're running use command
ansible --version
EDIT:
I can’t guarantee it’ll work, but I would try removing Ansible installed from system packages:
sudo yum remove ansible
followed by installing Python 3:
sudo yum install python36 python36-virtualenv python36-pip
then installing docker-py using pip:
python3 -m pip install docker-py
And finally installing Ansible with Python3 support:
python3 -m pip install ansible
Here’s Ansible documentation about Python3 support.
Also if package python36 is not available you can run
sudo yum list | grep python3
to find what version is available. As per documentation mentioned above you need at least python35 to make Ansible work.

Resources