记录一个ansible高级用法与shell结合
未经本人同意不得转载
目录
一.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 httpdyum:name: httpdstate: present - name: create index.htmlcopy:content: "{{ansible_hostname}}" #等同于剧本先读取变量成值dest: /var/www/html/index.html - name: set firewalldfirewalld:service: httpstate: enablespermanent: trueimmediate: trueignore_errors: yes #由于某些原因没有装firewall,直接跳过错误 - name: start httpdservice:name: httpdstate: startedenabled: true# 创建playbook调用角色 ~]# cd /root/ansible ~]# vim web.yml --- - hosts: allroles:- 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 httpdyum:name: httpdstate: present - name: create index.htmlcopy:content: "{{ansible_hostname}}" #等同于剧本先读取变量成值dest: /var/www/html/index.html - name: set firewalldfirewalld:service: httpstate: enablespermanent: trueimmediate: trueignore_errors: true - name: start httpdservice:name: httpdstate: startedenabled: true #测试,调用角色剧本不用改 ~]# cd /root/ansible# 创建playbook ~]# cd /root/ansible && vim web.yml --- - hosts: allgather_facts: Trueenvironment:PATH: "{{ ansible_env.PATH }}:/usr/local/bin"become: yesroles:- 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 factsset_fact: deploy_http = "{{ deploy_http }}"- name: install httpdyum:name: httpdstate: presentwhen: deploy_http == "true" and inventory_hostname in groups['http'] #最后可以写为['nginx'][0]代表nginx主机第一台[1]则是第二台 - name: create index.htmlcopy:content: "{{ansible_hostname}}" #等同于剧本先读取变量成值dest: /var/www/html/index.htmlwhen: deploy_http == "true" and inventory_hostname in groups['http'] - name: set firewalldfirewalld:service: httpstate: enablespermanent: trueimmediate: truewhen: deploy_http == "true" and inventory_hostname in groups['http'] - name: start httpdservice:name: httpdstate: startedenabled: truewhen: 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 factsset_fact: deploy_http = "{{ deploy_http }}"tags: install_http #定义别名- name: install httpdyum:name: httpdstate: presentwhen: deploy_http == "true" and inventory_hostname in groups['nginx']tags: install_http #定义别名 - name: create index.htmlcopy:content: "{{ansible_hostname}}"dest: /var/www/html/index.htmlwhen: deploy_http == "true" and inventory_hostname in groups['nginx']tags: install_http #定义别名 - name: start httpdservice: name: httpdstate: startedenabled: truewhen: 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 factsset_fact: deploy_http = "{{ deploy_http }}"tags: install_http- name: install httpdyum:name: httpdstate: presentwhen: deploy_http == "true" and inventory_hostname in groups['apache']tags: install_http - name: create index.htmlcopy:content: "{{ansible_hostname}}"dest: /var/www/html/index.htmlwhen: deploy_http == "true" and inventory_hostname in groups['apache']tags: install_http - name: set firewalldfirewalld:service: httpstate: enablespermanent: trueimmediate: truewhen: deploy_http == "true" and inventory_hostname in groups['apache']tags: install_httpignore_errors: true - name: start httpdservice:name: httpdstate: startedenabled: truewhen: deploy_http == "true" and inventory_hostname in groups['apache']tags: install_http# 删除剧本 - name: stop httpservice:name: httpdstate: stoppedenabled: falsewhen: deploy_http == "true" and inventory_hostname in groups['apache']tags: remove_http - name: remove httpyum:name: httpdstate: absentwhen: deploy_http == "true" and inventory_hostname in groups['apache']tags: remove_http - name: rm directoryfile:path: /var/www/html/index.htmlstate: absentwhen: 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: allgather_facts: Trueenvironment:PATH: "{{ ansible_env.PATH }}:/usr/local/bin"become: yesroles:- 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_DIRCALL_FUN="all_func" hosts="all" help(){echo "show usage"echo "install_http:deploy install http" } while getopts ":f:h:" opt docase $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 donehttp(){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_DIRCALL_FUN="all_func" hosts="all" help(){echo "show usage"echo "install_http:deploy install http" } while getopts ":f:h:" opt docase $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 donehttp(){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 -eBASE_DIR=$(cd `dirname $0` && pwd) cd $BASE_DIREXEC_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 docase $opt ins)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 donecmd(){/root/ansible/install-sh/${EXEC_SCRIPT} -f ${CALL_FUN} -h ${hosts} }main(){if [ "x${EXEC_SCRIPT}" == "x" ]; thenhelpelsecmdfi } 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 factsset_fact: deploy_nginx = "{{ deploy_nginx }}"tags: install_nginx# create save nginx loanginxalance dir - name: create nginx loanginxalance dirfile: path: /root/nginxstate: 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 gccyum:name: "{{ item }}"loop:- pcre-devel- zlib-devel- openssl-devel- gccwhen: 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 nodecopy:src: "{{ dpl_dir }}-tgz/{{ nginx_version }}.tar.gz"dest: /root/nginxwhen: deploy_nginx == "true" and inventory_hostname in groups['nginx']tags: install_nginx # unzip nginx install pkg - name: unzip nginx install pkgunarchive:creates: /root/nginx/nginx-1.20.1copy: nosrc: /root/nginx/nginx-1.20.1.tar.gzdest: /root/nginx when: deploy_nginx == "true" and inventory_hostname in groups['nginx'] tags: install_nginx# deploy install nginx - name: deploy nginxshell: 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;fiwhen: inventory_hostname in groups['nginx']tags: install_nginx# useradd nginx - name: useradd nginxshell: 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 configtemplate:src: templates/nginx/nginx.conf.j2 dest: /root/nginx/nginx/conf/nginx.confwhen: deploy_nginx == "true" and inventory_hostname in groups['nginx']tags: install_nginx# deploy nginx - name: deploy nginxshell: /root/nginx/nginx/sbin/nginxwhen: inventory_hostname in groups['nginx']tags: install_nginx# remove nginx - name: deploy remove nginxshell: 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";fiwhen: inventory_hostname in groups['nginx'] and deploy_nginx == "true"tags: remove_nginx# remove nginx rely on - name: install pcre-devel zlib-devel openssl-devel gccyum:name: "{{ item }}"state: absent loop:- pcre-devel- zlib-devel- openssl-devel- gccwhen: 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: allgather_facts: True # 当执行错误时,继续执行environment:PATH: "{{ ansible_env.PATH }}:/usr/local/bin"become: yesroles:- install
5.添加脚本安装与删除
1)安装脚本
~]# cd /root/ansible/install-sh ~]# vim install.sh #!/bin/bash set -e BASE_DIR=$(cd `dirname $0` && pwd) cd $BASE_DIRCALL_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 docase $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 donehttp(){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 initansible-playbook -f 10 -i /root/ansible/hosts --tags install_nginx /root/ansible/web.yml --extra-vars "hosts=${hosts}" echo "###### deploy nginx end ######" }all_func(){httpnginx_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_DIRCALL_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 docase $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 donehttp(){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 nginxansible-playbook -f 10 -i /root/ansible/hosts --tags remove_nginx /root/ansible/web.yml --extra-vars "hosts=${hosts}"echo "###### remove nginx end ######" }all_func(){httpremove_nginx }main(){$CALL_FUN || help } main
6.调用集成脚本(不变)
~]# cd /root/ansible ~]# vim pot-cmd.sh #!/bin/bash # Author: yhchen set -eBASE_DIR=$(cd `dirname $0` && pwd) cd $BASE_DIREXEC_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 docase $opt ins)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 donecmd(){/root/ansible/install-sh/${EXEC_SCRIPT} -f ${CALL_FUN} -h ${hosts} }main(){if [ "x${EXEC_SCRIPT}" == "x" ]; thenhelpelsecmdfi } main
4)测试用法与“‘7”’类似
记录一个ansible高级用法与shell结合相关推荐
- ansible高级用法(压测脚本)
记录一个ansible高级用法与shell结合_kali_yao的博客-CSDN博客_ansible shell 注:由于ansible的远程原则必须要key(也就是ssh远程测试),所以在配置文件中 ...
- ansible 高级用法
1.控制每次同时更新的主机数量 im test_serial.yml --- - hosts: allserial: 2 #每次只同时处理2个主机max_fail_percentage : 50 #当 ...
- Shell函数的高级用法
Shell函数的高级用法 一.函数的定义和使用 1.语法格式 2.如何调用函数 3.示例演示 4.小脚本(nginx守护进程) 二.向函数传递参数 1.Shell中传参 2.Shell中函数调用 3. ...
- SAP UI5 应用开发教程之七十七 - SAP UI5 动态页面路由的高级用法:路由记录 routes 和 target 的一对多关系试读版
一套适合 SAP UI5 初学者循序渐进的学习教程 教程目录 SAP UI5 本地开发环境的搭建 SAP UI5 应用开发教程之一:Hello World SAP UI5 应用开发教程之二:SAP U ...
- break 的一个“高级用法”
原文地址 http://www.d2school.com/bcyl/bhcpp/newls/ls11.htm#11.1.2 本小节不是很适于没有多少实际编程经历的初学者,所以初学者可以跳过,以后再回头 ...
- class() 高级用法 -- lua
class() 高级用法 class() 除了定义纯 Lua 类之外,还可以从 C++ 对象继承类.比如需要创建一个工具栏,并在添加按钮时自动排列已有的按钮,那么我们可以使用如下的代码:-- 从 CC ...
- 工作中必须要知道的git高级用法
1. rebase变基 问题: 工作中我们一般是从master分支拉自己的开发分支开发,如果master分支被组长合并了其他同事的开发,也就是master分支ahead你的分支,我们这时一般不能直接提 ...
- Linux之Ansible入门用法(实验解析)
Linux之Ansible入门用法(实验解析) 实验前提: 三台CentOS7和一台CentOS6,其中一台CentOS7当作Ansible堡垒机,其余三台主机当作被控主机.四台主机均为最小化安装,全 ...
- foreach用法_25个你不得不知道的数组reduce高级用法
作者:JowayYoung 仓库:Github.CodePen 博客:掘金.思否.知乎.简书.头条.CSDN 公众号:IQ前端 联系我:关注公众号后有我的微信哟 特别声明:原创不易,未经授权不得对此文 ...
最新文章
- 制度缺陷,美国96%受访公司的人工智能项目陷于停顿,难怪封堵
- idea中链接mysql查询_在Idea中编写Java程序连接查询Sqlite数据库
- Android Studio查看Gradle版本
- spring cloud config注意点(疑问)
- 从HttpServletRequest获取完整的请求路径
- vb.net2019-多线程并行计算(2)
- 基于Swoole和Redis实现的并发队列处理系统 1
- 测试计算机操作基础知识,计算机病毒基础知识测试
- git 一口气带你走完git之旅
- 局域网内抢网速_路由器要不要每天重启?多亏宽带师傅透露,难怪网速一天比一天慢...
- P5502 [JSOI2015]最大公约数(gcd性质/min性质/分治)
- 【2021.02.09更新】数字信号处理公式推导
- CPU+GPU异构集群搭建的总结说明
- python docx table 边框_使用pythondocx指定表中的边框外观
- SpringBoot中Session超时原理说明
- ITIL与DevOps
- java 定义整数数组_定义一个由整数组成的数组,要求求出其中的奇数个数和偶数个数...
- 数据结构的小知识点(初学者使用)“朝闻道”知识分享大赛
- 阿里云直播生成推流和播流地址类
- 深度学习模型压缩与优化加速