{"id":1204,"date":"2023-09-27T23:05:10","date_gmt":"2023-09-27T17:35:10","guid":{"rendered":"https:\/\/www.openwriteup.com\/?page_id=1204"},"modified":"2023-09-27T23:08:29","modified_gmt":"2023-09-27T17:38:29","slug":"conditional-statement","status":"publish","type":"page","link":"https:\/\/www.openwriteup.com\/?page_id=1204","title":{"rendered":"Conditional statement"},"content":{"rendered":"<p>Ansible check lab1:<\/p>\n<pre>pipeline {\r\n agent any\r\n stages {\r\n  stage('Check ansible') {\r\n   steps {\r\n    script {\r\n     if (fileExists('\/usr\/bin\/ansible')) {\r\n        echo \"File \/usr\/bin\/ansible found!\"\r\n      } else {\r\n        echo \"File \/usr\/bin\/ansible not found!\"\r\n       }\r\n     }\r\n   }\r\n  }\r\n }\r\n}<\/pre>\n<p>Lab2: Check and install package<\/p>\n<p>Part1: modify the sudoers file<\/p>\n<pre>vi \/etc\/sudoers\r\njenkins ALL=(ALL) NOPASSWD: ALL\r\nsave the file esc key (:wq!)\r\nsystemctl restart jenkins<\/pre>\n<p>Part2<\/p>\n<pre>pipeline {\r\n agent any\r\n  stages {\r\n   stage('Check Terraform') {\r\n    steps {\r\n      script {\r\n       if (fileExists('\/usr\/bin\/terraform')) {\r\n         echo \"File \/usr\/bin\/terraform found!\"\r\n       } else {\r\n        echo \"File \/usr\/bin\/terraform not found! Installing Terraform...\"\r\n\/\/ Download HashiCorp GPG key and add it to keyring\r\n       sh 'wget -O- https:\/\/apt.releases.hashicorp.com\/gpg | sudo gpg --dearmor -o \/usr\/share\/keyrings\/hashicorp-archive-keyring.gpg'\r\n\/\/ Add HashiCorp repository to sources.list.d\r\n      sh 'echo \"deb [signed-by=\/usr\/share\/keyrings\/hashicorp-archive-keyring.gpg] https:\/\/apt.releases.hashicorp.com $(lsb_release -cs) main\" | sudo tee \/etc\/apt\/sources.list.d\/hashicorp.list &gt; \/dev\/null'\r\n\/\/ Update package list\r\n     sh 'sudo apt-get update'\r\n\/\/ Install Terraform\r\n     sh 'sudo apt-get install terraform'\r\n    }\r\n   }\r\n  }\r\n }\r\n}\r\n}<\/pre>\n<p>Assignment:<\/p>\n<p>Try same conditional statement:<\/p>\n<p>sudo apt -y install cucumber<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ansible check lab1: pipeline { agent any stages { stage(&#8216;Check ansible&#8217;) { steps { script { if (fileExists(&#8216;\/usr\/bin\/ansible&#8217;)) { echo &#8220;File \/usr\/bin\/ansible found!&#8221; } else { echo &#8220;File \/usr\/bin\/ansible not found!&#8221; } } } } } } Lab2: Check and install package Part1: modify the sudoers file vi \/etc\/sudoers jenkins ALL=(ALL) NOPASSWD: ALL save the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_oct_exclude_from_cache":false,"footnotes":""},"class_list":["post-1204","page","type-page","status-publish","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/1204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1204"}],"version-history":[{"count":3,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/1204\/revisions"}],"predecessor-version":[{"id":1209,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/1204\/revisions\/1209"}],"wp:attachment":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}