참고하기
1. private 서브넷 생성
2. ssh 허용
3. 키등록으로 controller에서 생성한 id_rsa.pub 가져오기
4. bastion을 위한 public 인스턴스 생성
5. private 인스턴스 생성
6. !/.ssh/config
수정 및 ssh 점프 호스트 확인
Host bastion
HostName 배스천IP
User ec2-user
Port 22
IdentityFile ~/.ssh/id_rsa
Host node1
Hostname 프라이빗IP
User ec2-user
Port 22
IdentityFile ~/.ssh/id_rsa
$ ssh -J bastion node1
Host node1
ProxyJump bastion
ec2.ini
설정[ec2]
priavte 인스턴스 IP
[ec2:vars]
ansible_user=ec2-user
ansible_ssh_common_args='-o ProxyCommand="ssh -p 22 -W %h:%p -q ec2-user@bastion IP"'
$ ansible ec2 -i ec2.ini -m command -a id
[vagrant@controller ~]$ ansible wp -m yum -a 'name=https://rpms.remirepo.net/enterprise/remi-release-7.rpm state=present validate_certs=no' -b
192.168.100.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"/home/vagrant/.ansible/tmp/ansible-tmp-1649929435.8-2896-129465800683746/remi-release-7X8yptA.rpm"
]
}
관리노드에서 확인
[vagrant@node1 ~]$ yum repolist
repo id repo name status
remi-safe Safe Remi's RPM repository for
[vagrant@controller ~]$ ansible wp -m yum_repository -a 'name=remi-safe enabled=no baseurl=http://rpms.remirepo.net/enterprise/7/safe/$basearch/ description=remi-safe' -b
192.168.100.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"repo": "remi-safe",
"state": "present"
}
[vagrant@controller ~]$ ansible wp -m yum_repository -a 'name=remi-php74 enabled=yes baseurl=http://rpms.remirepo.net/en
terprise/7/php74/$basearch/ description=remi-php74' -b
192.168.100.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"repo": "remi-php74",
"state": "present"
}
필요한 패키지
패키지명 | 설명 |
---|---|
httpd | apache |
php | php |
php-mysqlnd | php에서 데이터베이스접속을 위한 라이브러리 |
mariadb | mariadb 클라이언트 |
mariadb-server | mariaddb 서버 |
python2-PyMySQL | python 2.x에서 mysql을 사용하기 위한 패키지 |
"msg":
"warning: /var/cache/yum/x86_64/7/remi-php74/packages/php-7.4.29-1.el7.remi.x86_64.rpm:
Header V4 DSA/SHA1 Signature,
key ID 00f97f56: NOKEY\n\n\nPublic key
for php-7.4.29-1.el7.remi.x86_64.rpm is not installed\n"
키 파일이 없어서 에러가 생긴다길래
rpm_key
remi-key
를 보고 굉장히 다양한 시도를 해봤지만
ansible wp -m rmp_key -a 'key=https://rpms.remirepo.net/RPM-GPG-KEY-remi'
ansible wp -m rpm_key -a 'key=https://rpms.remirepo.net/RPM-GPG-KEY-remi'
ansible wp -m rpm_key -a 'key=00f97f56'
ansible wp -m rpm_key -a 'key=RPM-GPG-KEY-remi'
ansible wp -m rpm_key -a 'key=RPM-GPG-KEY-remi2022'
전부다 실패.. 과연 key 값은 무엇으로 해야할까,,
정답은 repo파일에 있었다!
[vagrant@node1 ~]$ vi /etc/yum.repos.d/remi.repo
[remi]
...
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
[vagrant@controller ~]$ ansible wp -m rpm_key -a 'key=/etc/pki/rpm-gpg/RPM-GPG-KEY-remi' -b
192.168.100.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true
}
[vagrant@controller ~]$ ansible wp -m yum -a 'name=httpd,php,php-mysqlnd,mariadb,mariadb-server,python2-PyMySQL state=present' -b
192.168.100.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"httpd",
"php",
"php-mysqlnd",
"mariadb",
"mariadb-server",
"python2-PyMySQL"
]
}
[vagrant@controller ~]$ ansible wp -m service -a 'name=httpd enabled=yes state=started' -b
[vagrant@controller ~]$ ansible wp -m service -a 'name=mariadb enabled=yes state=started' -b
리눅스 시스템에서 불가능한 명령은 모듈에서도 불가능 -> 패키지 설치처럼 ,
를 사용해서 여러개 한번에 시작은 되지 않아서 하나씩 실행해야함
wordpress.org
다운로드 시 자동으로 최신버전 url이 지정되는데, 항상 버전을 지정하여 다운 받을 것
get_url module
[vagrant@controller ~]$ ansible wp -m get_url -a 'url=https://wordpress.org/wordpress-5.9.3.tar.gz dest=/home/vagrant'
홈 디렉토리에 압축파일 받아오기
[vagrant@controller ~]$ ansible wp -m unarchive -a 'src=/home/vagrant/wordpress-5.9.3.tar.gz remote_src=yes dest=/var/ww
w/html owner=apache group=apache' -b
[vagrant@controller ~]$ ansible wp -m mysql_db -a 'name=wordpress state=present login_user=root'
[vagrant@controller ~]$ ansible wp -m mysql_user -a 'name=wpadm password=P@ssw0rd state=present login_user=root priv="wordpress.*:ALL"'
[vagrant@controller ~]$ ansible wp -m copy -a 'src=/var/www/html/wordpress/wp-config-sample.php remote_src=yes dest=/var/www/html/wordpress/wp-config.php owner=apache group=apache' -b
그룹지정도 동시에
[vagrant@controller ~]$ ansible wp -m replace -a 'path=/var/www/html/wordpress/wp-config.php regexp=database_name_here replace=wordpress' -b
regexp의 값을 찾아 replace 값으로 변경
[vagrant@controller ~]$ ansible wp -m replace -a 'path=/var/www/html/wordpress/wp-config.php regexp=username_here replace=wpadm' -b
[vagrant@controller ~]$ ansible wp -m replace -a 'path=/var/www/html/wordpress/wp-config.php regexp=password_here replace=P@ssw0rd' -b
[vagrant@controller ~]$ ansible wp -m service -a 'name=httpd state=restarted' -b
[vagrant@controller ~]$ ansible wp -m service -a 'name=mariadb state=restarted' -b
[vagrant@controller ~]$ ansible wp -m service -a 'name=httpd state=stopped' -b
[vagrant@controller ~]$ ansible wp -m service -a 'name=mariadb state=stopped' -b
[vagrant@controller ~]$ ansible wp -m file -a 'path=/var/www/html/wordpress state=absent' -b
[vagrant@controller ~]$ ansible wp -m file -a 'path=/home/vagrant/wordpress-5.9.3.tar.gz state=absent' -b
[vagrant@controller ~]$ ansible wp -m yum -a 'name=httpd,php,php-mysqlnd,mariadb,mariadb-server,python2-PyMySQL autoremove=yes state=absent' -b
autoremove를 사용하면 의존성이 관련된 패키지까지 같이 삭제
no를 할 시 삭제되지 않음
[vagrant@controller ~]$ ansible wp -m file -a 'name=/var/lib/mysql state=absent' -b
삭제하지 않으면 다음에 데이터베이스 패키지를 재설치 후 실행 시, 유저 정보, 데이터베이스 정보가 남아 있게 되니 반드시 삭제
[vagrant@controller ~]$ ansible wp -m yum -a 'name=remi-release autoremove=yes state=absent' -b
yaml docs
yaml 형식의 파일을 작성하여 실행
ansible-playbook <YAMLFILE_NAME>
' '
안에 value를 넣어 해결|
, >
를 사용하여 멀티라인으로 작성- hosts: wp
tasks:
- yum:
name: https://rpms.remirepo.net/enterprise/remi-release-7.rpm
state: present
validate_certs: no
- yum_repository:
name: remi-safe
enabled: no
baseurl: http://rpms.remirepo.net/enterprise/7/safe/$basearch/
description: remi-safe
- yum_repository:
name: remi-php74
enabled: yes
baseurl: http://rpms.remirepo.net/enterprise/7/php74/$basearch/
description: remi-php74
- rpm_key:
key: /etc/pki/rpm-gpg/RPM-GPG-KEY-remi
- yum:
name: httpd,php,php-mysqlnd,mariadb,mariadb-server,python2-PyMySQL
state: present
- service:
name: httpd
enabled: yes
state: started
- service:
name: mariadb
enabled: yes
state: started
- get_url:
url: https://wordpress.org/wordpress-5.9.3.tar.gz
dest: /home/vagrant
- unarchive:
src: /home/vagrant/wordpress-5.9.3.tar.gz
remote_src: yes
dest: /var/www/html
owner: apache
group: apache
- mysql_db:
name: wordpress
state: present
login_user: root
- mysql_user:
name: wpadm
password: P@ssw0rd
state: present
login_user: root
priv: "wordpress.*:ALL"
- copy:
src: /var/www/html/wordpress/wp-config-sample.php
remote_src: yes
dest: /var/www/html/wordpress/wp-config.php
owner: apache
group: apache
- replace:
path: /var/www/html/wordpress/wp-config.php
regexp: database_name_here
replace: wordpress
- replace:
path: /var/www/html/wordpress/wp-config.php
regexp: username_here
replace: wpadm
- replace:
path: /var/www/html/wordpress/wp-config.php
regexp: password_here
replace: P@ssw0rd
- hosts: wp
tasks:
- service:
name: httpd
state: stopped
- service:
name: mariadb
state: stopped
- file:
path: /var/www/html/wordpress
state: absent
- file:
path: /home/vagrant/wordpress-5.9.3.tar.gz
state: absent
- yum:
name: httpd,php,php-mysqlnd,mariadb,mariadb-server,python2-PyMySQL
state: absent
autoremove: yes
- file:
name: /var/lib/mysql
state: absent
- yum:
name: remi-release
autoremove: yes
state: absent
잘 보고 갑니다.
세줄 요약 가능할까요?