[Ansible] Reduce Skipping

gununoo·2022년 10월 13일
0

Ansible

목록 보기
4/4
post-thumbnail

실습 환경 구성

control 	ubuntu 2004 	211.183.3.166 	10.10.10.10
node1 		CentOS7 		211.183.3.161 	10.10.10.11
node2 		CentOS7 		211.183.3.162 	10.10.10.12
node3 		CentOS7 		211.183.3.163 	10.10.10.13
node4 		ubuntu 2004 	211.183.3.164 	10.10.10.14
node5 		ubuntu 2004 	211.183.3.165 	10.10.10.15
  • Vagrantfile 작성
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.define "control" do |cfg|
    cfg.vm.box = "generic/ubuntu2004"
    cfg.vm.provider :libvirt do |resource|
      resource.cpus = 4
      resource.memory = 4096
    end
    cfg.vm.host_name = "control"
    cfg.vm.network "public_network", :dev => "br0",  :type =>"bridge", ip: "211.183.3.166"
    cfg.vm.network "private_network", ip: "10.10.10.10"
    cfg.vm.network "forwarded_port", guest: 22, host: 20010, id: "ssh"
    # 앤서블 설치와 개인키 추가
    cfg.vm.provision "shell", inline: "apt-get -y install ansible"
    cfg.vm.provision "file", source: "mykey.pem", destination: "/home/vagrant/.ssh/id_rsa"
    cfg.vm.provision "shell", inline: "chmod 600 /home/vagrant/.ssh/id_rsa"
    # ssh-keyscan 을 통한 노드등록
    cfg.vm.provision "shell", inline: "ssh-keyscan 10.10.10.11 >> /home/vagrant/.ssh/known_hosts"
    cfg.vm.provision "shell", inline: "ssh-keyscan 10.10.10.12 >> /home/vagrant/.ssh/known_hosts"
    cfg.vm.provision "shell", inline: "ssh-keyscan 10.10.10.13 >> /home/vagrant/.ssh/known_hosts"
    cfg.vm.provision "shell", inline: "ssh-keyscan 10.10.10.14 >> /home/vagrant/.ssh/known_hosts"
    cfg.vm.provision "shell", inline: "ssh-keyscan 10.10.10.15 >> /home/vagrant/.ssh/known_hosts"
    cfg.vm.provision "shell", inline: "chown vagrant.vagrant /home/vagrant/.ssh/known_hosts"
  end

  config.vm.define "node1" do |cfg|
    cfg.vm.box = "centos/7"
    cfg.vm.host_name = "node1"
    cfg.vm.network "public_network", :dev => "br0",  :type =>"bridge", ip: "211.183.3.161"
    cfg.vm.network "private_network", ip: "10.10.10.11"
    cfg.vm.network "forwarded_port", guest: 22, host: 20011, id: "ssh"
    # 공개키 등록
    cfg.vm.provision "file", source: "mykey.pem.pub", destination: "/home/vagrant/.ssh/mykey.pem.pub"
    cfg.vm.provision "shell", inline: "cat /home/vagrant/.ssh/mykey.pem.pub >> /home/vagrant/.ssh/authorized_keys"
  end

  config.vm.define "node2" do |cfg|
    cfg.vm.box = "centos/7"
    cfg.vm.host_name = "node2"
    cfg.vm.network "public_network", :dev => "br0",  :type =>"bridge", ip: "211.183.3.162"
    cfg.vm.network "private_network", ip: "10.10.10.12"
    cfg.vm.network "forwarded_port", guest: 22, host: 20012, id: "ssh"
    # 공개키 등록
    cfg.vm.provision "file", source: "mykey.pem.pub", destination: "/home/vagrant/.ssh/mykey.pem.pub"
    cfg.vm.provision "shell", inline: "cat /home/vagrant/.ssh/mykey.pem.pub >> /home/vagrant/.ssh/authorized_keys"
  end

  config.vm.define "node3" do |cfg|
    cfg.vm.box = "centos/7"
    cfg.vm.host_name = "node3"
    cfg.vm.network "public_network", :dev => "br0",  :type =>"bridge", ip: "211.183.3.163"
    cfg.vm.network "private_network", ip: "10.10.10.13"
    cfg.vm.network "forwarded_port", guest: 22, host: 20013, id: "ssh"
    # 공개키 등록
    cfg.vm.provision "file", source: "mykey.pem.pub", destination: "/home/vagrant/.ssh/mykey.pem.pub"
    cfg.vm.provision "shell", inline: "cat /home/vagrant/.ssh/mykey.pem.pub >> /home/vagrant/.ssh/authorized_keys"
  end

  config.vm.define "node4" do |cfg|
    cfg.vm.box = "generic/ubuntu2004"
    cfg.vm.host_name = "ubuntu1"
    cfg.vm.network "public_network", :dev => "br0", :type => "bridge", ip: "211.183.3.164"
    cfg.vm.network "forwarded_port", guest: 22, host: 20014, id: "ssh"
    cfg.vm.network "private_network", ip: "10.10.10.14"
    # 공개키 등록
    cfg.vm.provision "file", source: "mykey.pem.pub", destination: "/home/vagrant/.ssh/mykey.pem.pub"
    cfg.vm.provision "shell", inline: "cat /home/vagrant/.ssh/mykey.pem.pub >> /home/vagrant/.ssh/authorized_keys"
  end

  config.vm.define "node5" do |cfg|
    cfg.vm.box = "generic/ubuntu2004"
    cfg.vm.host_name = "ubuntu2"
    cfg.vm.network "public_network", :dev => "br0", :type => "bridge", ip: "211.183.3.165"
    cfg.vm.network "forwarded_port", guest: 22, host: 20015, id: "ssh"
    cfg.vm.network "private_network", ip: "10.10.10.15"
    # 공개키 등록
    cfg.vm.provision "file", source: "mykey.pem.pub", destination: "/home/vagrant/.ssh/mykey.pem.pub"
    cfg.vm.provision "shell", inline: "cat /home/vagrant/.ssh/mykey.pem.pub >> /home/vagrant/.ssh/authorized_keys"
  end
