{"id":1415,"date":"2023-02-22T22:04:27","date_gmt":"2023-02-22T21:04:27","guid":{"rendered":"https:\/\/www.anwarbenihissa.com\/?p=1415"},"modified":"2023-02-22T22:04:27","modified_gmt":"2023-02-22T21:04:27","slug":"introduction-to-devops-culture","status":"publish","type":"post","link":"https:\/\/www.anwarbenihissa.com\/index.php\/2023\/02\/22\/introduction-to-devops-culture\/","title":{"rendered":"Introduction to DevOps Culture"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Introduction<\/h4>\n\n\n\n<p>DevOps is a rapidly evolving field that has revolutionized how organizations approach software development and delivery. This series of articles will cover the fundamental concepts and tools essential for a successful DevOps implementation. We will take an in-depth look at:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Introduction to DevOps Culture: A comprehensive overview of DevOps, its history, and benefits for organizations.<\/li><li>Kubernetes and Containerization: An exploration of containerization and Kubernetes and their role in improving scalability, security, and reliability in a DevOps environment.<\/li><li>Ansible and Configuration Management: An introduction to Ansible and its role in managing and automating infrastructure, and its integration with other DevOps tools.<\/li><li>AWS for DevOps: Examining the use of Amazon Web Services in a DevOps context, including EC2, S3, Elastic Beanstalk, and comparing different cloud providers.<\/li><li>Terraform and Infrastructure as Code: A discussion of infrastructure as code and the benefits of using Terraform to provision and manage infrastructure in an automated and repeatable way.<\/li><li>GitLab and CI\/CD: An exploration of GitLab in a DevOps context, including its integration with Ansible and Terraform, and its use in implementing continuous integration and continuous deployment processes.<\/li><li>Best Practices for Monitoring and Observability: An overview of the importance of monitoring and observability in a DevOps environment and best practices for its implementation.<\/li><\/ol>\n\n\n\n<p>Join us on this journey as we delve into the exciting world of DevOps and its impact on organizations.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Introduction to DevOps Culture: Understanding the basics of DevOps, its history, and its benefits for organizations.<\/h4>\n\n\n\n<p>DevOps is a software development methodology that emphasizes collaboration between development and operations teams. The goal of DevOps is to automate and streamline the software development process from development to production. One of the critical aspects of DevOps is the automation of the deployment process. This article will explore how to automate the deployment of an application using Jenkins and Ansible or GitLab CI and Ansible.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How to automate the deployment of an application with Jenkins or Gitlab CI and Ansible<\/h4>\n\n\n\n<p>If you want to deliver new features to end-users quickly and efficiently, using automation tools like Jenkins or GitLab CI can be very helpful. With these tools, you can automate the entire process of building, testing, and deploying your application.<\/p>\n\n\n\n<p>Jenkins is an open-source automation server that enables the creation of continuous deployment pipelines. These pipelines can be triggered automatically by events such as commits in a Git repository. GitLab CI is also a popular automation tool that provides a similar set of features.<\/p>\n\n\n\n<p>To automate the deployment process, you can use a tool like Ansible to manage the different stages of the process. Ansible is an automatic configuration and deployment tool that can be used to perform tasks like package installation, file copying, and service management.<\/p>\n\n\n\n<p>By combining Jenkins or GitLab CI with Ansible, you can fully automate the deployment process of your application. This approach can help to reduce human errors and improve the speed of the deployment process, allowing you to get new features into the hands of end-users faster.<\/p>\n\n\n\n<p>Install Ansible on the Jenkins server:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">$ sudo apt-get update\n$ sudo apt-get install software-properties-common\n$ sudo apt-add-repository ppa:ansible\/ansible\n$ sudo apt-get update\n$ sudo apt-get install ansible<\/code><\/pre>\n\n\n\n<p>Create an inventory file in Ansible that lists the target servers:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-yaml\">[web]\nserver1.example.com\nserver2.example.com<\/code><\/pre>\n\n\n\n<p>Create a playbook in Ansible that defines the tasks needed to install and deploy the application:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-yaml\">---\n- name: Deploy web application\n  hosts: webservers\n  become: yes\n\n  vars:\n    app_name: myapp\n    app_version: 1.0.0\n\n  tasks:\n    - name: Install packages\n      apt:\n        name:\n          - nginx\n          - nodejs\n        state: present\n\n    - name: Copy application files\n      copy:\n        src: files\/{{ app_name }}-{{ app_version }}.tar.gz\n        dest: \/tmp\/\n\n    - name: Extract application files\n      unarchive:\n        src: \/tmp\/{{ app_name }}-{{ app_version }}.tar.gz\n        dest: \/var\/www\/{{ app_name }}\n        remote_src: yes\n\n    - name: Configure nginx\n      template:\n        src: templates\/nginx.conf.j2\n        dest: \/etc\/nginx\/sites-available\/{{ app_name }}\n        owner: root\n        group: root\n        mode: &#039;0644&#039;\n\n    - name: Enable site\n      file:\n        src: \/etc\/nginx\/sites-available\/{{ app_name }}\n        dest: \/etc\/nginx\/sites-enabled\/\n        state: link\n\n    - name: Restart nginx\n      service:\n        name: nginx\n        state: restarted\n<\/code><\/pre>\n\n\n\n<p>You can execute this playbook with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">ansible-playbook -i \/path\/to\/inventory \/path\/to\/playbook.yml<\/code><\/pre>\n\n\n\n<p>Create a Jenkins job that triggers the Ansible playbook:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-groovy\">pipeline {\n  agent any\n\n  environment {\n    APP_NAME = &quot;myapp&quot;\n    APP_VERSION = &quot;1.0.0&quot;\n  }\n\n  stages {\n    stage(&#039;Install packages&#039;) {\n      steps {\n        sh &#039;sudo apt-get update &amp;&amp; sudo apt-get install -y nginx nodejs&#039;\n      }\n    }\n\n    stage(&#039;Copy application files&#039;) {\n      steps {\n        sh &#039;cp files\/${APP_NAME}-${APP_VERSION}.tar.gz \/tmp\/&#039;\n      }\n    }\n\n    stage(&#039;Extract application files&#039;) {\n      steps {\n        sh &#039;tar -zxvf \/tmp\/${APP_NAME}-${APP_VERSION}.tar.gz -C \/var\/www\/${APP_NAME} --strip-components=1&#039;\n      }\n    }\n\n    stage(&#039;Configure nginx&#039;) {\n      steps {\n        sh &#039;cat templates\/nginx.conf | sed &quot;s\/APP_NAME\/${APP_NAME}\/g&quot; &gt; \/etc\/nginx\/sites-available\/${APP_NAME}&#039;\n      }\n    }\n\n    stage(&#039;Enable site&#039;) {\n      steps {\n        sh &#039;ln -s \/etc\/nginx\/sites-available\/${APP_NAME} \/etc\/nginx\/sites-enabled\/${APP_NAME}&#039;\n      }\n    }\n\n    stage(&#039;Restart nginx&#039;) {\n      steps {\n        sh &#039;sudo service nginx restart&#039;\n      }\n    }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p>This Jenkins pipeline uses the same set of tasks as the previous Ansible playbook to deploy a web application.<\/p>\n\n\n\n<p>The pipeline starts by defining environment variables for the application name and version. Then, it defines a series of stages that correspond to the tasks in the Ansible playbook.<\/p>\n\n\n\n<p>Each stage contains a set of steps that execute shell commands to perform the necessary tasks. For example, the first stage installs the necessary packages using <code>apt-get<\/code>, and the second stage copies the application files using <code>cp<\/code>.<\/p>\n\n\n\n<p>Note that this is just one example of how to automate the deployment of a web application using Jenkins. The specific steps in a Jenkins pipeline will depend on the needs of the project and the organization.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How to automate the deployment of an application with GitLab CI and Ansible<\/h4>\n\n\n\n<p>Install Ansible on the GitLab CI server:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">$ sudo apt-get update\n$ sudo apt-get install software-properties-common\n$ sudo apt-add-repository ppa:ansible\/ansible\n$ sudo apt-get update\n$ sudo apt-get install ansible<\/code><\/pre>\n\n\n\n<p>Create an inventory file in Ansible that lists the target servers:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-yaml\">[web]\nserver1.example.com\nserver2.example.com<\/code><\/pre>\n\n\n\n<p>We can use the same playbook we used below to define the tasks needed to deploy the same application&#8230;<\/p>\n\n\n\n<p>Next, it copies the application files to the<code>\/tmp<\/code> directory on the webservers using the <code>copy<\/code> module, and then extracts them to the <code>\/var\/www\/myapp<\/code> directory using the <code>unarchive<\/code> module.<\/p>\n\n\n\n<p>After that, it configures Nginx to serve the application using a template for the configuration file and then enables the site by creating a symbolic link from the configuration file to the<code>\/etc\/nginx\/sites-enabled<\/code> directory.<\/p>\n\n\n\n<p>Finally, it restarts the Nginx service to pick up the changes.<\/p>\n\n\n\n<p>Create a GitLab CI job that triggers the Ansible playbook:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Add a <code>.gitlab-ci.yml<\/code> file to your project<\/li><li>Define a <code>deploy<\/code> a job that runs the Ansible playbook using the <code>ansible-playbook<\/code> command:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-yaml\">stages:\n  - build\n  - test\n  - deploy\n  - cleanup\n\nbuild:\n  stage: build\n  script:\n    - docker build -t myapp .\n\ntest:\n  stage: test\n  script:\n    - docker run myapp npm test\n\ndeploy:\n  stage: deploy\n  environment:\n    name: production\n    url: https:\/\/myapp.example.com\n  script:\n    - ansible-playbook deploy.yml\n\ncleanup:\n  stage: cleanup\n  script:\n    - docker rmi myapp\n<\/code><\/pre>\n\n\n\n<p>Commit and push the <code>.gitlab-ci.yml<\/code> file to your repository<\/p>\n\n\n\n<p>In this example, the pipeline has four stages: <code>build<\/code>, <code>test<\/code>, <code>deploy<\/code>, and <code>cleanup<\/code>.<\/p>\n\n\n\n<p>In the <code>build<\/code> stage, the pipeline builds a Docker image of the application using the<code>Dockerfile<\/code> in the project directory.<\/p>\n\n\n\n<p>In the <code>test<\/code> stage, the pipeline runs the application&#8217;s tests using the <code>npm test<\/code> command in a Docker container.<\/p>\n\n\n\n<p>In the <code>deploy<\/code> stage, the pipeline deploys the application to a target environment using an Ansible playbook called <code>deploy.yml<\/code>. The <code>environment<\/code>keyword in this stage sets the name and URL of the production environment.<\/p>\n\n\n\n<p>In the <code>cleanup<\/code> stage, the pipeline cleans up any resources that were created during the pipeline, including the Docker image that was built on the <code>build<\/code> stage.<\/p>\n\n\n\n<p>Of course, this is just an example and the specific tasks and stages in a <code>gitlab-ci.yml<\/code> the file will depend on the needs of the project and the organization.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion:<\/h3>\n\n\n\n<p>Automating the deployment process is an essential aspect of DevOps, and using tools like Jenkins and Ansible or GitLab CI and Ansible can make it easier to achieve. By automating the deployment process, development and operations teams can work together more efficiently to deliver new features to end users quickly and reliably. With the right tools and procedures in place, automating the deployment process can also help to reduce human errors and improve the overall quality of the software development process.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction DevOps is a rapidly evolving field that has revolutionized how organizations approach software development and delivery. This series of articles will cover the fundamental concepts and tools essential for a successful DevOps implementation. We will take an in-depth look at: Introduction to DevOps Culture: A comprehensive overview of DevOps, its history, and benefits for organizations. Kubernetes and Containerization: An [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1450,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,10],"tags":[62,58,60,59],"class_list":["post-1415","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud","category-it","tag-ansible","tag-cloud","tag-gitlab-ci","tag-jenkins"],"_links":{"self":[{"href":"https:\/\/www.anwarbenihissa.com\/index.php\/wp-json\/wp\/v2\/posts\/1415"}],"collection":[{"href":"https:\/\/www.anwarbenihissa.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.anwarbenihissa.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.anwarbenihissa.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.anwarbenihissa.com\/index.php\/wp-json\/wp\/v2\/comments?post=1415"}],"version-history":[{"count":30,"href":"https:\/\/www.anwarbenihissa.com\/index.php\/wp-json\/wp\/v2\/posts\/1415\/revisions"}],"predecessor-version":[{"id":1457,"href":"https:\/\/www.anwarbenihissa.com\/index.php\/wp-json\/wp\/v2\/posts\/1415\/revisions\/1457"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.anwarbenihissa.com\/index.php\/wp-json\/wp\/v2\/media\/1450"}],"wp:attachment":[{"href":"https:\/\/www.anwarbenihissa.com\/index.php\/wp-json\/wp\/v2\/media?parent=1415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.anwarbenihissa.com\/index.php\/wp-json\/wp\/v2\/categories?post=1415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.anwarbenihissa.com\/index.php\/wp-json\/wp\/v2\/tags?post=1415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}