0706-Ansible, vagrant

hyejin·2022년 7월 6일
0

VM 생성

--- 앤서블 실습 환경 (cpu, ram 용량)
ansible-server 1c 1g
centos-node01 1c 1g
centos-node02 1c 1g
ubuntu-node01 1c 1g
ubuntu-node02 1c 1g

  • Centos7을 ansible-server 로 변경
  • ansible-server를 연결된 복제로 centos-node1 / centos-node2로 복제
  • ubuntu18을 ubuntu-node01로 변경
  • ubuntu-node01을 연결된 복제로 ubuntu-node02로 복제

ip
[centos]
192.168.0.242
192.168.0.248
[ubuntu]
192.168.0.217
192.168.0.252

mobaxterm으로 접속

  • ansible-server
[root@ansible-server ~]# hostnamectl set-hostname ansible-server
[root@ansible-server ~]# exit
  • centos-node01
[root@centos-node01 ~]# hostnamectl set-hostname centos-node01
[root@centos-node01 ~]# exit
  • centos-node02
[root@centos-node02 ~]# hostnamectl set-hostname centos-node02
[root@centos-node02 ~]# exit
  • ubuntu-node01
[root@ubuntu-node01 ~]# hostnamectl set-hostname ubuntu-node01
[root@ubuntu-node01 ~]# exit
  • ubuntu-node02
[root@ubuntu-node02 ~]# hostnamectl set-hostname ubuntu-node02
[root@ubuntu-node02 ~]# exit

vagrant 설치

  • cmd에서 실행
C:\Users\r2com>cd c:\HashiCorp
c:\HashiCorp>vagrant init
https://app.vagrantup.com/boxes/search?provider=virtualbox
c:\HashiCorp>notepad Vagrantfile
config.vm.box = "centos/7"
config.vm.network "public_network"
c:\HashiCorp>vagrant up
c:\HashiCorp>vagrant ssh
sudo yum install -y httpd
sudo systemctl enable --now httpd
c:\HashiCorp>vagrant destroy

ansible-server 에서 실행

[root@ansible-server ~]# yum install epel-release -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.kakao.com
 * extras: mirror.kakao.com
 * updates: mirror.kakao.com
base
extras
updates
updates/7/x86_64/primary_db
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================
 Package                                               Arch
======================================================================================
Installing:
 epel-release                                          noarch

Transaction Summary
======================================================================================
Install  1 Package

Total download size: 15 k
Installed size: 24 k
Downloading packages:
epel-release-7-11.noarch.rpm
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-11.noarch
  Verifying  : epel-release-7-11.noarch

Installed:
  epel-release.noarch 0:7-11

Complete!
[root@ansible-server ~]# yum --enablerepo=epel -y install ansible
[root@ansible-server ~]# vi /etc/ansible/hosts
[centos]
192.168.0.242
192.168.0.248

[ubuntu]
192.168.0.217
192.168.0.252

[root@ansible-server ~]# ansible all -m ping
192.168.0.242 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}
192.168.0.248 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}
192.168.0.217 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).",
    "unreachable": true
}
192.168.0.252 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).",
    "unreachable": true
}
[root@ansible-server ~]# ansible all -m ping -k
SSH password:
192.168.0.248 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.0.242 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.0.217 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
192.168.0.252 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
[root@ansible-server ~]# ansible centos -m ping -k
SSH password:
192.168.0.242 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.0.248 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
[root@ansible-server ~]# ansible ubuntu -m ping -k
SSH password:
192.168.0.252 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
192.168.0.217 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
[root@ansible-server ~]# echo "192.168.0.242" >> inventory.list
[root@ansible-server ~]# echo "192.168.0.217" >> inventory.list
[root@ansible-server ~]# ansible all -i inventory.list -m ping -k
SSH password:
192.168.0.242 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.0.217 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
[root@ansible-server ~]# ansible 192.168.0.242 -i inventory.list -m ping -k
SSH password:
192.168.0.242 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
[root@ansible-server ~]# ansible all --list-hosts
  hosts (4):
    192.168.0.242
    192.168.0.248
    192.168.0.217
    192.168.0.252
[root@ansible-server ~]# ansible all -i inventory.list  --list-hosts
  hosts (2):
    192.168.0.242
    192.168.0.217
[root@ansible-server ~]# ansible all -m shell -a "uptime" -k
SSH password:
192.168.0.248 | CHANGED | rc=0 >>
 14:45:20 up  2:19,  2 users,  load average: 0.00, 0.01, 0.02
192.168.0.242 | CHANGED | rc=0 >>
 14:45:19 up  2:19,  2 users,  load average: 0.00, 0.01, 0.02
192.168.0.252 | CHANGED | rc=0 >>
 14:45:21 up  2:19,  2 users,  load average: 0.00, 0.00, 0.00
