zhu894532094 发表于 2018-7-30 07:52:14

Ansible 一步一步从入门到精通(四)下

---  
- 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 -sShttps://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
页: [1]
查看完整版本: Ansible 一步一步从入门到精通(四)下