记录一个ansible高级用法与shell结合

15 篇文章 2 订阅
10 篇文章 1 订阅

未经本人同意不得转载

目录

一.ansible安装(ansible的配置与roles运用)

1.ansible的概述

2.安装python

3.ansible运用前准备

二.修改roles

1.初步修改apache-roles(一个roles可写多个剧本)

2.二次修改定义变量(开关)

3.三次修改(别名调用ansible)

4.四次修改添加删除剧本

5.五次修改(脚本调用创建与删除)

6.六次修改(脚本调用函数)

7.测试

三.添加nginx四层代理入集群

1.创建nginx角色剧本

2.创建四层代理文件

3.修改hosts

4.书写ansible-playbook

5.添加脚本安装与删除

6.调用集成脚本(不变)


一.ansible安装(ansible的配置与roles运用)

注:前面两步检查一下是否安装即可(可跳过);在2.7与3.5版本的python上默认安装pip

1.ansible的概述

ansible是一个非常简单的自动化部署项目,由python编写并且开源。用于提供自动化云配置、配置文件管理、应用部署、服务编排和很多其他的IT自动化需求。

ansible实现的自动化部署是多层次的,通过描述系统之间的逻辑关系来构建业务所需要的基础架构模型,而不仅仅用于管理一个单独的系统;也就是说ansible不仅仅能部署一个或多个独立的服务,它还能对这些服务做关联、对部署顺序做编排等,一个完美的ansible部署项目应该是层次分明、顺序有秩的。

另外,ansible是Serverless和Agentless项目,在部署工具准备阶段基本上是零成本,而且ansible使用YAML写playbooks,这使playbook看起来通俗易懂,一目了然。

ansible这个后起之秀在开源社区上也是非常火爆的,可以说是部署工具届的网红一枚。现在很多很火的开源项目都在使用ansible作为部署工具,例如我熟悉的openstack-ansible、openshift-ansible等等

2.安装python

1)寻找对应版本的安装包,官网的ftp地址如下

Index of /ftp/python/

# 这边就使用3.7.6版本,版本太高不是很好,很多第三方的库都根本上;感觉3.6是比较好的版本;
​
# 下载pthon安装包
~]# wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tgz

2)创建安装目录 看个人习惯,这边放在/usr/local下面

~]# mkdir -p /usr/local/python3

3)解压

~]# tar -zxvf Python-3.7.6.tgz 

4)编译安装

# 先需要gcc环境和zlib库为了方向键等不出现乱码还需要 readline-devel 包
yum -y install gcc zlib* readline-devel
​
# 进入解压好的目录并编译安装
~]# cd Python-3.7.6
~]# ./configure --prefix=/usr/local/python3
~]# make && make install

5)建立软链接

~]# ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3
~]# ln -s /usr/local/python3/bin/pip3.7 /usr/bin/pip3

6)测试安装 查看版本

~]# python3 --version
Python 3.7.6
~]# python3
Python 3.7.6 (default, Feb 15 2020, 19:40:45) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello word')
hello word

7)模块包setup-tools与pip安装

什么是setuptools

setuptools是Python distutils增强版的集合,它可以帮助我们更简单的创建和分发Python包,尤其是拥有依赖关系的。用户在使用setuptools创建的包时,并不需要已安装setuptools,只要一个启动模块即可。

功能亮点:

利用EasyInstall自动查找、下载、安装、升级依赖包 创建Python Eggs 包含包目录内的数据文件 自动包含包目录内的所有的包,而不用在setup.py中列举 自动包含包内和发布有关的所有相关文件,而不是创建一个MANIFEST.in文件 自动生成经过包装的脚本或Windows执行文件 支持Pyrex,即在可以setup.py中列出.pyx文件,而最终用户无需安装Pyrex 支持上传到PyPI 可以部署开发模式,使项目在sys.path中 用新命令或setup()参数扩展distutils,为多个项目发布/重用扩展 在项目setup()中简单声明entry points,创建可以自动发现扩展的应用和框架

