一:配置一个ubuntu server为lamp服务器并且自带drupal(这是一个类似wordpress一样的php应用)
注:不要使用最新版的ubuntu server 推荐使用14.04,新版的php默认是php7
先看服务器架构
文件目录结构
playbook
lamp.yml:
--- - hosts: 10.0.0.128 vars_files: - vars.yml #指定变量为一个独立的vars.yml文件 pre_tasks: # 指定在主任务之前运行的任务,更新apt cache - name: Update apt cache if need. apt: update_cache=yes cache_valid_time=3600 handlers: # 触发器,只有在任务通知改变了服务器状态的时候被触发,并且只在主任务执行完毕 # 之后运行一次 这里的作用是在apache 配置文件改变的时候,重启apache服务器 - name: restart apache service: name=apache2 state=restarted tasks: - name: Get software for apt repository management. apt: name={{ item }} state=installed # 安装apt的帮助模块,apt_repository模块需要 with_items: - python-apt - python-pycurl - name: Add ondrej repository for later version of PHP # 添加php5.6的仓库 apt_repository: repo='ppa:ondrej/php5-5.6' update_cache=yes - name: "Install apache,Mysql,Php,and other dependencies" # 安装lamp需要的软件和php扩展 apt: name={{ item }} state=installed with_items: - git - curl - sendmail - apache2 - php5 - php5-common - php5-mysql - php5-cli - php5-curl - php5-gd - php5-dev - php5-mcrypt - php-apc - php-pear - python-mysqldb - mysql-server - name: Disable the firewall # 关闭防火墙(测试目的) service: name=ufw state=stopped - name: "Start apache, Mysql,and Php" service: "name={{ item }} state=started" with_items: - apache2 - mysql - name: Enable Apache rewrite module (require for Drupal) # 打开apache的rewrite 模块 apache2_module: name=rewrite state=present notify: restart apache - name: Add apache virtualhost for drupal 8 development template: # 复制虚拟主机(jinja2)模板到apache的可用目录 src: "templates/drupal.dev.conf.j2" dest: "/etc/apache2/sites-available/{{ domain }}.dev.conf" owner: root group: root mode: 0644 notify: restart apache - name: Symlink Drupal virtualhost to sites-enabled # 联接虚拟主机到apache的enable目录 file: src: "/etc/apache2/sites-available/{{ domain }}.dev.conf" dest: "/etc/apache2/sites-enabled/{{ domain }}.dev.conf" state: link notify: restart apache - name: Remove default virtualhost file #移除默认的虚拟主机文件 file: path: "/etc/apache2/sites-enabled/000-default" state: absent notify: restart apache - name: Enable upload progress via APC lineinfile: # 这个模块的作用是确保一行文本出现在一个文件之中 dest: "/etc/php5/apache2/conf.d/20-apcu.ini" regexp: "^apc.rfc1867" line: "apc.rfc1867 = 1" state: present notify: restart apache - name: Remove the mysql test database mysql_db: db=test state=absent # mysql_db 模块 操作 mysql - name: Create a database for drupal mysql_db: "db={{ domain }} state=present" - name: Install Composer into the current directory # Composer 安装drush的相关依赖 shell: > curl -sS https://getcomposer.org/installer | php creates=/usr/local/bin/composer - name: Move Composer into global-accessible localtion shell: > mv composer.phar /usr/local/bin/composer # 上面的shell命令会生成composer.phar文件 creates=/usr/local/bin/composer - name: Check out drush master branch git: repo: https://github.com/drush-ops/drush.git dest: /opt/drush - name: Install Drush dependences with Composer shell: > /usr/local/bin/composer install chdir=/opt/drush # 会自动查找composer.json文件,安装相关依赖 creates=/opt/drush/vendor/autoload.php - name: Create drush bin symlink # drush命令全局可用 file: src: /opt/drush/drush dest: /usr/local/bin/drush state: link - name: Check out Drual Core to apache docroot git: repo: http://git.drupal.org/project/drupal.git version: "{{ drupal_core_version }}" dest: "{{ drupal_core_path }}" - name: Install Drual # 在指定的目录安装drual,生成settings.php文件 command: > drush si -y --site-name="{{ drupal_site_name }}" --account-name=admin --account-pass=admin --db-url=mysql://root@localhost/{{ domain }} chdir={{ drupal_core_path }} creates={{ drupal_core_path }}/sites/default/settings.php notify: restart apache - name: Set permisions properly on settings.php # 修改文件权限 file: path: "{{ drupal_core_path }}/sites/default/settings.php" mode: 0744 - name: Set permissions on files directory # 修改目录权限 file: path: "{{ drupal_core_path }}/sites/default/files" mode: 0777 state: directory recurse: yes
jinja2模板(apache虚拟主机)
drupal.dev.conf.j2:
ServerAdmin jwh5566@localhost ServerName {{ domain }}.dev ServerAlias wwww.{{ domain }}.dev DocumentRoot {{ drupal_core_path }} Options FollowSymLinks Indexes AllowOverride All
变量文件
vars.yml:
--- # the core version you want use (e.g. 6.x, 7.x, 8.x) drupal_core_version: "8.0.x" # the path where drupal will be download and installed drupal_core_path: "/var/www/drupal-{{ drupal_core_version }}-dev" # the resulting domain will be [domain].dev (with .dev append) domain: "drupaltest" # your Drupal site name drupal_site_name: "D8 Test"
运行结果:
测试访问web页面:(修改hosts文件添加ip和域名的映射)
二:配置一个tomcat服务并且自带solr的搜索服务器
服务器架构
文件目录结构:
playbook
tomcat_solr.yml:
--- - hosts: 10.0.0.128 vars_files: - vars.yml pre_tasks: - name: update apt cache if need # 更新apt 缓存 apt: update_cache=yes cache_valid_time=3600 handlers: # 配置文件修改触发tomcat 重启 - name: restart tomcat service: name=tomcat7 state=restarted tasks: - name: install tomcat 7 apt: "name={{ item }} state=installed" with_items: - tomcat7 - tomcat7-admin # web页面管理tomcat,方便重启 - name: ensure tomcat7 is started and enabled service: name=tomcat7 state=started - name: download solr get_url: url: https://archive.apache.org/dist/lucene/solr/4.9.1/solr-4.9.1.tgz dest: "{{ download_dir }}/solr-4.9.1.tgz" # 这里不用目录是因为防止每次执行重新下载 - name: expand solr command: > tar -C /tmp -xvzf {{ download_dir }}/solr-4.9.1.tgz creates={{ download_dir }}/solr-4.9.1/dist/solr-4.9.1.war - name: copy solr into place command: > cp -r {{ download_dir }}/solr-4.9.1 {{ solr_dir }} # 复制war包到指定目录 creates=`solr_dir`/dist/solr-4.9.1.war - name: copy solr components into place shell: > cp -r {{ item.src }} {{ item.dest }} creates={{ item.creates }} with_items: # solr configration and war file - { src: "{{ solr_dir }}/example/webapps/solr.war", dest: "{{ solr_dir }}/solr.war", creates: "{{ solr_dir }}/solr.war" } - { src: "{{ solr_dir }}/example/solr/*", # 复制solr的应用文件 dest: "{{ solr_dir }}/", creates: "{{ solr_dir }}/solr.xml" } # solr log4j logging configration - { src: "{{ solr_dir }}/example/lib/ext/*", # 复制所有相关的jar包 dest: "/var/lib/tomcat7/shared", creates: "/var/lib/tomcat7/shared/log4j-1.2.17.jar" } - { src: "{{ solr_dir }}/example/resources/log4j.properties", dest: "/var/lib/tomcat7/shared/classes", creates: "/var/lib/tomcat7/shared/classes/log4j.properties" } notify: restart tomcat - name: ensure solr example directory is absent # 这里很重要solr会递归查找solr_addr目录 # 所以这里需要删除example目录,防止tomcat崩溃 file: path: "{{ solr_dir }}/example" state: absent - name: set up data directory file: path: "{{ solr_dir }}/data" state: directory owner: tomcat7 group: tomcat7 - name: configure solrconfig.xml for new data directory # 指定dataDir的路径 lineinfile: dest: "{{ solr_dir }}/collection1/conf/solrconfig.xml" regexp: "^.*${solr.data.dir:{{ solr_dir }}/data}" state: present - name: set permissons for solr home file: path: "{{ solr_dir }}" recurse: yes owner: tomcat7 group: tomcat7 - name: add catalina configration for solr # 复制context xml文件,指定web应用目录 template: src: "templates/solr.xml.j2" dest: "/etc/tomcat7/Catalina/localhost/solr.xml" owner: root group: tomcat7 mode: 0644 notify: restart tomcat
模板文件
solr.xml.j2:
变量文件
vars.yml:
download_dir: /tmp solr_dir: /opt/solr
运行结果:
测试访问web页面
总结:
第四章下,到此为止,通过这两个实例,相信大家已经能够学到很多东西,也能够体会到了ansible的方便,下一章,将会详解playbook,欢迎大家一起学习交流。大家有什么问题的话,可以给我留言