ТЕМА: Arlington : lean devops - Эдуард Кабринский

Arlington : lean devops - Эдуард Кабринский 2 років 10 місяців тому #30343

Eduard Kabrinskiy - Jenkins ansible docker - Кабринский Эдуард


<h1>Jenkins ansible docker</h1>
<p>[youtube]</p>
Jenkins ansible docker <a href="remmont.com">Today's national news</a> Jenkins ansible docker
<h1>Integrating Ansible and Docker for a CI/CD Pipeline Using Jenkins</h1>
<p style="clear: both"><img src="appfleet-com.cdn.ampproject.org/i/s/appf...ne-Using-Jenkins.png" /></p>
<p>In this guide, we will use Ansible as a Deployment tool in a Continuous Integration/Continuous Deployment process using Jenkins Job.</p>
<p>In the world of CI/CD process, Jenkins is a popular tool for provisioning development/production environments as well as application deployment through pipeline flow. Still, sometimes, it gets overwhelming to maintain the application's status, and script reusability becomes harder as the project grows.</p>
<p>To overcome this limitation, Ansible plays an integral part as a shell script executor, which enables Jenkins to execute the workflow of a process.</p>
<p>Let us begin the guide by installing Ansible on our Control node.</p>
<h2>Install and Configure Ansible</h2>
<p><strong>Installing Ansible:</strong> <br />Here we are using CentOS 8 as our Ansible Control Node. To install Ansible, we are going to use python2-pip , and to do so, first, we have to install python2 . Use the below-mentioned command to do so:</p>
<p>After Python is installed on the system, use pip2 command to install Ansible on the Control Node:</p>
<p>It might take a minute or two to complete the installation, so sit tight. Once the installation is complete, verify:</p>
<p>Through the above command, we notice that the config file path is missing, which we will create and configure later. For now, let’s move to the next section.</p>
<p><strong>Configuring Ansible Control Node User:</strong> <br />The first thing we are going to do is create a user named ansadmin , as it is considered the best practice. So let’s create a user, by using the command adduser , which will create a new user to our system:</p>
<p>Now, use the passwd command to update the ansadmin user’s password. Make sure that you use a strong password.</p>
<p>Copy the password for user ansadmin and save it somewhere safe.</p>
<p>Once we have created the user, it's time to grant sudo access to it, so it doesn't ask for a password when we log in as root . To do so, follow the below-mentioned steps:</p>
<p>Go to the end of the file and paste the below-mentioned line as it is:</p>
<p>Before moving forward, we have one last thing to do. By default, SSH password authentication is disabled in our instance. To enable it, follow the below-mentioned steps:</p>
<p>Find PasswordAuthentication , uncomment it and replace no with yes , as shown below:</p>
<p>You will see why we are doing this in the next few steps. To reflect changes, reload the ssh service:</p>
<p>Now, log in as an ansadmin user on your Control Node and generate ssh key, which we will use to connect with our remote or managed host. To generate the private and public key, follow the below-mentioned commands:</p>
<p>Use ssh-keygen command to generate key:</p>
<p>Usually, keys are generated in the .ssh/ directory. In our case, you can find keys at /home/ansadmin/.ssh/ . Now let us configure our Managed Host for Ansible.</p>
<p><strong>Configuring Ansible Managed Host User:</strong> <br />First, we will create a user on our managed host, so log in to your host and create a user with the same name and password.</p>
<p>As our managed host is an Ubuntu machine, therefore here we have to use the adduser command. Please make sure that the password for the username ansadmin is the same for Control and Managed Host.</p>
<p>Other than this, it is also an excellent thing to cross-check if password authentication is enabled on the Managed Host as we need to copy the ssh public key from the control node to the Managed Host.</p>
<p>Switch to Control Node machine; to copy the public key to our Managed Host machine, we will use the command ssh-copy-id :</p>
<p>For the first time, it will ask for the password. Enter the password for ansadmin , and you are done. Now, if you wish, you can disable Password Authentication on both machines.</p>
<p><strong>Setting Ansible Inventory:</strong> <br />Ansible allows us to manage multiple nodes or hosts at the same time. The default location for the inventory resides in /etc/ansible/hosts . In this file, we can define groups and sub-groups.</p>
<p>If you remember, earlier, the hosts' file was not created automatically for our Ansible. So let's create one:</p>
<p>Add the following lines in your hosts' file and save it:</p>
<p>Make sure that you replace your-managed-host-ip with your host IP address.</p>
<p>Let's break down the basic INI format:</p>
<p>It is time to test our Ansible Inventory, which can be done through the following command. Here we are going to use a simple Ansible module <em><strong>PING</strong></em>:</p>
<p>It looks like the Ansible system can now communicate with our Managed Host as well as with the localhost.</p>
<h2>Install Docker:</h2>
<p>We need a Docker ready system to manage our process; for this, we have to install Docker on both systems. So follow the below-mentioned steps:</p>
<p><strong>For CentOS (Control Node):</strong> <br />Run the following command on your Control Node:</p>
<p>In case you encounter the below-mentioned error during installation:</p>
<p>Next, run the following command:</p>
<p><strong>For Ubuntu OS (Managed Host):</strong> <br />Run the following command on your Managed Host, which is a Ubuntu-based machine:</p>
<p>That’s it for this section. Next, we are going to cover how to integrate Ansible with Jenkins.</p>
<h2>Integrating Ansible with Jenkins:</h2>
<p>In this section, we will integrate Ansible with Jenkins. Fire up your Jenkins, go to Dashboard > Manage Jenkins > Manage Plugins > Available and then search for <em>Publish Over SSH</em> as shown in the image below:</p>
<p style="clear: both"> <img src="appfleet-com.cdn.ampproject.org/i/s/appf...0.01.31-14_31_23.png" /></p>
<p>Now, go to <em>Configure System</em> and find <em>Publish over SSH</em>; under this section, go to <em>SSH Servers</em> and click on the <em>Add</em> button. Here we are going to add our Docker Server as well as Ansible Server, as shown in the image:</p>
<p>SSH server setting for Docker:</p>
<p style="clear: both"> <img src="appfleet-com.cdn.ampproject.org/i/s/appf...0.01.31-14_56_39.png" /></p>
<p>SSH server setting for Ansible:</p>
<p style="clear: both"> <img src="appfleet-com.cdn.ampproject.org/i/s/appf...0.01.31-14_58_00.png" /></p>
<p>In the <em>Hostname</em> field, add your IP address or domain name of Docker and Ansible server. Before saving the setting, make sure that you test the connection before saving the configuration, by clicking on the Test Configuration button as shown in the image below:</p>
<p style="clear: both"> <img src="appfleet-com.cdn.ampproject.org/i/s/appf...0.01.31-15_01_47.png" /></p>
<h2>Create Jenkins Job</h2>
<p>The next step is to create Jenkins jobs. The sole propose of this Job is to build, test, and upload the artifact to our Ansible Server. Here we are going to create Job as a Maven Project, as shown in the image below:</p>
<p style="clear: both"> <img src="appfleet-com.cdn.ampproject.org/i/s/appf...0.01.31-21_49_53.png" /></p>
<p>Next in Job setting page, go to the <em>Source Code Management</em> section and add your Maven project repo URL, as shown in the image below:</p>
<p style="clear: both"> <img src="appfleet-com.cdn.ampproject.org/i/s/appf...0.01.31-22_03_50.png" /></p>
<p>Find the <em>Build</em> section, and in <em>Root POM</em> field enter your pom.xml file name. Additionally in the <em>Goals and options</em> field enter clean install package :</p>
<p style="clear: both"> <img src="appfleet-com.cdn.ampproject.org/i/s/appf...0.01.31-22_06_08.png" /></p>
<p>After successful build completion, your goal is to send the <em>war</em> file to the specified directory to your Ansible server with the right permissions so that it doesn't give us the writing permission by assigning ansadmin to the directory.</p>
<p>Right now, we don't have such a directory, so let us create one. Follow the below-mentioned steps:</p>
<p>Directory /opt/docker will be used as our workspace, where Jenkins will upload the artifacts to Ansible Server.</p>
<p>Now, go to the <em>Post-build Actions</em> section and from the drop-down menu, select <em>Send build artifacts over SSH</em>, as shown in the image below:</p>
<p style="clear: both"> <img src="appfleet-com.cdn.ampproject.org/i/s/appf...0.02.03-16_18_07.png" /></p>
<p>Make sure that in the <em>Remote Directory</em> field, you enter the pattern //opt//docker as it doesn’t support special characters. Apart from this, for now, we are going to leave the <em>Exec Command</em> field empty so that we can test whether our existing configuration works or not.</p>
<p>Now <em>Build</em> the project, and you will see the following output in your Jenkins’s console output:</p>
<p style="clear: both"> <img src="appfleet-com.cdn.ampproject.org/i/s/appf...0.02.03-16_31_42.png" /></p>
<p>Go to your Ansible Server terminal and see if the artifact was sent with right user privileges:</p>
<p>It looks like our webapp.war file was transferred successfully. In the following step, we will create an Ansible Playbook and Dockerfile.</p>
<h2>Creating Dockerfile and Ansible Playbook:</h2>
<p>To create a Docker Image with the webapp.war file, first, we will create a DockerFile. Follow the below-mentioned steps:</p>
<p>First, log in to your Ansible Server and go to directory /opt/docker and create a file named as Dockerfile :</p>
<p>Now open the Dockerfile in your preferred editor, and copy the below-mentioned lines and save it:</p>
<p>Here instructions are to pull a Tomcat image with tag 8.5.50-jdk8-openjdk and copying the webapp.war file to Tomcat default webapp directory., which is /usr/local/tomcat/webapps</p>
<p>With the help of this Dockerfile, we will create a Docker container. So let us create the Ansible Playbook, which will enable us to automate the Docker image build process and later run the Docker container out of it.</p>
<p>We are creating a Ansible Playbook, which does two tasks for us:</p>
<p><ol>
<li>Pull Tomcat’s latest version and build an image using webapp.war file.</li>
<li>Run the built image on the desired host.</li>
</ol>
</p>
<p>For this, we are going to create a new YAML format file for your Ansible Playbook:</p>
<p>Now copy the below-mentioned line into your simple-ansible.yaml file:</p>
<p>You can get more help here: docker_image and docker_container. Now, as our Playbook is created, we can run a test to see if it works as planned:</p>
<p>Here we have used the --limit flag, which means it will only run on our Ansible Server (Control Node). You might see the following output, in your terminal window:</p>
<p>Look's like Playbook ran sccessfully and no error was detected during the Ansible Playbook check, so now we can move to Jenkins to complete our CI/CD process using Ansible.</p>
<h2>Run Ansible Playbook using Jenkins</h2>
<p>In this step, we would execute our Ansible Playbook (i.e., simple-ansible-playbook.yaml ) file, and to do so let us go back to the <em>Project Configuration</em> page in Jenkins and find <strong>Post-build Actions</strong> there.</p>
<p>In this section, copy the below-mentioned command in the <em>Exec command</em> field:</p>
<p style="clear: both"> <img src="appfleet-com.cdn.ampproject.org/i/s/appf...0.02.03-18_43_06.png" /></p>
<p>Now, let us try to build the project and see the Jenkins Job's console output:</p>
<p style="clear: both"> <img src="appfleet-com.cdn.ampproject.org/i/s/appf...0.02.03-18_48_46.png" /></p>
<p>In the output, you can see that our Ansible playbook ran successfully. Let us verify if at Ansible Server the image is created and the container is running:</p>
<p>For Docker Image list:</p>
<p>For Docker Container list:</p>
<p>It looks like Jenkins was able to run the Ansible Playbook successfully. Next, we are going to push Docker Image to Docker Hub.</p>
<h2>Pushing Docker Image to Docker Hub Using Ansible</h2>
<p>We are going to use Docker Hub public repository for this guide; in case you want to work on a live project, then you should consider using the Docker Hub private registry.</p>
<p>For this step, you have to create a Docker Hub account if you haven’t had one yet.</p>
<p>Our end goal for this step is to publish the Docker Image to Docker Hub using Ansible Playbook. So go to your Ansible Control Node and follow the below-mentioned steps:</p>
<p>Make sure that you enter the right username and password.</p>
<p>Now it’s time to create a new Ansible Playbook which will build and push the Docker image to your Docker Hub account. Note that this image will be publicly available, so be cautious.</p>
<p>Create a new Ansible Playbook, which will build a Docker image and push it to our Docker Hub account:</p>
<p>Let us run the playbook now and see what we get:</p>
<p>Go to your Docker Hub account and see if the image was pushed successfully, as shown in the image below:</p>
<p style="clear: both"> <img src="appfleet-com.cdn.ampproject.org/i/s/appf...0.02.07-14_10_48.png" /></p>
<p>Next, let us modify our simple-ansible-playbook.yaml playbook, which we created earlier, as from here on, we are going to pull the Docker image from Docker Hub Account and create a container out of it.</p>
<p>Note that we have used the import_playbook statement at the top of the existing playbook, which means that we want to run the build-push.yaml playbook first along with our main playbook, and this way, we don’t have to run multiple playbooks manually.</p>
<p>Let us break the whole process into steps:</p>
<p><ol>
<li>With the help of build-push.yaml playbook, we are asking Ansible to build an image with the artifacts sent by Jenkins to our Control Node, and later push the built image (i.e., simple-docker-image ) to our Docker Hub’s account or any other private registry like AWS ECR or Google’s Container Registry.</li>
<li>In the simple-ansible-playbook.yaml file, we have imported the build-push.yaml file, which is going to run prior to any statement present within the simple-ansible-playbook.yaml file.</li>
<li>Once build-push.yaml playbook is executed, Ansible will launch a container into our Managed Docker Host by pulling our image from our defined registry.</li>
</ol>
</p>
<p>Now, it's time to build our job. So in the next step, we will deploy the artifact to our Control Node, where Ansible Playbook will build an image, push to Docker Hub and run the container in Managed Host. Let us get started!</p>
<h2>Jenkins Jobs to Deploy Docker Container Using Ansible</h2>
<p>To begin, go to <em>JenkinstoDockerUsingAnsible</em> configure page and change the <em>Exec command</em> in the <em>Post-build Actions</em> section.</p>
<p>Copy the below-mentioned command and add it as shown in the image below:</p>
<p style="clear: both"> <img src="appfleet-com.cdn.ampproject.org/i/s/appf...0.02.12-16_03_17.png" /></p>
<p>Save the configuration and start the build; you will see the following output:</p>
<p style="clear: both"> <img src="appfleet-com.cdn.ampproject.org/i/s/appf...0.02.12-16_22_39.png" /></p>
<p>Now go to your Control Node and verify if our images were built:</p>
<p>It looks like Ansible Playbook was successfully executed on our Control Node. It’s time to verify if Ansible was able to launch containers on our Managed Host or not.</p>
<p>Go to your Managed Host and enter the following command:</p>
<p>Now visit the following URL http://your-ip-addr:8888/webapp/ in your browser. Note that, Tomcat Server may take some time before you can see the output showing your project is successfully setup.</p>
<p>And you are done!</p>
<p>You successfully managed to deploy your application using Jenkins, Ansible, and Docker. Now, whenever someone from your team pushes code to the repository, Jenkins will build the artifact and send it to Ansible, from there Ansible will be responsible for publishing the application to the desired machine.</p>
<h2>Jenkins ansible docker</h2>

<h3>Jenkins ansible docker</h3>
<p>[youtube]</p>
Jenkins ansible docker <a href="remmont.com">Recent news headlines</a> Jenkins ansible docker
<h4>Jenkins ansible docker</h4>
Integrating Ansible and Docker for a CI/CD Pipeline Using Jenkins In this guide, we will use Ansible as a Deployment tool in a Continuous Integration/Continuous Deployment process using
<h5>Jenkins ansible docker</h5>
Jenkins ansible docker <a href="remmont.com">Jenkins ansible docker</a> Jenkins ansible docker
SOURCE: <h6>Jenkins ansible docker</h6> <a href="dev-ops.engineer/">Jenkins ansible docker</a> Jenkins ansible docker
#tags#[replace: -,-Jenkins ansible docker] Jenkins ansible docker#tags#

Эдуард Кабринский
breaking news
  • EUROBoolo
  • EUROBoolo аватар
  • Немає на сайті
  • Платиновий учасник
  • Дописи: 2122
  • Репутація: 0
Адміністратор заборонив доступ на запис.
Час відкриття сторінки: 0.050 секунд