#网上找的安装包setuptools
~]# wget https://pypi.python.org/packages/45/29/8814bf414e7cd1031e1a3c8a4169218376e284ea2553cc0822a6ea1c2d78/setuptools-36.6.0.zip#md5=74663b15117d9a2cc5295d76011e6fd1
​
#解压
~]# unzip setuptools-36.6.0.zip 
​
#进入解压的文件并编译安装
~]# cd setuptools-36.6.0
~]# python3.5 setup.py build 
~]# python setup.py install
​
# pip下载
~]# wget --no-check-certificate  https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb
​
# 解压文件  
~]# tar zxvf pip-8.0.2.tar.gz
​
# 进入该目录 
~]# cd pip-8.0.2 
​
#同样执行:
~]# python setup.py build
~]# python setup.py install
#没有提示错误,那么就安装成功了。
​
# 安装好了之后会在我们的python目录中成成该执行文件的。
Adding pip 8.0.2 to easy-install.pth file
Installing pip3.5 script to /usr/local/python/bin
Installing pip3 script to /usr/local/python/bin
Installing pip script to /usr/local/python/bin
​
# 这个就是安装是的提示,给我们说的很清楚,说将pip3安装到了/usr/local/python/bin目录中
​
# 对于我此时的目录就是:
  /usr/local/python/bin
~]# ln -s /usr/bin/pip3 /usr/local/python/bin/pip3.5

3.ansible运用前准备

#将防火墙状态enforcing模式修改为permissive变成宽容模式 
~]# setenforce 0
​
# 配置免密登陆
~]# ssh-keygen -t rsa  生成公钥
~]# ssh-copy-id root@ip地址  
~]# ssh-copy-id root@192.168.42.110

4.安装ansible

官方软件下载: Index of /ansible

CentOS6自带ansible版本为2.6.20

CentOS7自带ansible版本为2.9.21

CentOS8自带ansible版本为2.9.21-1

# 安装ansible
~]# yum -y install ansible
​
# 创建ansible目录
~]# mkdir ansible
​
# 拷贝配置文件
~]# cd ansible
~]# cp /etc/ansible/ansible.cfg ansible.cfg
​
# 书写配置文件
~]# vim ansible.cfg
[defaults]
inventory      = ~/ansible/hosts  #指定主机清单文件
​
# 书写主机清单
~]# vim hosts
[http]
172.17.0.114
172.17.0.142
172.17.0.98
​
#测试
~]# ansible all -m ping
172.17.0.142 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
172.17.0.114 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
172.17.0.98 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

5.创建角色运用(用ansible之前必须进入ansible)

# 创建角色目录
~]# mkdir roles
​
# 修改ansible配置文件
~]# vim ansible.cfg
inventory = ~/ansible/inventory
remote_user     = root                       //连接受管机的远程用户   
roles_path    = roles                       //指定默认的角色目录
host_key_checking = false                  //当其中有执行错误的命令时也继续执行
​
[privilege_escalation]                      //设置用户 sudo 提权
become=True                               //需要提权 
become_method=sudo                        //提权方式为 sudo   
become_user=root                         //提权为 root  
become_ask_pass=False                   //无需验证密码
​
​
# 拉取角色
~]# ansible-galaxy init roles/install
​
## 安装apache
# 在角色中创建apache的剧本
~]# cd /root/ansible/roles/install/tasks/
~]# vim main.yaml
---
- name: install httpd
  yum:
     name: httpd
     state: present
- name: create index.html
  copy:
     content: "{{ansible_hostname}}"   #等同于剧本先读取变量成值
     dest: /var/www/html/index.html
- name: set firewalld
  firewalld:
     service: http
     state: enables
     permanent: true
     immediate: true
  ignore_errors: yes #由于某些原因没有装firewall,直接跳过错误
- name: start httpd
  service:
     name: httpd
     state: started
     enabled: true
                  
# 创建playbook调用角色
~]# cd /root/ansible
~]# vim web.yml
---
- hosts: all
  roles:
    - install # 角色名与上面对应
    
# 运用ansible-playbook    
 ~]# ansible-playbook web.yml
