ansible小试牛刀

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ansible小试牛刀相关的知识,希望对你有一定的参考价值。

看完ansible的基础,然后写了三个简单部署的YML,分别是安装nginxmysql、tomcat+jdk

这里贴上源码吧,已经测试通过了


tomcat:

/etc/ansible/roles/tomcat/
├── defaults
│   └── main.yml
├── files
│   ├── apache-tomcat-7.0.52.tar.gz
│   ├── apr-1.5.1.tar.bz2
│   ├── 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

主文件 tomcat.yml:

cat tomcat.yml 
---
- hosts: test
  
  roles:
    - tomcat

var/main.yml

cat vars/main.yml 
---
# vars file for /etc/ansible/roles/tomcat

jdk: jdk-7u55-linux-x64.tar.gz
tomcat: apache-tomcat-7.0.52.tar.gz
apr: apr-1.5.1.tar.bz2
apr_util: apr-util-1.5.4.tar.bz2
soft_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

主文件nginx.yml

---

- hosts: test

  roles:
    - nginx

vars/main.yml

---
# vars file for /etc/ansible/roles/nginx

nginx: 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.gz
soft_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
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

主文件mysql.yml

---

- hosts: test

  roles:
    - mysql

vars/main.yml

---
# vars file for /etc/ansible/roles/mysql

mysql: mysql-5.6.13.tar.gz
soft_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 && 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


本文出自 “八英里” 博客,谢绝转载!

以上是关于ansible小试牛刀的主要内容,如果未能解决你的问题,请参考以下文章

saltstack小试牛刀

小试牛刀之python实现批量获取主机相关数据

前端试题-小试牛刀

ansible plugins简介

在ansible模板中使用动态组名称

用ansible 完成一次性的工作(ad-Hoc)工作