tags:
- always
+ - import_tasks: master-update.yml
+ tags:
+ - master-update
+
- name: Launch instance
ec2:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
key_name: "{{ keypair }}"
group: "{{ security_group }}"
- instance_type: "{{ instance_type }}"
+ instance_type: "{{ master_instance_type }}"
image: "{{ image }}"
wait: true
region: "{{ region }}"
- vpc_subnet_id: "{{ vpc_subnet_id }}"
+ vpc_subnet_id: "{{ vpc_subnet_id }}"
assign_public_ip: yes
instance_tags: "{{ aws_tags }}"
wait: yes
label: "{{ item.id }} - {{ item.public_ip }}"
- name: Remove jenkins docker old container
- command: ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ubuntu@"{{ item.public_ip }}" sudo docker rm jenkins
+ command: ssh -i "{{ rook_key }}" ubuntu@"{{ item.public_ip }}" sudo docker rm jenkins
with_items: '{{ ec2_instances.instances }}'
loop_control:
label: "{{ item.id }} - {{ item.public_ip }}"
- name: Start jenkins container
- command: ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ubuntu@"{{ item.public_ip }}" sudo docker run -d --name jenkins -p 8080:8080 -p 50000:50000 -v /mnt/jenkins/jenkins:/var/jenkins_home "{{ jenkins_image }}"
+ command: ssh -i "{{ rook_key }}" ubuntu@"{{ item.public_ip }}" sudo docker run -d --name jenkins -p 8080:8080 -p 50000:50000 -v /mnt/jenkins/jenkins:/var/jenkins_home "{{ jenkins_master_image }}"
with_items: '{{ ec2_instances.instances }}'
loop_control:
label: "{{ item.id }} - {{ item.public_ip }}"
- - name: The instance was succssfuly started
+ - name: The instance was succssfuly started
debug:
msg:
- - "The Rook Jenkins is up and running the instance is named: {{ aws_tags.Name }}-{{ '%02d' | format(item.0 + 1) }}"
+ - "The Rook Jenkins is up and running the instance is named: {{ aws_tags.Name }}-{{ '%02d' | format(item.0 + 1) }}"
- "In order to make him avalible to public network you will need to add him to the load balancing target group"
with_indexed_items: "{{ ec2_instances.instances }}"
loop_control:
--- /dev/null
+---
+ - name: Gather facts
+ ec2_instance_facts:
+ filters:
+ "tag:Name": "{{ master_name }}"
+ instance-state-name: running
+ register: master_metadata
+
+ - name: create date
+ shell: "date +%d-%m-%y"
+ register: date
+
+ - name: Take a image of the master
+ ec2_ami:
+ aws_access_key: "{{ aws_access_key }}"
+ aws_secret_key: "{{ aws_secret_key }}"
+ instance_id: "{{ master_metadata.instances[0].instance_id }}"
+ no_reboot: yes
+ wait: yes
+ name: "{{ master_name }}-{{ date.stdout }}"
+ tags:
+ Name: "{{ master_name }}-{{ date.stdout }}"
+
+ - name: Update apt cache
+ command: ssh -i "{{ rook_key }}" ubuntu@"{{ master_metadata.instances[0].public_dns_name }}" sudo apt-get update
+
+ - name: Update packages
+ command: ssh -i "{{ rook_key }}" ubuntu@"{{ master_metadata.instances[0].public_dns_name }}" sudo apt-get upgrade -y
+
+ - name: Check if system requires reboot
+ command: ssh -i "{{ rook_key }}" ubuntu@"{{ master_metadata.instances[0].public_dns_name }}" [ -f /var/run/reboot-required ]; echo $?
+ register: reboot
+
+ - name: Reboot if required
+ command: ssh -i "{{ rook_key }}" ubuntu@"{{ master_metadata.instances[0].public_dns_name }}" sudo reboot
+ ignore_errors: yes
+ when: reboot.stdout == '0'
+
+ - name: Wait for SSH to come up
+ wait_for: host={{ master_metadata.instances[0].public_dns_name }} port=22 delay=60 timeout=320 state=started
+ when: reboot.stdout == '0'
+
+ - name: Check if old container exist
+ command: ssh -i "{{ rook_key }}" ubuntu@"{{ master_metadata.instances[0].public_dns_name }}" docker ps -a | grep -i jenkins | wc -l
+ register: container
+ when: reboot.stdout == '0'
+
+ - name: Remove jenkins old container if exist
+ command: ssh -i "{{ rook_key }}" ubuntu@"{{ master_metadata.instances[0].public_dns_name }}" sudo docker rm jenkins
+ when:
+ - container.stdout == '1'
+ - reboot.stdout == '0'
+
+ - name: Start jenkins container
+ command: ssh -i "{{ rook_key }}" ubuntu@"{{ master_metadata.instances[0].public_dns_name }}" sudo docker run -d --name jenkins -p 8080:8080 -p 50000:50000 -v /mnt/jenkins/jenkins:/var/jenkins_home "{{ jenkins_master_image }}"
+ when: reboot.stdout == '0'
---
-jenkins_image: jenkins/jenkins:2.190.1
+jenkins_master_image: jenkins/jenkins:2.235.5
keypair: root-jenkins-new-key
-instance_type: m4.large
+master_instance_type: m4.large
security_group: rook-jenkins-group
image: ami-09c003703cafa9056
region: us-east-1
aws_tags:
Name: "{{ instance_name }}"
Application: "Jenkins"
+master_name: Prod-Jenkins
+rook_key: "{{ secrets_path | mandatory }}/rook_key.yml"