# 报错,看节点端口是否被占用
# roles目录介绍
~]# tree /root/ansible/roles
/root/ansible/roles/
`-- install
    |-- defaults
    |   `-- main.yml
    |-- files
    |-- handlers
    |   `-- main.yml
    |-- meta
    |   `-- main.yml
    |-- README.md
    |-- tasks
    |   `-- main.yml
    |-- templates
    |-- tests
    |   |-- inventory
    |   `-- test.yml
    `-- vars
        `-- main.yml
​
defualts/main.yml    :定义变量的缺省值,优先级较低
vars/main.yml        :定义变量,优先级高
files目录             :存储静态文件的目录,如tar包、音乐、视频等
templates目录         :存放动态数据文件的地方(文件中包含了变量的模板文件)
meta/main.yml        :写作者、版本等描述信息
README.md            :整个角色(role)的描述信息
handlers/main.yml    :定义handlers
tasks/main.yml       :定义任务的地方

二.修改roles

1.初步修改apache-roles(一个roles可写多个剧本)

# 在tasks/main.yml文件中引用剧本文件,而直接做剧本
~]# cd roles/install/tasks/
~]# vim main.yml 
---
- include: http.yml
​
# 书写要调用的剧本
~]# vim http.yml
- name: install httpd
  yum:
     name: httpd
     state: present
- name: create index.html
  copy:
     content: "{{ansible_hostname}}"   #等同于剧本先读取变量成值
     dest: /var/www/html/index.html
- name: set firewalld
  firewalld:
     service: http
     state: enables
     permanent: true
     immediate: true
  ignore_errors: true
- name: start httpd
  service:
     name: httpd
     state: started
     enabled: true
#测试,调用角色剧本不用改
~]# cd /root/ansible

# 创建playbook
~]# cd /root/ansible && vim web.yml
---
- hosts: all
  gather_facts: True
  environment:
    PATH: "{{ ansible_env.PATH }}:/usr/local/bin"
  become: yes
  roles:
    - install

~]# ansible-playbook web.yml
# 基本上,使用 include 语句引用 task 文件的方法,可允许你将一个配置策略分解到更小的文件中。使用 include 语句引用 tasks 是将 tasks 从其他文件拉取过来。因为 handlers 也是 tasks,所以你也可以使用 include 语句去引用 handlers 文件。handlers 文件来自 ‘handlers:’ section。

2.二次修改定义变量(开关)

~]# cd /root/ansible/roles/install/tasks
~]# vim http.yml
#书写变量
# tasks file for roles/install
# set facts
- name: set deploy_nginx facts
  set_fact: deploy_http = "{{ deploy_http }}"

- name: install httpd
  yum:
     name: httpd
     state: present
  when: deploy_http == "true" and inventory_hostname in groups['http']    #最后可以写为['nginx'][0]代表nginx主机第一台[1]则是第二台   
- name: create index.html
  copy:
     content: "{{ansible_hostname}}"   #等同于剧本先读取变量成值
     dest: /var/www/html/index.html
  when: deploy_http == "true" and inventory_hostname in groups['http']  
- name: set firewalld
  firewalld:
     service: http
     state: enables
     permanent: true
     immediate: true
  when: deploy_http == "true" and inventory_hostname in groups['http']   
- name: start httpd
  service:
     name: httpd
     state: started
     enabled: true
   when: deploy_http == "true" and inventory_hostname in groups['http']  

#修改hosts定义变量
~]# cd /root/ansible
~]# vim hosts
[all:vars]
deploy_http="true"

[http]
172.17.0.114
172.17.0.142
172.17.0.98

#测试,main.yml与playbook不做修改
~]# ansible-playbook web.yml

3.三次修改(别名调用ansible)

~]# cd /root/ansible/roles/install/tasks
~]# vim http.yml
# tasks file for roles/install
# set facts
- name: set deploy_nginx facts
  set_fact: deploy_http = "{{ deploy_http }}"
  tags: install_http  #定义别名

- name: install httpd
  yum:
     name: httpd
     state: present
  when: deploy_http == "true" and inventory_hostname in groups['nginx']
  tags: install_http   #定义别名
- name: create index.html
  copy:
     content: "{{ansible_hostname}}"
     dest: /var/www/html/index.html
  when: deploy_http == "true" and inventory_hostname in groups['nginx']
  tags: install_http   #定义别名