192.168.0.217 | CHANGED | rc=0 >>
 14:45:21 up  2:19,  2 users,  load average: 0.00, 0.00, 0.00
[root@ansible-server ~]# ansible all -m shell -a "df -h" -k
SSH password:
192.168.0.242 | CHANGED | rc=0 >>
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 484M     0  484M   0% /dev
tmpfs                    496M     0  496M   0% /dev/shm
tmpfs                    496M  6.8M  489M   2% /run
tmpfs                    496M     0  496M   0% /sys/fs/cgroup
/dev/mapper/centos-root  124G  1.5G  122G   2% /
/dev/sda1               1014M  181M  834M  18% /boot
tmpfs                    100M     0  100M   0% /run/user/0
192.168.0.248 | CHANGED | rc=0 >>
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 484M     0  484M   0% /dev
tmpfs                    496M     0  496M   0% /dev/shm
tmpfs                    496M  6.8M  489M   2% /run
tmpfs                    496M     0  496M   0% /sys/fs/cgroup
/dev/mapper/centos-root  124G  1.5G  122G   2% /
/dev/sda1               1014M  181M  834M  18% /boot
tmpfs                    100M     0  100M   0% /run/user/0
192.168.0.252 | CHANGED | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
udev            462M     0  462M   0% /dev
tmpfs            99M  652K   98M   1% /run
/dev/sda1       126G  3.9G  116G   4% /
tmpfs           493M     0  493M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           493M     0  493M   0% /sys/fs/cgroup
tmpfs            99M     0   99M   0% /run/user/0
192.168.0.217 | CHANGED | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
udev            462M     0  462M   0% /dev
tmpfs            99M  652K   98M   1% /run
/dev/sda1       126G  3.9G  116G   4% /
tmpfs           493M     0  493M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           493M     0  493M   0% /sys/fs/cgroup
tmpfs            99M     0   99M   0% /run/user/0
[root@ansible-server ~]# ansible all -m shell -a "free -h" -k
SSH password:
192.168.0.242 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           990M        103M        799M        6.7M         88M        770M
Swap:          3.9G          0B        3.9G
192.168.0.248 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           990M        105M        797M        6.7M         88M        767M
Swap:          3.9G          0B        3.9G
192.168.0.252 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           985M         97M        726M        652K        160M        751M
Swap:          2.0G          0B        2.0G
192.168.0.217 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           985M         97M        726M        652K        161M        751M
Swap:          2.0G          0B        2.0G

-> 띄어쓰기를 위해 큰 따옴표를 써야함

[root@ansible-server ~]# ansible all -m user -a "name=kosa" -k
SSH password:
192.168.0.248 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1000,
    "home": "/home/kosa",
    "name": "kosa",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1000
}
192.168.0.242 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1000,
    "home": "/home/kosa",
    "name": "kosa",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1000
}
192.168.0.217 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/kosa",
    "name": "kosa",
    "shell": "/bin/sh",
    "state": "present",
    "system": false,
    "uid": 1001
}
192.168.0.252 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/kosa",
    "name": "kosa",
    "shell": "/bin/sh",
    "state": "present",
    "system": false,
    "uid": 1001
}
[root@ansible-server ~]# ansible all -m shell -a "tail -n 1 /etc/passwd" -k
SSH password:
192.168.0.248 | CHANGED | rc=0 >>
kosa:x:1000:1000::/home/kosa:/bin/bash
192.168.0.242 | CHANGED | rc=0 >>
kosa:x:1000:1000::/home/kosa:/bin/bash
192.168.0.217 | CHANGED | rc=0 >>
kosa:x:1001:1001::/home/kosa:/bin/sh
192.168.0.252 | CHANGED | rc=0 >>
kosa:x:1001:1001::/home/kosa:/bin/sh
  • 삭제
[root@ansible-server ~]# ansible all -m user -a "name=kosa state=absent" -k
SSH password:
192.168.0.242 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "force": false,
    "name": "kosa",
    "remove": false,
    "state": "absent"
}
192.168.0.248 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "force": false,
    "name": "kosa",
    "remove": false,
    "state": "absent"
}
192.168.0.252 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "force": false,
    "name": "kosa",
    "remove": false,
    "state": "absent"
}
192.168.0.217 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "force": false,
    "name": "kosa",
    "remove": false,
    "state": "absent"
}

--- 센토스 아파치 애드혹 관리

