jy166102 发表于 2018-1-3 06:17:04

使用ansible 完成yum安装lamp环境

  使用ansible 完成yum安装lamp环境
# cd /etc/ansible/playbook/
# ls
  lamp
# tree lamp/
  lamp/
  ├── group_vars
  │   └── lamp-vars
  ├── hosts
  ├── roles
  │   ├── init_sys
  │   │   └── tasks
  │   │       └── mail.yml
  │   └── install
  │       ├── handlers
  │       │   └── main.yml
  │       ├── tasks
  │       │   ├── install_apache.yml
  │       │   ├── install_mysql.yml
  │       │   ├── install_php.yml
  │       │   └── main.yml
  │       └── templates
  │         ├── httpd.conf
  │         └── phpinfo.php
  └── site.yml
  8 directories, 11 files
  变量文件
# cat group_vars/lamp-vars
  pkg1: httpd
  pkg2: mariadb
  pkg3: mariadb-server
  pkg4: php
  pkg5: php-mysql
  系统初始化(禁用SElinux,停止防火墙,配置yum源)
# cat roles/init_sys/tasks/mail.yml
  ---
  - name: configure yum.repo
  shell: yum instalyum install http://mirrors.163.com/centos/7.4.1708/extras/x86_64/Packages/epel-release-7-9.noarch.rpm -y
  - name: stop firewalld
  shell: systemctl stop firewalld
  - name: disable firewalld
  shell: systemctl disable firewalld
  - name: stop selinux
  shell: sed 's/=permissive/=disabled/' /etc/selinux/config | setenforce 0
  安装apache
# cat roles/install/tasks/install_apache.yml
  ---
  - name: install apache
  yum: name=httpd state=installed
  - name: start apache
  command: systemctl start httpd
  - name:deploy apache rules
  template: src=/etc/ansible/playbook/lamp/roles/install/templates/httpd.conf dest=/etc/httpd/conf/httpd.conf
  notify: restart apache
  - name: wait for apache to start
  wait_for: port=80
  安装mysql
# cat roles/install/tasks/install_mysql.yml
  ---
  - nmae: install mysql
  yum: pkg={{ pkg2 }} state=latest
  - name: install mysql-sever
  yum: pkg={{ pkg3 }} state=latest
  - name: start mysql
  command: systemctl start mariadb
  安装php
# cat roles/install/tasks/install_php.yml
  ---
  - name: install php
  yum: pkg={{ pkg4 }} state=latest
  - name: install php-mysql
  yum: pkg={{ pkg5 }} state=latest
  - name: /var/www/html
  template: src=/etc/ansible/playbook/lamp/roles/install/templates/phpinfo.php dest=/var/www/html/phpinfo.php
  notify: restart mysql
  - name: wait for mysql to start
  wait_for: port=3306
  调用notify,当notify中有触发动作时调用
# cat roles/install/handlers/main.yml
  ---
  - name: restart apache
  service: name=apache state=restarted
  - name: restart mysql
  service: name=mysql state=restarted
  模板文件
  1)cat roles/install/templates/httpd.conf
  #
  # If your host doesn't have a registered DNS name, enter its IP address here.
  #
  ServerName 192.168.138.13:80
  .....................................................
  .........................省略
  2)# cat roles/install/templates/phpinfo.php
  ======================================================================
  this       is          a          test         web
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  入口文件
# cat site.yml
  ---
  - hosts: lamp-vars
  remote_user: root
  roles:
  - init_sys
  - install
页: [1]
查看完整版本: 使用ansible 完成yum安装lamp环境