- name: start httpd
  service: 
     name: httpd
     state: started
     enabled: true
  when: deploy_http == "true" and inventory_hostname in groups['nginx']
  tags: install_http   #定义别名

#修改hosts
~]# cd /root/ansible
~]# vim ansible.cfg 
[defaults]
inventory      = ~/ansible/hosts
remote_user     = root                    
roles_path    = roles                   

[privilege_escalation]                  
become=True                             
become_method=sudo                         
become_user=root                          
become_ask_pass=False                   
forks = 10    #ssh并发数量(默认是5)

#测试,main.yml,playbook不做修改
~]# ansible-playbook -f 10 -i /root/ansible/hosts --tags install_http /root/ansible/web.yml --extra-vars "hosts=${hosts}"
#--extra-vars "hosts=${hosts}" 暂不知道外部变量是什么,这里不写

-f FORKS, --forks=FORKS
             #specify number of parallel processes to use(default=5)
             #并行任务数。FORKS被指定为一个整数,默认是5
-i INVENTORY, --inventory-file=INVENTORY
             #specify inventory host path (default=/etc/ansible/hosts) or comma separated host list.
             #指定要读取的Inventory文件    
-tags           
             #available tags
             #指定可用的tags 
-e EXTRA_VARS, --extra-vars=EXTRA_VARS
             #set additional variables as key=value or YAML/JSON
             #在Playbook中引入外部参数变量

4.四次修改添加删除剧本

# 书写剧本
~]# cd /root/ansible/roles/install/tasks
~]# vim http.yml
---
# tasks file for roles/install
# set facts
- name: set deploy_nginx facts
  set_fact: deploy_http = "{{ deploy_http }}"
  tags: install_http

- name: install httpd
  yum:
     name: httpd
     state: present
  when: deploy_http == "true" and inventory_hostname in groups['apache']
  tags: install_http
- name: create index.html
  copy:
     content: "{{ansible_hostname}}"
     dest: /var/www/html/index.html
  when: deploy_http == "true" and inventory_hostname in groups['apache']
  tags: install_http
- name: set firewalld
  firewalld:
     service: http
     state: enables
     permanent: true
     immediate: true
  when: deploy_http == "true" and inventory_hostname in groups['apache']
  tags: install_http
  ignore_errors: true
- name: start httpd
  service:
     name: httpd
     state: started
     enabled: true
  when: deploy_http == "true" and inventory_hostname in groups['apache']
  tags: install_http

# 删除剧本
- name: stop http
  service:
     name: httpd
     state: stopped
     enabled: false
  when: deploy_http == "true" and inventory_hostname in groups['apache']
  tags: remove_http
- name: remove http
  yum:
    name: httpd
    state: absent
  when: deploy_http == "true" and inventory_hostname in groups['apache']
  tags: remove_http
- name: rm directory
  file:
   path: /var/www/html/index.html
   state: absent
  when: deploy_http == "true" and inventory_hostname in groups['apache']
  tags: remove_http

#测试,main.yml,playbook不做修改
~]# ansible-playbook -f 10 -i /root/ansible/hosts --tags remove_http /root/ansible/web.yml --extra-vars "hosts=${hosts}"

5.五次修改(脚本调用创建与删除)

1)书写playbook

~]# cd /root/ansible && vim web.yml
---
- hosts: all
  gather_facts: True
  environment:
    PATH: "{{ ansible_env.PATH }}:/usr/local/bin"
  become: yes
  roles:
    - install

2)掉用创建

# 创建目录
~]# cd /root/ansible
~]# mkdir install-sh && cd install-sh
~]# vim install.sh
#!/bin/bash
set -e 

BASE_DIR=$(cd `dirname $0` && pwd)
cd $BASE_DIR

CALL_FUN="all_func"
hosts="all"
help(){
    echo "show usage"
    echo "install_http:deploy install http"
}
while getopts ":f:h:" opt
do
   case $opt in 
   f)
     CALL_FUN="${OPTARG}";;
   h)
     hosts="${OPTARG}";;
   ?)
     echo "unkown args! just suport -f[call function] and -h[ansible hosts group] arg!!!"
     exit 0;;
   esac
done