end
  • 배포
[root@hypervisor project1]# vagrant up 
  • 배포 확인
[root@hypervisor project1]# vagrant status 
Current machine states:

control                   running (libvirt)
node1                     running (libvirt)
node2                     running (libvirt)
node3                     running (libvirt)
node4                     running (libvirt)
node5                     running (libvirt)

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.

  • control 노드 접속
[root@hypervisor project1]# vagrant ssh control 
vagrant@control:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:eb:9f:9b brd ff:ff:ff:ff:ff:ff
    inet 192.168.121.90/24 brd 192.168.121.255 scope global dynamic eth0
       valid_lft 2252sec preferred_lft 2252sec
    inet6 fe80::5054:ff:feeb:9f9b/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:b8:de:06 brd ff:ff:ff:ff:ff:ff
    inet 211.183.3.166/24 brd 211.183.3.255 scope global eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:feb8:de06/64 scope link 
       valid_lft forever preferred_lft forever
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:5e:f0:a8 brd ff:ff:ff:ff:ff:ff
    inet 10.10.10.10/24 brd 10.10.10.255 scope global eth2
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe5e:f0a8/64 scope link 
       valid_lft forever preferred_lft forever
vagrant@control:~$ 


vagrant@control:~$ ansible --version 
ansible 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/vagrant/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0]

vagrant@control:~$ sudo vi /etc/ansible/hosts 
[centos]
centos1 anisble_host=10.10.10.11
centos2 ansible_host=10.10.10.12
centos3 ansible_host=10.10.10.13

[ubuntu]
ubuntu1 ansible_host=10.10.10.14
ubuntu2 ansible_host=10.10.10.15

[east]
centos1
centos2
ubuntu1

[west]
centos3
ubuntu2
vagrant@control:~$ ansible all -m ping 
ubuntu2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
ubuntu1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
centos2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
centos3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
centos1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

  • setup 정보 보기
vagrant@control:~$ ansible localhost -m setup > setup.txt 
vagrant@control:~$ vi setup.txt 
  • installnginx
vagrant@control:~$ vi installnginx.yaml 
---
- name: install nginx
  hosts: all
  become: yes
 
  tasks:
    - name: epel
      action: "{{ ansible_pkg_mgr }} name=epel-release state=latest"
      when: ansible_distribution == 'CentOS'
 
    - name: install web server
      action: "{{ ansible_pkg_mgr }} name=nginx state=present"
      when: ansible_distribution == 'CentOS'
 
    - name: upload index.html
      get_url: url=https://www.nginx.com dest=/usr/share/nginx/html mode=0644
      when: ansible_distribution == 'CentOS'
 
    - name: start nginx
      service: name=nginx state=started
      when: ansible_distribution == 'CentOS'
 
    - name: install nginx on ubuntu
      action: "{{ ansible_pkg_mgr }} name=nginx state=present update_cache=yes"
      when: ansible_distribution == 'Ubuntu'
 
    - name: upload index.html
      get_url: url=https://www.nginx.com dest=/var/www/html/
               mode=0644 validate_certs=no
      when: ansible_distribution == 'Ubuntu'
  • 배포