[root@ansible-server ~]# ansible all -m yum -a "name=httpd state=present" -k
[root@ansible-server ~]# curl https://www.nginx.com/ -o index.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  397k    0  397k    0     0   372k      0 --:--:--  0:00:01 --:--:--  372k
[root@ansible-server ~]# ansible centos -m copy -a "src=index.html dest=/var/www/html/index.html" -k
SSH password:
192.168.0.248 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "8235733676e48ae17d69e099b936c4d0133e16b3",
    "dest": "/var/www/html/index.html",
    "gid": 0,
    "group": "root",
    "md5sum": "e982ab0d5d6cad8081ff67e63ddda693",
    "mode": "0644",
    "owner": "root",
    "size": 406557,
    "src": "/root/.ansible/tmp/ansible-tmp-1657088530.14-10200-217593443857477/source",
    "state": "file",
    "uid": 0
}
192.168.0.242 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "8235733676e48ae17d69e099b936c4d0133e16b3",
    "dest": "/var/www/html/index.html",
    "gid": 0,
    "group": "root",
    "md5sum": "e982ab0d5d6cad8081ff67e63ddda693",
    "mode": "0644",
    "owner": "root",
    "size": 406557,
    "src": "/root/.ansible/tmp/ansible-tmp-1657088530.14-10199-94052195310567/source",
    "state": "file",
    "uid": 0
}
[root@ansible-server ~]# ansible centos -m service -a "name=httpd state=started" -k

  • 방화벽
[root@ansible-server ~]# ansible centos -m shell -a "systemctl status firewalld" -k
SSH password:
192.168.0.248 | FAILED | rc=3 >>
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)non-zero return code
192.168.0.242 | FAILED | rc=3 >>
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)non-zero return code
[root@ansible-server ~]# ansible centos -m shell -a "systemctl start firewalld" -k
SSH password:
192.168.0.242 | CHANGED | rc=0 >>

192.168.0.248 | CHANGED | rc=0 >>
[root@ansible-server ~]# ansible centos -m shell -a "firewall-cmd --permanent --zone=public --add-service=http" -k
SSH password:
192.168.0.242 | CHANGED | rc=0 >>
success
192.168.0.248 | CHANGED | rc=0 >>
success
[root@ansible-server ~]# ansible centos -m shell -a "firewall-cmd --reload" -k        SSH password:
192.168.0.248 | CHANGED | rc=0 >>
success
192.168.0.242 | CHANGED | rc=0 >>
success
  • 서비스 멈추기
[root@ansible-server ~]# ansible centos -m service -a "name=httpd state=stopped" -k
[root@ansible-server ~]# ansible centos -m shell -a "systemctl stop firewalld" -k     SSH password:
192.168.0.242 | CHANGED | rc=0 >>

192.168.0.248 | CHANGED | rc=0 >>

[root@ansible-server ~]# ansible centos -m shell -a "systemctl disable firewalld" -k
SSH password:
192.168.0.248 | CHANGED | rc=0 >>

192.168.0.242 | CHANGED | rc=0 >>
[root@ansible-server ~]# ansible centos -m yum -a "name=httpd state=absent" -k

--- 우분투 아파치 애드혹 관리

[root@ansible-server ~]# ansible ubuntu -m apt -a "name=apache2 state=present" -k
[root@ansible-server ~]# ansible ubuntu -m copy -a "src=index.html dest=/var/www      /html/index.html" -k
SSH password:
192.168.0.217 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "checksum": "8235733676e48ae17d69e099b936c4d0133e16b3",
    "dest": "/var/www/html/index.html",
    "gid": 0,
    "group": "root",
    "md5sum": "e982ab0d5d6cad8081ff67e63ddda693",
    "mode": "0644",
    "owner": "root",
    "size": 406557,
    "src": "/root/.ansible/tmp/ansible-tmp-1657091194.36-11112-72014757839114/so      urce",
    "state": "file",
    "uid": 0
}
192.168.0.252 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "checksum": "8235733676e48ae17d69e099b936c4d0133e16b3",
    "dest": "/var/www/html/index.html",
    "gid": 0,
    "group": "root",
    "md5sum": "e982ab0d5d6cad8081ff67e63ddda693",
    "mode": "0644",
    "owner": "root",
    "size": 406557,
    "src": "/root/.ansible/tmp/ansible-tmp-1657091194.34-11113-243903440796261/s      ource",
    "state": "file",
    "uid": 0
}
  • 192.168.0.217 로 접속

  • 192.168.0.252 로 접속

  • apache 멈추기

[root@ansible-server ~]# ansible ubuntu -m service -a "name=apache2 state=stopped" -k
[root@ansible-server ~]# ansible ubuntu -m service -a "name=apache2 state=started" -k
  • 지우기
[root@ansible-server ~]# ansible ubuntu -m apt -a "name=apache2 state=absent" -k
  • 멱등성 확인
