From: Adam Kraitman Date: Sun, 31 Jan 2021 18:46:07 +0000 (+0200) Subject: Creating a new tag for updating the master os - master-update X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=4c02e783da410857eca86d2b2b71ac76a1c0340f;p=ceph-cm-ansible.git Creating a new tag for updating the master os - master-update And also doing changes to the rook-recovery role Signed-off-by: Adam Kraitman --- diff --git a/roles/rook-recovery/tasks/main.yml b/roles/rook-recovery/tasks/main.yml index 5b1dc37..84a0b06 100644 --- a/roles/rook-recovery/tasks/main.yml +++ b/roles/rook-recovery/tasks/main.yml @@ -5,17 +5,21 @@ 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 @@ -44,21 +48,21 @@ 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: diff --git a/roles/rook-recovery/tasks/master-update.yml b/roles/rook-recovery/tasks/master-update.yml new file mode 100644 index 0000000..c49fd1a --- /dev/null +++ b/roles/rook-recovery/tasks/master-update.yml @@ -0,0 +1,56 @@ +--- + - 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' diff --git a/roles/rook-recovery/vars/main.yml b/roles/rook-recovery/vars/main.yml index b67ac6d..f3ce2ad 100644 --- a/roles/rook-recovery/vars/main.yml +++ b/roles/rook-recovery/vars/main.yml @@ -1,7 +1,7 @@ --- -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 @@ -10,3 +10,5 @@ instance_name: Recovery-Rook-Jenkins aws_tags: Name: "{{ instance_name }}" Application: "Jenkins" +master_name: Prod-Jenkins +rook_key: "{{ secrets_path | mandatory }}/rook_key.yml"