vagrant@control:~$ ansible-playbook installnginx.yaml 

PLAY [install nginx] ******************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************
ok: [centos1]
ok: [centos2]
ok: [centos3]
ok: [ubuntu2]
ok: [ubuntu1]

TASK [epel] ***************************************************************************************************************************************************************************************************
skipping: [ubuntu1]
skipping: [ubuntu2]
changed: [centos2]
changed: [centos1]
changed: [centos3]

TASK [install web server] *************************************************************************************************************************************************************************************
skipping: [ubuntu1]
skipping: [ubuntu2]
changed: [centos2]
changed: [centos1]
changed: [centos3]

TASK [upload index.html] **************************************************************************************************************************************************************************************
skipping: [ubuntu1]
skipping: [ubuntu2]
changed: [centos2]
changed: [centos1]
changed: [centos3]

TASK [start nginx] ********************************************************************************************************************************************************************************************
skipping: [ubuntu1]
skipping: [ubuntu2]
changed: [centos3]
changed: [centos2]
changed: [centos1]

TASK [install nginx on ubuntu] ********************************************************************************************************************************************************************************
skipping: [centos1]
skipping: [centos2]
skipping: [centos3]
changed: [ubuntu2]
changed: [ubuntu1]

TASK [upload index.html] **************************************************************************************************************************************************************************************
skipping: [centos1]
skipping: [centos2]
skipping: [centos3]
changed: [ubuntu1]
changed: [ubuntu2]

