1. 程式人生 > >ansible案例-安裝nginx

ansible案例-安裝nginx

創建 安裝 開機啟動 linu ucc 主機 pre selinux brush

一、創建目錄:

mkidr -p playbook/{files,templates}

二、自定義index.html文件

$ vim playbook/templates/index.html.j2

---------------------------------------------------------->

<html>
<head>
<title>Welcome to ansible</title>
<body>
        <h1>nginx,configured by Ansible</h1>
        <p>If you cat see this,Ansible successfully installed nginx.</p>
</body>
</head>
</html>

三、配置web_nginx.yml

$ vim playbook/web_nginx.yml

--------------------------------------------->

- hosts: 172.16.1.201                          //目標主機
  remote_user: root                           //遠程主機用戶
  tasks:
  - name: install epel-release                     //centos安裝nginx前,需先安裝epel-release
    command: yum install epel-release -y
  - name: install libselinux-python                  //利用ansible copy 文件需安裝此包
    command: yum install libselinux-python -y
  - name: install nginx
    command: yum install nginx -y
  - name: copy html
    copy: src="templates/index.html.j2" dest="/usr/share/nginx/html/index.html"      //copy自定義html到指定目錄
    tags: html
    notify:
    - server restart
  - name: server start
    service: name=nginx state=started enabled=true        //啟動並設置開機啟動服務
  handlers:
  - name: server restart
    service: name=nginx state=restarted

四、執行配置文件web_nginx.yml

$ ansible-playbook  web_nginx.yml

結果如下圖

技術分享

運行http://172.16.1.201查看結果

技術分享

ansible案例-安裝nginx