http(){
    echo "install http"
ansible-playbook -f 10 -i /root/ansible/hosts --tags install_http /root/ansible/web.yml --extra-vars "hosts=${hosts}"
}

all_func(){
          http
}

main(){
      $CALL_FUN || help
}
main

#测试
~]# chmod +x http.sh
~]# ./http.sh

3)调用删除

~]# cd /root/ansible/install-sh
~]# vim remove.sh
#!/bin/bash
set -e 

BASE_DIR=$(cd `dirname $0` && pwd)
cd $BASE_DIR

CALL_FUN="all_func"
hosts="all"
help(){
    echo "show usage"
    echo "install_http:deploy install http"
}
while getopts ":f:h:" opt
do
   case $opt in 
   f)
     CALL_FUN="${OPTARG}";;
   h)
     hosts="${OPTARG}";;
   ?)
     echo "unkown args! just suport -f[call function] and -h[ansible hosts group] arg!!!"
     exit 0;;
   esac
done

http(){
    echo "install http"
ansible-playbook -f 10 -i /root/ansible/hosts --tags remove_http /root/ansible/web.yml --extra-vars "hosts=${hosts}"
}

all_func(){
          http
}

main(){
      $CALL_FUN || help
}
main

6.六次修改(脚本调用函数)

~]# cd /root/ansible
~]# vim pot-cmd.sh 
#!/bin/bash
# Author: yhchen
set -e

BASE_DIR=$(cd `dirname $0` && pwd)
cd $BASE_DIR

EXEC_SCRIPT=""
CALL_FUN="all_func"
hosts="all"

help(){
  echo "show usage:"
  echo "you can exec script list: "
  echo `ls /root/ansible/install-sh`
  exit 0
}

while getopts ":s:f:h:" opt
do
  case $opt in
    s)
    EXEC_SCRIPT="${OPTARG}"
    ;;
    f)
    CALL_FUN="${OPTARG}"
    ;;
    h)
    hosts="${OPTARG}"
    ;;
    ?)
    echo "unkown args! just suport -s[mgr-scripts's script] -f[call function] and -h[ansible hosts group] arg!!!"
    exit 0;;
  esac
done

cmd(){
   /root/ansible/install-sh/${EXEC_SCRIPT} -f ${CALL_FUN} -h ${hosts}
}

main(){
  if [ "x${EXEC_SCRIPT}" == "x" ]; then
    help
  else
    cmd
  fi
}
main

7.测试

~]# ./pot-cmd.sh -f xxx
show usage:
you can exec script list: 
install.sh remove.sh

~]# ./pot-cmd.sh -s install.sh -f xxx
/root/ansible/install-sh/install.sh: line 36: xxx: command not found
show usage
install_http:deploy install http

~]# ./pot-cmd.sh -s install.sh -h install_http

三.添加nginx四层代理入集群

1.创建nginx角色剧本

注:模块可查帮助ansible-doc [模块名]

~]# cd /root/nginx/roles/install/tasks/
~]# vim nginx.yml
---
# set facts
- name: set deploy_nginx facts
  set_fact: deploy_nginx = "{{ deploy_nginx }}"
  tags: install_nginx

# create save nginx loanginxalance dir
- name: create nginx loanginxalance dir
  file: 
    path: /root/nginx
    state: directory 
  when: deploy_nginx == "true" and inventory_hostname in groups['nginx']
  tags: install_nginx

# install nginx rely on
- name: install pcre-devel zlib-devel openssl-devel gcc
  yum:
    name: "{{ item }}"
  loop:
        - pcre-devel
        - zlib-devel
        - openssl-devel
        - gcc
  when: deploy_nginx == "true" and inventory_hostname in groups['nginx'][0]
  tags: install_nginx
# copy nginx  install pkg to nginx node
- name: copy nginx install pkg to nginx node
  copy:
    src: "{{ dpl_dir }}-tgz/{{ nginx_version }}.tar.gz"
    dest: /root/nginx
  when: deploy_nginx == "true" and inventory_hostname in groups['nginx']
  tags: install_nginx