PLAY RECAP ****************************************************************************************************************************************************************************************************
centos1                    : ok=5    changed=4    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
centos2                    : ok=5    changed=4    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
centos3                    : ok=5    changed=4    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
ubuntu1                    : ok=3    changed=2    unreachable=0    failed=0    skipped=4    rescued=0    ignored=0   
ubuntu2                    : ok=3    changed=2    unreachable=0    failed=0    skipped=4    rescued=0    ignored=0   ```

skip이 여러 차례 발생한다.

  • 211.183.3.161 ~ 165 접속

  • removenginx
vagrant@control:~$ vi removenginx.yaml
---
- name: remove nginx
  hosts: all
  become: yes
 
  tasks:
    - name: remove nginx
      yum:
        name: ['epel-release', 'nginx']
        state: absent
      when: ansible_distribution == 'CentOS'
 
    - name: remove nginx on ubuntu
      apt:
        name: nginx
        state: absent
        autoremove: true
      when: ansible_distribution == 'Ubuntu'
 
    - name: upload index.html
      get_url: url=https://www.nginx.com dest=/var/www/html/
               mode=0644 validate_certs=no
      when: ansible_distribution == 'Ubuntu'

include_tasks

  • “include_tasks”를 활용한 skipping 줄이기
    4대정도의 서버에서는 크게 문제가 없겠지만 다수의 서버가 동작하는 환경에서 배포판에 맞지 않는 명령어를 전달하여 이를 확인한 뒤, skipping 하는 작업은 서버에 부담을 작용할 것이다. 이를 줄이기 위하여 “include_tasks”를 사용할 수 있다.

  • centos.yaml

vagrant@control:~$ vi centos.yaml 
    - name: epel
      action: "{{ ansible_pkg_mgr }} name=epel-release state=latest"

    - name: install web server
      action: "{{ ansible_pkg_mgr }} name=nginx state=present"

    - name: upload index.html
      get_url: url=https://www.nginx.com dest=/usr/share/nginx/html mode=0644

    - name: start nginx
      service: name=nginx state=started
  • ubuntu.yaml
vagrant@control:~$ vi ubuntu.yaml 
    - name: install nginx on ubuntu
      action: "{{ ansible_pkg_mgr }} name=nginx state=present update_cache=yes"

    - name: upload index.html
      get_url: url=https://www.nginx.com dest=/var/www/html/
               mode=0644 validate_certs=no
  • installnginx_include_task.yaml
vagrant@control:~$ vi installnginx_include_task.yaml 
---
- name: install nginx using include_tasks
  hosts: all
  become: yes

  tasks:
    - name: centos
      include_tasks: centos.yaml
      when: ansible_distribution == 'CentOS'

    - name: ubuntu 
      include_tasks: ubuntu.yaml 
      when: ansible_distribution == 'Ubuntu'
  • 배포
vagrant@control:~$ ansible-playbook installnginx_include_task.yaml 

PLAY [install nginx using include_tasks] **********************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************
ok: [centos1]
ok: [centos2]
ok: [centos3]
ok: [ubuntu1]
ok: [ubuntu2]

TASK [centos] *************************************************************************************************************************************************************************************************
skipping: [ubuntu1]
skipping: [ubuntu2]
included: /home/vagrant/centos.yaml for centos2, centos1, centos3

TASK [epel] ***************************************************************************************************************************************************************************************************changed: [centos3]
changed: [centos1]
changed: [centos2]

TASK [install web server] *************************************************************************************************************************************************************************************
ok: [centos3]
ok: [centos2]
ok: [centos1]

TASK [upload index.html] **************************************************************************************************************************************************************************************
changed: [centos3]
changed: [centos2]
changed: [centos1]

TASK [start nginx] ********************************************************************************************************************************************************************************************
ok: [centos1]
ok: [centos2]
ok: [centos3]

TASK [ubuntu] *************************************************************************************************************************************************************************************************
skipping: [centos1]
skipping: [centos2]
skipping: [centos3]
included: /home/vagrant/ubuntu.yaml for ubuntu1, ubuntu2

TASK [install nginx on ubuntu] ********************************************************************************************************************************************************************************
ok: [ubuntu2]
ok: [ubuntu1]

TASK [upload index.html] **************************************************************************************************************************************************************************************
changed: [ubuntu1]
changed: [ubuntu2]

PLAY RECAP ****************************************************************************************************************************************************************************************************
centos1                    : ok=6    changed=2    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
centos2                    : ok=6    changed=2    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
centos3                    : ok=6    changed=2    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
ubuntu1                    : ok=4    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
ubuntu2                    : ok=4    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

skip이 각 1회로 줄었다.

  • nginx 지우기
vagrant@control:~$ ansible-playbook removenginx.yaml 

PLAY [remove nginx] *******************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************
ok: [centos3]
ok: [centos2]
ok: [centos1]
ok: [ubuntu2]
ok: [ubuntu1]

TASK [remove nginx] *******************************************************************************************************************************************************************************************
skipping: [ubuntu1]
skipping: [ubuntu2]
changed: [centos3]
changed: [centos2]
changed: [centos1]

TASK [remove nginx on ubuntu] *********************************************************************************************************************************************************************************
skipping: [centos1]
skipping: [centos2]
skipping: [centos3]
changed: [ubuntu1]
changed: [ubuntu2]

TASK [upload index.html] **************************************************************************************************************************************************************************************
skipping: [centos1]
skipping: [centos2]
skipping: [centos3]
changed: [ubuntu1]
changed: [ubuntu2]

PLAY RECAP ****************************************************************************************************************************************************************************************************
centos1                    : ok=2    changed=1    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
centos2                    : ok=2    changed=1    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
centos3                    : ok=2    changed=1    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
ubuntu1                    : ok=3    changed=2    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
ubuntu2                    : ok=3    changed=2    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

If 조건

  • installnginx_using_if
vagrant@control:~$ vi installnginx_using_if.yaml 
---
- name: install nginx using if
  hosts: all
  become: yes
  vars:
    dist: "{{ 'centos' if ansible_distribution == 'CentOS' 
               else 'ubuntu' if ansible_distribution == 'Ubuntu'
               else 'linux' 
           }}"
  tasks:
    - name: file selection
      include_tasks: "{{ dist }}.yaml"
  • 배포
vagrant@control:~$ ansible-playbook installnginx_using_if.yaml 

PLAY [install nginx using if] *********************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************
ok: [centos3]
ok: [centos1]
ok: [centos2]
ok: [ubuntu1]
ok: [ubuntu2]

TASK [file selection] *****************************************************************************************************************************************************************************************
included: /home/vagrant/centos.yaml for centos1, centos3, centos2
included: /home/vagrant/ubuntu.yaml for ubuntu1, ubuntu2

TASK [epel] ***************************************************************************************************************************************************************************************************
changed: [centos1]
changed: [centos2]
changed: [centos3]

TASK [install web server] *************************************************************************************************************************************************************************************
changed: [centos2]
changed: [centos1]
changed: [centos3]

TASK [upload index.html] **************************************************************************************************************************************************************************************
changed: [centos3]
changed: [centos1]
changed: [centos2]

TASK [start nginx] ********************************************************************************************************************************************************************************************
changed: [centos3]
changed: [centos1]
changed: [centos2]

TASK [install nginx on ubuntu] ********************************************************************************************************************************************************************************
changed: [ubuntu1]
changed: [ubuntu2]

TASK [upload index.html] **************************************************************************************************************************************************************************************
changed: [ubuntu1]
changed: [ubuntu2]

PLAY RECAP ****************************************************************************************************************************************************************************************************
centos1                    : ok=6    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
centos2                    : ok=6    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
centos3                    : ok=6    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
ubuntu1                    : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
ubuntu2                    : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

skipped가 전혀 발생하지 않았음!


Handers

  1. 직전 실행했던 파일의 결과를 확인하고 정상적으로 설치가 되었다면 웹을 통해 확인하기
  2. "1"에 문제가 없다면 removenginx.yaml 실행하기
  3. if 파일에 handler를 적용하여 각 노드에서 nginx가 설치된 뒤 어떤 버전인지 여부를 debug로 출력시켜 주세요.

주의해야 할 점! centos, ubuntu에서 공통적으로 nginx의 버전을 확인할 수 있는 명령??

  • 정상 배포 확인
  • removenginx
vagrant@control:~$ ansible-playbook removenginx.yaml 

PLAY [remove nginx] *******************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************ok: [centos3]
ok: [centos1]
ok: [centos2]
ok: [ubuntu1]
ok: [ubuntu2]

TASK [remove nginx] *******************************************************************************************************************************************************************************************skipping: [ubuntu1]
skipping: [ubuntu2]
changed: [centos1]
changed: [centos3]
changed: [centos2]

TASK [remove nginx on ubuntu] *********************************************************************************************************************************************************************************skipping: [centos1]
skipping: [centos2]
skipping: [centos3]
changed: [ubuntu1]
changed: [ubuntu2]

TASK [upload index.html] **************************************************************************************************************************************************************************************skipping: [centos2]
skipping: [centos1]
skipping: [centos3]
changed: [ubuntu1]
changed: [ubuntu2]

PLAY RECAP ****************************************************************************************************************************************************************************************************
centos1                    : ok=2    changed=1    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
centos2                    : ok=2    changed=1    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
centos3                    : ok=2    changed=1    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
ubuntu1                    : ok=3    changed=2    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
ubuntu2                    : ok=3    changed=2    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

  • centos
vi centos.yaml
    - name: epel
      action: "{{ ansible_pkg_mgr }} name=epel-release state=latest"

    - name: install web server
      action: "{{ ansible_pkg_mgr }} name=nginx state=present"
      notify:
        - trg1

    - name: upload index.html
      get_url: url=https://www.nginx.com dest=/usr/share/nginx/html mode=0644

    - name: start nginx
      service: name=nginx state=started
  • ubuntu
vi ubuntu.yaml 
    - name: install nginx on ubuntu
      action: "{{ ansible_pkg_mgr }} name=nginx state=present update_cache=yes"
      notify:
        - trg1

    - name: upload index.html
      get_url: url=https://www.nginx.com dest=/var/www/html/
               mode=0644 validate_certs=no
  • install nginx using if
vi installnginx_using_if.yaml
---
- name: install nginx using if
  hosts: all
  become: yes
  vars:
    dist: "{{ 'centos' if ansible_distribution == 'CentOS'
               else 'ubuntu' if ansible_distribution == 'Ubuntu'
               else 'linux'
           }}"

  tasks:
    - name: file selection
      include_tasks: "{{ dist }}.yaml"

  handlers:
    - name: trg1
      command: "nginx -v"
      register: result_trg1
      notify:
        - trg2
    - name: trg2
      debug:
        var: result_trg1
  • 배포
vagrant@control:~$ ansible-playbook installnginx_using_if.yaml
PLAY [install nginx using if] *********************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************
ok: [ubuntu1]
ok: [centos3]
ok: [centos2]
ok: [centos1]
ok: [ubuntu2]

TASK [file selection] *****************************************************************************************************************************************************************************************
included: /home/vagrant/centos.yaml for centos3, centos2, centos1
included: /home/vagrant/ubuntu.yaml for ubuntu1, ubuntu2

TASK [epel] ***************************************************************************************************************************************************************************************************
changed: [centos1]
changed: [centos3]
changed: [centos2]

TASK [install web server] *************************************************************************************************************************************************************************************changed: [centos1]
changed: [centos2]
changed: [centos3]

TASK [upload index.html] **************************************************************************************************************************************************************************************changed: [centos3]
changed: [centos2]
changed: [centos1]

TASK [start nginx] ********************************************************************************************************************************************************************************************
changed: [centos3]
changed: [centos2]
changed: [centos1]

TASK [install nginx on ubuntu] ********************************************************************************************************************************************************************************
changed: [ubuntu2]
changed: [ubuntu1]

TASK [upload index.html] **************************************************************************************************************************************************************************************
changed: [ubuntu1]
changed: [ubuntu2]

RUNNING HANDLER [trg1] ****************************************************************************************************************************************************************************************
changed: [ubuntu1]
changed: [ubuntu2]
changed: [centos2]
changed: [centos3]
changed: [centos1]

RUNNING HANDLER [trg2] ****************************************************************************************************************************************************************************************
ok: [ubuntu1] => {
    "result_trg1": {
        "changed": true,
        "cmd": [
            "nginx",
            "-v"
        ],
        "delta": "0:00:00.007056",
        "end": "2022-10-14 00:27:55.903409",
        "failed": false,
        "rc": 0,
        "start": "2022-10-14 00:27:55.896353",
        "stderr": "nginx version: nginx/1.18.0 (Ubuntu)",
        "stderr_lines": [
            "nginx version: nginx/1.18.0 (Ubuntu)"
        ],
        "stdout": "",
        "stdout_lines": []
    }
}
ok: [ubuntu2] => {
    "result_trg1": {
        "changed": true,
        "cmd": [
            "nginx",
            "-v"
        ],
        "delta": "0:00:00.006994",
        "end": "2022-10-14 00:27:55.906846",
        "failed": false,
        "rc": 0,
        "start": "2022-10-14 00:27:55.899852",
        "stderr": "nginx version: nginx/1.18.0 (Ubuntu)",
        "stderr_lines": [
            "nginx version: nginx/1.18.0 (Ubuntu)"
        ],
        "stdout": "",
        "stdout_lines": []
    }
}
ok: [centos2] => {
    "result_trg1": {
        "changed": true,
        "cmd": [
            "nginx",
            "-v"
        ],
        "delta": "0:00:00.009818",
        "end": "2022-10-14 00:27:55.972997",
        "failed": false,
        "rc": 0,
        "start": "2022-10-14 00:27:55.963179",
        "stderr": "nginx version: nginx/1.20.1",
        "stderr_lines": [
            "nginx version: nginx/1.20.1"
        ],
        "stdout": "",
        "stdout_lines": []
    }
}
ok: [centos3] => {
    "result_trg1": {
        "changed": true,
        "cmd": [
            "nginx",
            "-v"
        ],
        "delta": "0:00:00.008736",
        "end": "2022-10-14 00:27:55.983031",
        "failed": false,
        "rc": 0,
        "start": "2022-10-14 00:27:55.974295",
        "stderr": "nginx version: nginx/1.20.1",
        "stderr_lines": [
            "nginx version: nginx/1.20.1"
        ],
        "stdout": "",
        "stdout_lines": []
    }
}
ok: [centos1] => {
    "result_trg1": {
        "changed": true,
        "cmd": [
            "nginx",
            "-v"
        ],
        "delta": "0:00:00.011468",
        "end": "2022-10-14 00:27:55.985955",
        "failed": false,
        "rc": 0,
        "start": "2022-10-14 00:27:55.974487",
        "stderr": "nginx version: nginx/1.20.1",
        "stderr_lines": [
            "nginx version: nginx/1.20.1"
        ],
        "stdout": "",
        "stdout_lines": []
    }
}

PLAY RECAP ****************************************************************************************************************************************************************************************************
centos1                    : ok=8    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
centos2                    : ok=8    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
centos3                    : ok=8    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
ubuntu1                    : ok=6    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
ubuntu2                    : ok=6    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

vagrant@control:~$ 
profile
take a look

0개의 댓글

관련 채용 정보