After reading the basis of ansible, and then wrote three simple deployment of YML, respectively, installation Nginx, MySQL, tomcat+jdk
Put the source code here, has been tested to pass the
Tomcat:
/etc/ansible/roles/tomcat/├──defaults│└──main.yml├──files│├──apache-tomcat-7.0.52.tar.gz│├──apr-1.5.1.tar.bz 2│├──apr-util-1.5.4.tar.bz2│├──jdk-7u55-linux-x64.tar.gz│└──profile├──handlers│└──main.yml├──meta│└── Main.yml├──readme.md├──tasks│└──main.yml├──templates├──tests│├──inventory│└──test.yml└──vars└──main . yml
Main file Tomcat.yml:
Cat tomcat.yml----hosts:test roles:-Tomcat
Var/main.yml
Cat vars/main.yml---# vars file for/etc/ansible/roles/tomcatjdk:jdk-7u55-linux-x64.tar.gztomcat: Apache-tomcat-7.0.52.tar.gzapr:apr-1.5.1.tar.bz2apr_util:apr-util-1.5.4.tar.bz2soft_path:/opt/soft/
Tasks/main.yml
---# tasks file for /etc/ansible/roles/tomcat- name: yum install yum: name= "{{item}}" state=installed with_items: [' gcc ', ' gcc-c++ ', ' Make ', ' CMake ', ' Openssl-devel ']- name: mkdir soft path file: path={{soft_path}} state=directory- name: copy tomcat copy: src=files/{{tomcat}} dest={ {Soft_path}} - name: copy jdk copy: src=files/{{jdk}} dest={{soft_path}}- name: copy apr copy: src=files/{{apr}} dest={{soft_path}}- name: copy apr-util copy: src=files/{{apr_util}} dest={{soft_path}}- name: copy profile copy: src=files/profile dest=/etc backup=yes- name: tar tomcat unarchive: src={{soft_path}}{{tomcat}} dest=/opt copy=no- name: touch tomcat link file: src=/opt/apache-tomcat-7.0.52 dest=/opt/tomcat state=link- name: tar jdk unarchive: src={{soft_path}}{{jdk}} dest=/opt copy=no- name: touch jdk link file: src=/opt/jdk1.7.0_55 dest=/opt/java state=link- name: tar apr unarchive: src={{soft_path}}{{apr}} dest={{soft_path}} copy= No- name: install apr shell: chdir={{soft_path}}apr-1.5.1/ ./configure --prefix=/opt/apr && make && make install- name: tar apr_util unarchive: src={{soft_path}}{{apr_util}} dest={{soft_path}} copy=no- name: install apr_util shell: chdir={{soft_path}}apr-util-1.5.4/ ./ configure --prefix=/opt/apr-util --with-apr=/opt/apr && make && Make install- name: tar tomcat-native unarchive: src=/opt/tomcat/bin/tomcat-native.tar.gz dest=/opt/tomcat/bin/ copy=no- name: install tomcat-native shell: chdir=/ opt/tomcat/bin/tomcat-native-1.1.29-src/jni/native ./configure --prefix=/opt/apr --with-apr=/ opt/apr --with-java-home=/opt/java --with-ssl=yes && make && Make install notify: - reload profile
Handlers/main.yml
---# Handlers file For/etc/ansible/roles/tomcat-name:reload profile Shell:source/etc/profile
-------------------------------------------------------------------------------------------
Nginx:
/etc/ansible/roles/nginx/├──defaults│└──main.yml├──files│├──nginx-1.8.0.tar.gz│├──openssl-1.0.0.tar.gz│├─ ─pcre-8.37.tar.gz│└──zlib-1.2.8.tar.gz├──handlers│└──main.yml├──meta│└──main.yml├──readme.md├──tasks│└ ──main.yml├──templates├──tests│├──inventory│└──test.yml└──vars└──main.yml
Master File nginx.yml
----hosts:test roles:-Nginx
Vars/main.yml
---# vars file for/etc/ansible/roles/nginxnginx:nginx-1.8.0.tar.gz openssl:openssl-1.0.0.tar.gz Pcre:pcre-8.37.tar. GZ Zlib:zlib-1.2.8.tar.gzsoft_path:/opt/soft/
Tasks/main.yml
---# tasks file for /etc/ansible/roles/nginx- name: yum install yum: name={{item}} state=installed with_items: [' gcc ', ' gcc-c++ ', ' make ', ' cmake ']- name: copy nginx copy: src=files/{{nginx}} dest={{soft_path}}- name: copy openssl copy: src=files/{{openssl}} dest={{soft_path}}- name: copy zlib copy: src=files/{{zlib}} dest={{soft_path}}- name: copy pcre copy: src=files/{{pcre}} dest={{soft_path}}- name: tar pcre unarchive: src={{soft_path}}{{pcre}} dest={{soft_path}} copy=no- name: install pcre shell: chdir={{soft_path}}pcre-8.37 ./configure && make && make install- name: tar openssl unarchive: src={{ Soft_path}}{{openssl}} desT={{soft_path}} copy=no- name: install openssl shell: chdir={{soft_path}} openssl-1.0.0 ./config && make && make install- name: tar zlib unarchive: src={{soft_path}}{{zlib}} dest={{soft_path}} copy=no- name: install zlib shell: chdir={{soft_path}}zlib-1.2.8 ./configure & & make && make install- name: groupadd www group: name=www - name: useradd www user: name=www group=www- name: tar nginx unarchive: src={{soft_path}}{{nginx}} dest={{soft_path}} copy=no- name: install nginx shell: chdir={{soft_path}}nginx-1.8.0 ./ configure --prefix=/opt/nginx --user=www --group=www --with-http_stub_status_module - -with-http_ssl_module --with-http_gzip_static_module --with-openssl=/opt/soft/openssl-1.0.0 --with-pcre=/opt/soft/ pcre-8.37 --with-zlib=/opt/soft/zlib-1.2.8 && make && make Install
-------------------------------------------------------------------------------------------
Mysql:
/etc/ansible/roles/mysql/├──defaults│└──main.yml├──files│├──my.cnf│├──mysql│└──mysql-5.6.13.tar.gz├──ha Ndlers│└──main.yml├──meta│└──main.yml├──readme.md├──tasks│└──main.yml├──templates├──tests│├──inventor Y│└──test.yml└──vars└──main.yml
Master File mysql.yml
----hosts:test roles:-MySQL
Vars/main.yml
---# vars file for/etc/ansible/roles/mysqlmysql:mysql-5.6.13.tar.gzsoft_path:/opt/soft/
Tasks/main.yml
---# tasks file for /etc/ansible/roles/mysql- name: yum install Yum: name= "{{item}}" state=installed with_items: [' gcc ', ' gcc-c++ ', ' make ', ' cmake ', ' Ncurses-devel ', ' libaio ', ' Bison '] - name: copy mysql copy: src=files/ {{MYSQL}} DEST={{SOFT_PATH}}- NAME: COPY MY.CNF  COPY: SRC=FILES/MY.CNF dest=/etc- name: tar mysql unarchive: src={{soft_path}}{{mysql}} dest={{soft_path}} copy=no- name: groupadd group: name=mysql- name: useradd user: name=mysql group=mysql - name: install mysql shell: chdir={{soft_path}}mysql-5.6.13 cmake . -dcmake_install_prefix=/opt/mysql -dwith_innobase_storage_engine=1 -dmysql_user=mysql -ddefault_charset=utf8 -ddefault_ Collation=utf8_general_ci &aMp;& make && make install- name: mkdir log_path file : path=/opt/mysql/data/log state=directory- name: chown mysql1 file: path=/opt/mysql/ owner=mysql group=mysql recurse=yes- name: mysql_install_db shell: chdir=/opt/mysql/scripts/ ./mysql_install_db --user=mysql --basedir=/opt/ Mysql/ --datadir=/opt/mysql/data- name: chown mysql2 file: path=/opt/mysql / owner=root recurse=yes- name: chown data file: path=/opt/mysql/data / owner=mysql recurse=yes- name: copy mysql_init copy: src=files/ mysql dest=/etc/init.d/- name: chmod mysql_init file: path=/etc/init.d/ mysql mode=0755
This article from "Eight Miles" blog, declined reprint!
Ansible Small Test Sledgehammer