# unzip nginx install pkg
- name: unzip nginx install pkg
  unarchive:
    creates: /root/nginx/nginx-1.20.1
    copy: no
    src: /root/nginx/nginx-1.20.1.tar.gz
    dest: /root/nginx 
  when: deploy_nginx == "true" and inventory_hostname in groups['nginx']  
  tags: install_nginx

# deploy install nginx
- name: deploy nginx
  shell: if [ `ls /root/nginx/nginx | wc -l` -eq 1]; then echo "install nginx"; else cd /root/nginx/nginx-1.20.1 && ./configure --prefix=/root/nginx/nginx --user=nginx --group=nginx --with-http_ssl_module --with-stream && make && make install;fi
  when: inventory_hostname in groups['nginx']
  tags: install_nginx

# useradd nginx
- name: useradd nginx
  shell: if [ `id nginx | wc -l` -eq 1 ]; then echo "true"; else useradd -s /sbin/nologin -M nginx;fi 
  when: inventory_hostname in groups['nginx']
  tags: install_nginx

# copy deploy nginx script to nginx node
- name: deploy nginx config
  template:
    src: templates/nginx/nginx.conf.j2 
    dest: /root/nginx/nginx/conf/nginx.conf
  when: deploy_nginx == "true" and inventory_hostname in groups['nginx']
  tags: install_nginx

# deploy nginx
- name: deploy nginx
  shell: /root/nginx/nginx/sbin/nginx
  when: inventory_hostname in groups['nginx']
  tags: install_nginx

# remove nginx
- name: deploy remove nginx
  shell: if [ `ss -nulpt |grep nginx |wc -l` -ge 1 ];then /root/nginx/nginx/sbin/nginx -s stop  && rm -rf /root/nginx/ && rm -rf /data/nginx/; else echo "no nginx";fi
  when: inventory_hostname in groups['nginx'] and deploy_nginx == "true"
  tags: remove_nginx

# remove nginx rely on
- name: install pcre-devel zlib-devel openssl-devel gcc
  yum:
     name: "{{ item }}"
     state: absent 
  loop:
        - pcre-devel
        - zlib-devel
        - openssl-devel
        - gcc
  when: deploy_nginx == "true" and inventory_hostname in groups['nginx']
  tags: remove_nginx

2.创建四层代理文件

~]# /root/ansible/roles/install/templates
~]# mkdir nginx
~]# vim nginx/nginx.conf.j2
worker_processes  4;

events {
    worker_connections  1024;
}
stream {
    upstream tapd_http {
        server {{ groups['stream'][1] }}:80 max_fails=3 fail_timeout=30s;
        server {{ groups['stream'][2] }}:80 max_fails=3 fail_timeout=30s;
    }

    upstream tapd_https {
       server {{ groups['stream'][1] }}:443 max_fails=3 fail_timeout=30s;
       server {{ groups['stream'][2] }}:443 max_fails=3 fail_timeout=30s;
    }

    upstream apiserver_lb {
        server {{ groups['stream'][0] }}:6443 max_fails=3 fail_timeout=30s;
        server {{ groups['stream'][1] }}:6443 max_fails=3 fail_timeout=30s;
        server {{ groups['stream'][2] }}:6443 max_fails=3 fail_timeout=30s;
    }

     upstream tke_platform_api {
        server {{ groups['stream'][0] }}:31138 max_fails=3 fail_timeout=30s;
        server {{ groups['stream'][1] }}:31138 max_fails=3 fail_timeout=30s;
        server {{ groups['stream'][2] }}:31138 max_fails=3 fail_timeout=30s;
    }
    server {
        listen 80;
        proxy_connect_timeout 5s;
        proxy_pass tapd_http;
    }

    server {
       listen 443;
       proxy_connect_timeout 5s;
       proxy_pass tapd_https;
    }

    server {
       listen 6443;
       proxy_connect_timeout 5s;
       proxy_pass apiserver_lb;
    }

    server {
       listen 31138;
       proxy_connect_timeout 5s;
       proxy_pass tke_platform_api;
    }

}

3.修改hosts

[all:vars]
dpl_dir=/root/ansible/install
nginx_version="nginx-1.20.1"

deploy_http="true"
deploy_nginx="true"

[apache]
172.17.0.114
172.17.0.142
172.17.0.98

[nginx]
172.17.0.142

