Ansible
ssh-keygen ssh-copy-id root@{비밀번호 없이 접속하고자 하는 ip}
-i (--inventory-file) : 적용 될 호스트들에 대한 파일 정보 -m (--module-name) : 모듈 선택 -k (--ask-pass) : 관리자 암호 요청 -K (--ask-become-pass) : 관리자 권한 상승 --list-hosts : 적용되는 호스트 목록
- Ansible Test
cat /etc/ansible/hosts #ex) #[devops] #172.17.0.2 #172.17.0.3 #devops 그룹만 실행 # $ ansible devops $ ansible all -m pingansible all -m shell -a "free -h"ansible all -m copy -a "src=./test.txt dest=/tmp"yum list installed | grep httpdansible devops -m yum -a "name=httpd state=present"--- - name: Add an ansible hosts hosts: localhost tasks: - name: Add an ansible hosts blockinfile: path: /etc/ansible/hosts block: | [mygroup] 172.17.0.5
- name: Ansible Copy Example Local to Remote hosts: devops tasks: - name: copying file with playboook copy: src: ~/sample.txt dest: /tmp owner: root mode: 0644# 전 : 복사 [root@cf3bc7d6a2c6 ~]# cat Dockerfile FROM tomcat:9.0 LABEL org.opencontainers.image.authors="edowon0623@gmail.com" COPY ./hello-world.war /usr/local/tomcat/webapps #후 : command에 해당하는 script를 실행하겠다는 의미 [root@cf3bc7d6a2c6 ~]# cat first-devops-playbook.yml - hosts: all # become: true tasks: - name: build a docker image with deployed war file command: docker build -t cicd-project-ansible . args: chdir: /root
# cicd-project-ansible를 뒤의 name으로 변경(docker login-id) docker tag cicd-project-ansible ny2485/cicd-project-ansible # docker login docker login #docker push docker push ny2485/cicd-project-ansible **<https://hub.docker.com/> 들어가서 login 후 repository를 보면 해당 image가 올라와 있는 걸 볼 수 있음**[root@cf3bc7d6a2c6 ~]# cat create-cicd-devops-image.yml - hosts: all # become: true tasks: - name: build a docker image with deployed war file command: docker build -t ny2485/cicd-project-ansible . args: chdir: /root - name: push the image on Docker Hub command: docker push ny2485/cicd-project-ansible - name: remove the docker image from the ansible server command: docker rmi ny2485/cicd-project-ansible ignore_errors: yes #ansible 실행 -> docker hub보면 새롭게 업데이트 되어있음 ansible-playbook -i hosts create-cicd-devops-image.yml[root@cf3bc7d6a2c6 ~]# cat hosts 172.17.0.2 172.17.0.4 $ ansible-playbook -i hosts create-cicd-devops-image.yml --limit 172.17.0.2 #실행 중 : 172.17.0.2 이것만 실행 #이미지 ok, build, push, remove까지 완료 PLAY [all] *************************************************************************************************************************************************************** TASK [Gathering Facts] *************************************************************************************************************************************************** ok: [172.17.0.2] TASK [build a docker image with deployed war file] *********************************************************************************************************************** changed: [172.17.0.2] TASK [push the image on Docker Hub] ************************************************************************************************************************************** changed: [172.17.0.2] TASK [remove the docker image from the ansible server] ******************************************************************************************************************* changed: [172.17.0.2] PLAY RECAP *************************************************************************************************************************************************************** 172.17.0.2 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 # docker-hub을 보면 새로 이미지가 올라옴을 알 수 있음ansible-playbook -i hosts create-cicd-devops-image.yml --limit 172.17.0.2 ansible-playbook -i hosts create-cicd-devops-container.yml --limit 172.17.0.4
Last updated

