2022. 7. 7. 14:15ㆍCloud/Ansible
먼저 ssh key 접근을 좀 더 수월하게 하기 위해 keyscan를 반복문으로 설정해 보도록 하자.
# vi keyscan.yml
- name: Setup for the Ansible's Environment
==> yml 파일에 대한 설명
hosts: localhost
gather_facts: no
==> 변수수집을 하지 않겠다. 라는 의미
tasks:
- name: Keysacn
shell: "{{ item }}"
==> 반복문 생성을 할 것이다.
with_items:
- "ssh-keyscan 192.168.1.22 >> ~/.ssh/known_hosts"
- "ssh-keyscan 192.168.1.21 >> ~/.ssh/known_hosts"
- "ssh-keyscan 192.168.0.229 >> ~/.ssh/known_hosts"
- "ssh-keyscan 192.168.1.24 >> ~/.ssh/known_hosts"
==>접근하고자 하는 ip
# ansible-playbook keysacn.yml
==> 상단에서 만든 파일 실행하기
# vi /ansible_env.yml
---
- name: Setup for the Ansible's Environment
==> 이름지정
host:localhost
==> host를 지정할 건데 특별한 인터넷이 아닌 내부 localhost를 사용할 것임.
gather_facts: no
tasks:
- name: Add "/etc/ansible/hosts"
blockinfile:
path: /etc/ansible/hosts
block: |
[centos]
192.168.1.21
192.168.1.22
[ubuntu]
192.168.0.229 ansible_python_interpreter=/usr/bin/python3
192.168.1.24 ansible_python_interpreter=/usr/bin/python3
==> 앤서블의 위치 및 ip, 사용할 python3위치
- name: Configure Bashrc
lineinfile:
path: /root/.bashrc
line: "{{ item }}"
with_items:
- "alias ans='ansible'"
- "alias anp='ansible-playbook'"
이러한 방식을 사용하면 ip를 하나하나 기입해야하는 불편함이 있다. 그렇기에, 불편함을 방지하고자 다른 방식을 써 보도록 하자. 좀 더 동적인 방식인 매직변수를 활용하자. 새로 keypair_new 라는 yaml파일을 생성한 후 아래의 내용을 기입하자.
# vi keypair_new.yml
- name: Create known_hosts between server and nodes
hosts: all
connection: local
serial: 1
gather_facts: no
tasks:
- name: ssh-keyscan for known_hosts file
command: /usr/bin/ssh-keyscan -t ecdsa {{ ansible_host }}
==> 매직 변수 ansible_host 활용하여 hosts ip 호출
register: keyscan
- name: input key
lineinfile:
path: ~/.ssh/known_hosts
line: "{{ item }}"
create: yes
with_items:
- "{{ keyscan.stdout_lines }}"
- name: Create authorized_keys between server and nodes
hosts: all
connection: local
gather_facts: no
vars:
ansible_password: 비밀번호
==> 사용자가 사용하는 비밀번호를 입력
tasks:
- name: ssh-keygen for authorized_keys file
openssh_keypair:
path: ~/.ssh/id_rsa
size: 2048
type: rsa
force: False
==> ssh-keygen -t rsa 명령어를 실행할 것이다. 암호화의 비트수를 2048로 하며 타입을 rsa로 한다. overwrite하지 않는다는 False라고 값을 넣거나 아니면 삭제하거나 하면 되겠습니다.
- name: input key for each node
connection: ssh
authorized_key:
user: root
state: present
key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
==> .ssh/에 있는 id_rsa.pub파일을 가져와서 퍼블릭(authorized) key로 삼고, 인벤토리에 있는 서버로 발송하겠다 라는 의미이다.
기타 리포지토리에 있는 파일을 다운받기 위해서는 epel을 설치한 후 실행하면 된다. nignx를 설치해 보도록 하자.
vi nignx_install.yml 파일을 생성후 아래 내용을 기입하자.
- name: Install nginx on centos
hosts: centos
gather_facts: no
==> centos호스트에 파일을 설치할 것이다.
tasks:
- name: install epel-release
yum:
name: epel-release
state: latest
- name: install nginx web server
yum: name:nginx state:present
- name: upload default index.html for web server
get_url: url=https://www.nginx.com dest=/usr/share/nginx/html/ mode=0644
- name: start nginx web server
service: name:nginx state:started
- name: Install nginx on ubuntu
hosts: ubuntu
gather_facts: no
==> 우분투에 파일을 설치할 것이다.
tasks:
- name: install nginx web server
apt:
pkg:nginx
state:present
update_cache=yes # apt를 update하겠다는 의미
- name: Upload default index.html for web server
get_url: url=https://www.nginx.com dest=/var/www/html/
mode=0644 validate_certs=no
삭제하는 방법은 아래의 yml 파일을 설치해서 진행하면 된다.
# vi nginx_remove.yml
- name: Remove nginx on centos
hosts: centos
gather_facts: no
tasks:
- name: remove nginx web server
yum: name=nginx state=absent
- name: Remove nginx on ubuntu
hosts: ubuntu
gather_facts: no
tasks:
- name: remove nginx web server
apt: pkg=nginx* state=absent
# ansible-playbook nginx_remove.yml
'Cloud > Ansible' 카테고리의 다른 글
Ansible 활용 NFS 및 Wordpress설치하기 (0) | 2022.07.07 |
---|---|
Ansible 개괄 및 기초 환경, 명령어 (0) | 2022.07.06 |