From 9573355ec3e3c7372f66ec44dc78c7173d8dfa57 Mon Sep 17 00:00:00 2001 From: Nana Janashia Date: Wed, 7 Apr 2021 11:24:55 +0200 Subject: [PATCH] Add jenkinsfile for ansible execution --- Jenkinsfile | 37 +++++++++------------------------- ansible/ansible.cfg | 9 +++++++++ ansible/inventory_aws_ec2.yaml | 9 +++++++++ ansible/my-playbook.yaml | 29 ++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 28 deletions(-) create mode 100644 ansible/ansible.cfg create mode 100644 ansible/inventory_aws_ec2.yaml create mode 100644 ansible/my-playbook.yaml diff --git a/Jenkinsfile b/Jenkinsfile index 51f3419..4e33bbb 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,36 +1,17 @@ -def gv - pipeline { agent any stages { - stage("init") { + stage("copy files to ansible server") { steps { script { - gv = load "script.groovy" - } - } - } - stage("build jar") { - steps { - script { - echo "building jar" - //gv.buildJar() - } - } - } - stage("build image") { - steps { - script { - echo "building image" - //gv.buildImage() - } - } - } - stage("deploy") { - steps { - script { - echo "deploying" - //gv.deployApp() + echo "copying all neccessary files to ansible control node" + sshagent(['ansible-server-key']) { + sh "scp -o StrictHostKeyChecking=no ansible/* root@167.99.136.157:/root" + + withCredentials([sshUserPrivateKey(credentialsId: 'ec2-server-key', keyFileVariable: 'keyfile', usernameVariable: 'user')]) { + sh "scp ${keyfile} root@167.99.136.157:/root/ssh-key.pem" + } + } } } } diff --git a/ansible/ansible.cfg b/ansible/ansible.cfg new file mode 100644 index 0000000..0f5e329 --- /dev/null +++ b/ansible/ansible.cfg @@ -0,0 +1,9 @@ +[defaults] +host_key_checking = False +inventory = inventory_aws_ec2.yaml + +interpreter_python = /usr/bin/python3 +enable_plugins = aws_ec2 + +remote_user = ec2-user +private_key_file = ~/ssh-key.pem \ No newline at end of file diff --git a/ansible/inventory_aws_ec2.yaml b/ansible/inventory_aws_ec2.yaml new file mode 100644 index 0000000..8d94d50 --- /dev/null +++ b/ansible/inventory_aws_ec2.yaml @@ -0,0 +1,9 @@ +--- +plugin: aws_ec2 +regions: + - eu-west-3 +keyed_groups: + - key: tags + prefix: tag + - key: instance_type + prefix: instance_type \ No newline at end of file diff --git a/ansible/my-playbook.yaml b/ansible/my-playbook.yaml new file mode 100644 index 0000000..b11ccad --- /dev/null +++ b/ansible/my-playbook.yaml @@ -0,0 +1,29 @@ +--- +- name: Install python3, docker, docker-compose + hosts: all + become: yes + gather_facts: False + tasks: + - name: Install python3 and docker + vars: + ansible_python_interpreter: /usr/bin/python + yum: + name: + - python3 + - docker + update_cache: yes + state: present + - name: Install Docker-compose + get_url: + url: https://github.com/docker/compose/releases/download/1.27.4/docker-compose-Linux-{{lookup('pipe', 'uname -m')}} + dest: /usr/local/bin/docker-compose + mode: +x + - name: Start docker daemon + systemd: + name: docker + state: started + - name: Install docker python module + pip: + name: + - docker + - docker-compose \ No newline at end of file