|
| 1 | +Jenkins - Ansible playbook Scenario |
| 2 | + |
| 3 | +Git Repository name: |
| 4 | +https://github.com/Sonal0409/myansible |
| 5 | + |
| 6 | +Open the Jenkins AWS instance |
| 7 | +Download ansible on this instance |
| 8 | + |
| 9 | +# systemctl start jenkins |
| 10 | +# yum install epel-release |
| 11 | +#sudo amazon-linux-extras install epel |
| 12 | +# yum install ansible |
| 13 | + |
| 14 | +y |
| 15 | +y |
| 16 | +complete! |
| 17 | + |
| 18 | +Go to jenkins page |
| 19 | +hostname:8080 |
| 20 | + |
| 21 | +Loginto jenkins |
| 22 | +admin |
| 23 | +admin |
| 24 | + |
| 25 | +Go to manage jenkins --> manage plugins--> Available-->Ansible |
| 26 | +Install it |
| 27 | + |
| 28 | +Go to Global tool configuration |
| 29 | + |
| 30 | +Scroll down to ansible will be there |
| 31 | +first file give name as myansible |
| 32 | +path as /usr/bin |
| 33 | + |
| 34 | +or go to jenkins aws instance and query ansible locationas : |
| 35 | +# which ansible ==> /usr/bin/ansible |
| 36 | + |
| 37 | +Create a new Job |
| 38 | + |
| 39 | +name as myansible |
| 40 | +select pipeline project |
| 41 | + |
| 42 | +go to pipeline and write the code as: |
| 43 | + |
| 44 | +Pipeline code: |
| 45 | + |
| 46 | +pipeline{ |
| 47 | + agent any |
| 48 | + stages{ |
| 49 | + stage('SCM Checkout'){ |
| 50 | + steps{ |
| 51 | + git branch: 'main', url: 'https://github.com/Sonal0409/myansible' |
| 52 | + } |
| 53 | + } |
| 54 | + stage('execute playbook'){ |
| 55 | + steps{ |
| 56 | + ansiblePlaybook credentialsId: 'privatekey', disableHostKeyChecking: true, installation: 'myansible', inventory: 'dev.inv', playbook: 'playbook1.yml' |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | +For above code |
| 68 | + |
| 69 | +Stage1 |
| 70 | + |
| 71 | +click on pipeline syntax |
| 72 | + |
| 73 | +select sample step as git.Git |
| 74 | + |
| 75 | +give repo url as :https://github.com/Sonal0409/myansible |
| 76 | +branch : main |
| 77 | + |
| 78 | +scroll down and click on generate pipeline script |
| 79 | + |
| 80 | +Capy the code and put in script |
| 81 | + |
| 82 | +Stage2 |
| 83 | + |
| 84 | +click on pipeline syntax |
| 85 | + |
| 86 | +Select step as ansible playbook : |
| 87 | + |
| 88 | +Ansible tool : myansible // will be there by default |
| 89 | + |
| 90 | +Playbook file path in workspace : playbook1.yml |
| 91 | +// filename of playbookfrom git |
| 92 | + |
| 93 | +Inventory file path in workspace: dev.inv |
| 94 | + |
| 95 | +SSH connection credentials : |
| 96 | + |
| 97 | +click on add-->jenkins |
| 98 | + |
| 99 | +select kind : SSH username with private key |
| 100 | + |
| 101 | +id: private-key |
| 102 | +Description: ansible |
| 103 | +Username: ec2-user |
| 104 | +privateKey : |
| 105 | + |
| 106 | +go to downloads folder.. go to the host pem key which is gittoday |
| 107 | +open it with notepad and copy the text |
| 108 | + |
| 109 | +go to jenkins |
| 110 | +click on add button and paste the text |
| 111 | + clcik on add key at bottom |
| 112 | + |
| 113 | +now at SSH connection credentials : select ec2-user from drop down |
| 114 | + |
| 115 | +Scroll down and check on |
| 116 | + Disable the host SSH key check |
| 117 | + |
| 118 | +Clcik on Generate pipeline script |
| 119 | + |
| 120 | +Copy the code and as to your pipeline script. |
| 121 | + |
| 122 | +Save and Build Now |
| 123 | + |
0 commit comments