[root@ansible-server ~]# ansible localhost -c local -m lineinfile -a "path=inventory.list line=172.16.0.200" -k
SSH password:
localhost | CHANGED => {
    "backup": "",
    "changed": true,
    "msg": "line added"
}
[root@ansible-server ~]# ansible localhost -c local -m lineinfile -a "path=inventory.list line=172.16.0.200" -k
SSH password:
localhost | SUCCESS => {
    "backup": "",
    "changed": false,
    "msg": ""
}
[root@ansible-server ~]# cat inventory.list
192.168.0.242
192.168.0.217
172.16.0.100
172.16.0.100
172.16.0.200

--- ssh key 활용

[root@ansible-server ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:epnvOC6jf0+y3Rwlf2SaSkTAGDLOD0tE1+bW1FvoHLY root@ansible-server
The key's randomart image is:
+---[RSA 2048]----+
|     .= o=.  . . |
|     + +. +.. = .|
|      =  o o.+ = |
|     . +  o.. E  |
|      . S.  o . o|
|       . o . + = |
|      . = . o + .|
|      o.oB + o . |
|    .o.=++= +    |
+----[SHA256]-----+
[root@ansible-server ~]# ssh-copy-id root@192.168.0.242
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.0.242's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.0.242'"
and check to make sure that only the key(s) you wanted were added.
[root@ansible-server ~]# ssh-copy-id root@192.168.0.248
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.0.248's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.0.248'"
and check to make sure that only the key(s) you wanted were added.

[root@ansible-server ~]# ssh-copy-id root@192.168.0.217
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.0.217's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.0.217'"
and check to make sure that only the key(s) you wanted were added.

[root@ansible-server ~]# ssh-copy-id root@192.168.0.252
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.0.252's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.0.252'"
and check to make sure that only the key(s) you wanted were added.
  • ping 확인
[root@ansible-server ~]# ansible all -m ping
192.168.0.242 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.0.248 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.0.252 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
192.168.0.217 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

--- 플레이북 구조

--- 센토스, 우분투 아파치 설치 플레이북

# vi apache_install.yml
- name: Install apache on centos
  hosts: centos
  gather_facts: no

  tasks:
    - name: install apache web server
      yum: name=httpd state=present 
    - name: upload default index.html for web server
      get_url: url=https://www.nginx.com dest=/var/www/html/ mode=0644
    - name: start apache web server
      service: name=httpd state=started enabled=yes

- name: Install apache on ubuntu
  hosts: ubuntu
  gather_facts: no

  tasks:
    - name: install apache web server
      apt: name=apache2 state=present
    - name: upload default index.html for web server
      get_url: url=https://www.nginx.com dest=/var/www/html/ mode=0644
    - name: start apache web server
      service: name=apache2 state=started
[root@ansible-server ~]# mkdir apache && cd $_
[root@ansible-server apache]# vi apache_install.yml
[root@ansible-server apache]# ansible-playbook apache_install.yml -k
SSH password:

PLAY [Install apache on centos] ******************************************************

TASK [install apache web server] *****************************************************
changed: [192.168.0.242]
changed: [192.168.0.248]

TASK [upload default index.html for web server] **************************************
changed: [192.168.0.242]
changed: [192.168.0.248]

TASK [start apache web server] *******************************************************
changed: [192.168.0.248]
changed: [192.168.0.242]

PLAY [Install apache on ubuntu] ******************************************************

TASK [install apache web server] *****************************************************
changed: [192.168.0.217]
changed: [192.168.0.252]

TASK [upload default index.html for web server] **************************************
changed: [192.168.0.217]
changed: [192.168.0.252]

TASK [start apache web server] *******************************************************
ok: [192.168.0.217]
ok: [192.168.0.252]

PLAY RECAP ***************************************************************************
192.168.0.217              : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.0.242              : ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.0.248              : ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.0.252              : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  • centos-node01로 접속

  • centos-node02로 접속

  • ubuntu-node01로 접속

  • ubuntu-node02로 접속

--- 센토스, 우분투 아파치 삭제 플레이북

# vi apache_remove.yml
- name: Remove apache on centos
  hosts: centos
  gather_facts: no

  tasks:
    - name: remove apache web server
      yum: name=httpd state=absent

- name: Remove apache on ubuntu
  hosts: ubuntu
  gather_facts: no

  tasks:
    - name: remove apache web server
      apt: name=apache2 state=absent
[root@ansible-server apache]# ansible-playbook apache_remove.yml -k
SSH password:

PLAY [Remove apache on centos] *******************************************************

TASK [remove apache web server] ******************************************************
changed: [192.168.0.248]
changed: [192.168.0.242]

PLAY [Remove apache on ubuntu] *******************************************************

TASK [remove apache web server] ******************************************************
changed: [192.168.0.217]
changed: [192.168.0.252]

PLAY RECAP ***************************************************************************
192.168.0.217              : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.0.242              : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.0.248              : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.0.252              : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

0개의 댓글

관련 채용 정보