[stream]
172.17.0.114
172.17.0.142
172.17.0.98

4.书写ansible-playbook

~]# cd /root/ansible && vim web.yml
---
- hosts: all
  gather_facts: True    # 当执行错误时,继续执行
  environment:
    PATH: "{{ ansible_env.PATH }}:/usr/local/bin"
  become: yes
  roles:
    - install

5.添加脚本安装与删除

1)安装脚本

~]# cd /root/ansible/install-sh
~]# vim install.sh
#!/bin/bash
set -e 

BASE_DIR=$(cd `dirname $0` && pwd)
cd $BASE_DIR

CALL_FUN="all_func"
hosts="all"
help(){
    echo "show usage"
    echo "install_http:deploy install http"
    echo "nginx_lb: deploy nginx"
}
while getopts ":f:h:" opt
do
   case $opt in 
   f)
     CALL_FUN="${OPTARG}";;
   h)
     hosts="${OPTARG}";;
   ?)
     echo "unkown args! just suport -f[call function] and -h[ansible hosts group] arg!!!"
     exit 0;;
   esac
done

http(){
    echo "install http"
ansible-playbook -f 10 -i /root/ansible/hosts --tags install_http /root/ansible/web.yml --extra-vars "hosts=${hosts}"
}

nginx_lb(){
  echo "###### deploy nginx start ######"
  #nginx init
  ansible-playbook -f 10 -i /root/ansible/hosts --tags install_nginx /root/ansible/web.yml --extra-vars "hosts=${hosts}" 
  echo "###### deploy nginx end ######"
}

all_func(){
          http
          nginx_lb
}

main(){
      $CALL_FUN || help
}
main

2)删除脚本

~]# cd /root/ansible/install-sh
~]# vim remove.sh
#!/bin/bash
set -e 

BASE_DIR=$(cd `dirname $0` && pwd)
cd $BASE_DIR

CALL_FUN="all_func"
hosts="all"
help(){
    echo "show usage"
    echo "remove_http:deploy remove http"
    echo "remove_nginx: remove nginx lb"
}
while getopts ":f:h:" opt
do
   case $opt in 
   f)
     CALL_FUN="${OPTARG}";;
   h)
     hosts="${OPTARG}";;
   ?)
     echo "unkown args! just suport -f[call function] and -h[ansible hosts group] arg!!!"
     exit 0;;
   esac
done

http(){
    echo "install http"
ansible-playbook -f 10 -i /root/ansible/hosts --tags remove_http /root/ansible/web.yml --extra-vars "hosts=${hosts}"
}

remove_nginx(){
  echo "###### remove nginx start ######"
  # remove nginx
  ansible-playbook -f 10 -i /root/ansible/hosts --tags remove_nginx /root/ansible/web.yml --extra-vars "hosts=${hosts}"
  echo "###### remove nginx end ######"
}

all_func(){
          http
          remove_nginx
}

main(){
      $CALL_FUN || help
}
main

6.调用集成脚本(不变)

~]# cd /root/ansible
~]# vim pot-cmd.sh
#!/bin/bash
# Author: yhchen
set -e

BASE_DIR=$(cd `dirname $0` && pwd)
cd $BASE_DIR

EXEC_SCRIPT=""
CALL_FUN="all_func"
hosts="all"

help(){
  echo "show usage:"
  echo "you can exec script list: "
  echo `ls /root/ansible/install-sh`
  exit 0
}

while getopts ":s:f:h:" opt
do
  case $opt in
    s)
    EXEC_SCRIPT="${OPTARG}"
    ;;
    f)
    CALL_FUN="${OPTARG}"
    ;;
    h)
    hosts="${OPTARG}"
    ;;
    ?)
    echo "unkown args! just suport -s[mgr-scripts's script] -f[call function] and -h[ansible hosts group] arg!!!"
    exit 0;;
  esac
done

cmd(){
   /root/ansible/install-sh/${EXEC_SCRIPT} -f ${CALL_FUN} -h ${hosts}
}

main(){
  if [ "x${EXEC_SCRIPT}" == "x" ]; then
    help
  else
    cmd
  fi
}
main

4)测试用法与“‘7”’类似

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值