Ansible-playbook(安装Apache)

Exercise

  1. Install Apache on all web hosts
  2. Modify the listening port of the configuration file to 8080
  3. Set the default homepage hello world
  4. Start service
  5. Set power-on auto-start

Server planning

MANAGER: IP 192.168.1.10
WEB1: IP 192.168.1.21 WEB2
: IP 192.168.1.22

Installation prerequisite: (need to enter the ansible file and run "/etc/ansible")

  1. The basic configuration of ansible has been completed, as shown below:
[root@manager ansible]# ansible web -m ping
web1 | SUCCESS => {
    
    
    "changed": false, 
    "ping": "pong"
}
web2 | SUCCESS => {
    
    
    "changed": false, 
    "ping": "pong"

2. The YUM source configuration of all machines is complete.

[root@web1 ~]# yum repolist
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
源标识                   源名称                                           状态
base/7/x86_64            CentOS-7 - Base - mirrors.aliyun.com             10,097
extras/7/x86_64          CentOS-7 - Extras - mirrors.aliyun.com              323
local_repo               CentOS-7 - Base                                   9,911
updates/7/x86_64         CentOS-7 - Updates - mirrors.aliyun.com           1,446
repolist: 21,777

Start writing YAML files
[root@ansible ansible]# vim http.yml
---
- hosts: web
  remote_user: root
  tasks:
    - name: install one specific version of Apache
      yum:
        name: httpd        //安装Apache
        state: installed
    - lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: '^Listen '
        line: 'Listen 8080'        //修改端口为8080
    - service:
        name: httpd
        enabled: yes        //开机自启
        state: restarted
    - copy:
        src: /root/index.html        //修改主页,可以自己写个页面
        dest: /var/www/html/index.html
When running, you need to delete the comment information with "//" to avoid reporting errors!
[root@manager ansible]# echo 'hello world' > /root/index.html
[root@manager ansible]# ansible-playbook http.yml 

PLAY [web] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [web1]
ok: [web2]

TASK [install one specific version of Apache] **********************************
changed: [web2]
changed: [web1]

TASK [lineinfile] **************************************************************
changed: [web1]
changed: [web2]

TASK [service] *****************************************************************
changed: [web1]
changed: [web2]

TASK [copy] ********************************************************************
changed: [web1]
changed: [web2]

PLAY RECAP *********************************************************************
web1                       : ok=5    changed=4    unreachable=0    failed=0   
web2                       : ok=5    changed=4    unreachable=0    failed=0   
Check if the installation is successful
[root@manager ansible]# curl 192.168.1.21:8080
hello world

Guess you like

Origin blog.csdn.net/weixin_45942735/article/details/104293674