ansible 1、ansible介绍安装及常用模块 2、ansible的playbook 3、ansible的配置 ================================================================================================================================================ ansible 1、介绍 ansible是一个自动化运维工具,ansible基于Python开发。ansible基于ssh协议与被管理服务器进行通信,而无需被管理的服务器 安装agent。ansible是一个高度模块化的程序,功能都依赖的于各模块,ansible仅仅是一个框架。ansible的使用,不需要启动任何 进程,只需要安装即可使用。 2、ansible架构 ansible有核心模块(Core Modules)和自定义模块(Custom Modules),用来实现特定的功能操作; 用插件的方式(Plugin)扩展ansible功自身的功能; 使用主机清单(Host Inventory)的方式定义那些是管理的服务器; 将多个模块实现的操作写成Playbooks,使得一次性完成多重操作; 3、基于ssh无密通信 ssh-keygen ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.1.151 ssh root@192.168.1.151 4、查看命令 ansible-doc -l # 列出ansible支持的核心模块 ansible-doc -s MODULES-NAME # 查看某模块的用法 5、ansible指令使用 格式:ansible [-m module_name] [-a args] [options] :HOST-IP|all|HOSTGROUP-NAME -m module_name:command是默认模块 ansible命令选项: -v|-vv|-vvv # 显示命令执行的详细结果信息 -i # 指定hosts文件路径 -f # 一个整数,默认是5,fork开启同步进程的个数 -C # 测试指令,不会真正执行命令 模块使用: user模块: # 创建用户 ansible webserver -m user -a "name=nginx state=present" # 删除用户 ansible webserver -m user -a "name=nginx state=absent" cron模块: # 创建一个任务计划 ansible webserver -m cron -a 'name="sync time" minute="*/10" job="/sbin/ntpdate 192.168.1.150 &> /dev/null"' # 删除一个任务计划 ansible webserver -m cron -a 'name="sync time" state=absent' copy模块 # 复制一个文件到远程服务器 ansible webserver -m copy -a 'src=/etc/hosts dest=/root/ group=root owner=root mode=u+rwx,g=r,o=r' file模块 # 设置文件属性 # 创建一个目录 ansible webserver -m file -a 'path=/tmp/dir state=directory' # 创建一个软链接 ansible webserver -m file -a 'path=/tmp/dir-link src=/tmp/dir state=link' ping模块 # 查看主机是否在线 ansible webserver -m ping yum模块 # 安装nginx ansible webserver -m yum -a 'name=nginx state=latest' service模块 # 启动nginx ansible webserver -m service -a 'name=nginx state=started enabled=yes' shell模块 # command模块不支持管道和变量的使用 # 设置密码 ansible webserver -m shell -a 'echo test | passwd --stdin test' script模块 ansible webserver -m script -a "mkdir.sh" setup模块 ansible webserver -m setup 6、YAML语法 YAML最初是Yet Another Markup Language(另一种标记语言)的缩写,后来为了强调yaml是以数据为中心重新命名为递归写法YAML Ain’t Markup Language(YAML 不是一种标记语言)。 yaml用#作为注释标记 散列表:(又叫哈希表),有键值对组成。 示例: name: xuejinwei age: 12 或者 {name: xuejinwei, age: 12} 列表: 示例: - list1 - list2 - list3 或者 [list1, list2, list3] 散列表中是引用列表: 示例: name: - name1 - name2 列表中使用散列项: 示例: - name: xuejinwei age: 18 或者 - {name: xuejinwei, age: 18} 列表中使用列表项: 示例: - [name, age, ] 或者 - - name1 - name2 散列表中使用散列表: 示例: names1: name1: xue name2: jin name3: wei names2: .....: .. .....: .. 7、ansible中的变量 变量命名方式:由数字、字母、下划线组成,只能以字母开头 变量定义|传递方式: facts # facts是由正在通信的远程目标主机发回的信息,这些信息被保存在ansible变量中。要获取指定的远程主机所支持的所有facts,可使用如下命令进行: ansible hostname -m setup register # 把任务的输出定义为变量 通过命令行 ansible-playbook test.yml --extra-vars "name=nginx user=root" 通过roles传递变量 roles: - { rele: var1, dir: __, user: __ } 主机变量: [webservers] www1.magedu.com http_port=80 maxRequestsPerChild=808 www2.magedu.com http_port=8080 maxRequestsPerChild=909 或者 [webservers] www1.magedu.com www2.magedu.com [webservers:vars] ntp_server=ntp.magedu.com nfs_server=nfs.magedu.com 组嵌套: [apache] httpd1.magedu.com httpd2.magedu.com [nginx] ngx1.magedu.com ngx2.magedu.com [webservers:children] apache nginx [webservers:vars] ntp_server=ntp.magedu.com 8、playbook组成结构: Inventory Modules Ad Hoc Command hosts Playbook Task:任务,即调用模块完成的某操作 Variables:变量 Templates:模板 Handlers:处理器,由某事件触发执行的操作 Roles:角色 示例: - hosts: webnodes # hosts用于指定要执行指定任务的主机,其可以是一个或多个由冒号分隔主机组 vars: http_port: 80 max_clients: 256 remote_user: root # remote_user则用于指定远程主机上的执行任务的用户。remote_user也可用于各task中 tasks: # 任务列表task list中的各任务按次序逐个在hosts中指定的所有主机上执行,即在所有主机上完成 第一个任务后再开始第二个如果中途发生错误,所有已执行任务都有可能将回滚 - name: ensure apache is at the latest version # 在众多模块中,只有command和shell模块仅需要给定一个列表而无需使用“key=value command: /sbin/setenforce 0 yum: name=httpd state=latest - name: ensure apache is running service: name=httpd state=started handlers: - name: restart apache service: name=httpd state=restarted 9、playbook中的条件判断 when: 示例: task: - name: create user1 user user: name=user1 when: ansible_fqdn=xuekaixin 10、playbook中的迭代 ================================================================================================================================================ 示例: ansible命令格式 ansible HOST-PATTERN -m MOD_NAME -a MOD_ARGS -C -f FORKS command模块 # chdir=/path/to/dir 参数指定命令在执行前将当前路径切换到chdir指定的目录下在执行 ansible 172.18.26.2 -m command -a "chdir=/root pwd" ansible 172.18.26.2 -m command -a "touch test.txt chdir=/tmp" # creates=FILENAME|GLOB_PATTERN 保证命令执行的幂等性。当文件存在时,不执行 ansible 172.18.26.2 -m command -a "mkdir mydir chdir=/tmp creates=mydir" # removes=/path/to/fie 如果此处给定的文件或目录不存在,则不执行命令 # 创建一个用户 ansible websrvs -m command -a "useradd user1" shell模块 # executable=/path/to/shell 指定shell执行命令 # 使用shell模块修改用户密码 ansible websrvs -m shell -a "echo xuekaixin | passwd --stdin user1" group模块 # 操作的资源状态 state=present|absent 默认是present # 创建一个组 ansible websrvs -m group -a "name=haproxy system=yes state=present" ansible websrvs -m group -a "name=haproxy system=yes state=absent" user模块 # 创建用户 ansible websrvs -m user -a "name=xuekaixin groups=haproxy state=present uid=1024 createhome=yes shell=/bin/tcsh generate_ssh_key=true" # 修改用户UID ansible websrvs -m user -a "name=xuekaixin groups=haproxy state=present uid=2048 createhome=yes shell=/bin/tcsh generate_ssh_key=false" cpoy模块 # 属性 src=/path/to/file dest=/path/to/file content=" " # 直接指定文件内容 # 复制文件 ansible websrvs -m copy -a "src=/root/haha.xixi dest=/tmp/haha.xixi owner=xuekaixin group=xuekaixin mode=664" file模块 # 属性 state=file|directory|link|hard|touch|absent # 创建链接文件 ansible websrvs -m file -a "path=/tmp/xue.xixi src=/tmp/haha.xixi state=link" get_url模块 # 批量下载一个文件 ansible all -m get_url -a "url=http://172.16.0.1/centos/CentOS-6.8-x86_64/EULA dest=/tmp" cron模块 # 定义一个模块 ansible all -m cron -a "name='timesync' job='/usr/sbin/ntpdate 172.18.0.1 &> /dev/null' state=absent minute='*/5'" yum模块 # 属性 state=present|installed|latest absent|remove name=Package_Name 可以指定版本号 conf_file=file.conf disable_gpg_check= list # 安装nginx ansible all -m yum -a "name=nginx state=latest" pip模块 管理Python依赖库的管理工具 service模块 state=started|stopped|restarted|reloaded name= enabled=true|false # 启动Nginx ansible all -m service -a "name=nginx enabled=true state=started" git模块 # Deploy software (or files) from git checkouts # 属性 repo= 仓库地址 dest= 目标路径 deploy_helper模块 # Manages some of the steps common in deploying projects. haproxy模块 # Enable, disable, and set weights for HAProxy backend servers using socket commands. backend= 指明那个backend的那个host host= state=drain|enabled|disabled playbook ansible-playbook -C file.yaml ansible-playbook --list-hosts ansible-playbook --list-tasks --syntax-check ansible-playbook file.yaml # 安装nginx并启动;安装redis并提供配置文件,同时启动之 - hosts: web remote_user: root tasks: - name: install nginx package yum: name=nginx state=latest - name: start nginx service service: name=nginx enabled=true state=started - hosts: db remote_user: root tasks: - name: install redis package yum: name=redis state=latest - name: install conf file copy: src=/root/redis.conf dest=/etc/redis.conf owner=redis group=root mode=644 - name: start redis service service: name=redis enabled=true state=started # 标签的使用 - hosts: web remote_user: root tasks: - name: install nginx package yum: name=nginx state=latest - name: start nginx service service: name=nginx enabled=true state=started - hosts: db remote_user: root tasks: - name: install redis package yum: name=redis state=latest - name: install conf file copy: src=/root/redis.conf dest=/etc/redis.conf owner=redis group=root mode=644 tags: instconf - name: start redis service service: name=redis enabled=true state=started tags: startredis # 使用标签 # 使用 ansible-playbook -t instconf,startredis nginx.yaml # 条件式触发器 - hosts: web remote_user: root tasks: - name: install nginx package yum: name=nginx state=latest - name: start nginx service service: name=nginx enabled=true state=started - hosts: db remote_user: root tasks: - name: install redis package yum: name=redis state=latest - name: install conf file copy: src=/root/redis.conf dest=/etc/redis.conf owner=redis group=root mode=644 notify: restart redis service # 引用触发器 tags: instconf - name: start redis service service: name=redis enabled=true state=started tags: startredis handlers: # 定义触发器 - name: restart redis service service: name=redis state=restarted # 给play添加tags - hosts: web remote_user: root tags: nginx service # play添加标签 tasks: - name: install nginx package yum: name=nginx state=latest - name: start nginx service service: name=nginx enabled=true state=started - hosts: db remote_user: root tags: redis service # play添加标签 tasks: - name: install redis package yum: name=redis state=latest - name: install conf file copy: src=/root/redis.conf dest=/etc/redis.conf owner=redis group=root mode=644 notify: restart redis service tags: instconf - name: start redis service service: name=redis enabled=true state=started tags: startredis handlers: - name: restart redis service service: name=redis state=restarted {default,file,handlers,meta,tasks,templates,vars} 变量 (1) facts:可直接调用; 注意:可使用setup模块直接获取目标主机的facters; (2) 用户自定义变量: (a) ansible-playbook命令的命令行中的 -e VARS, --extra-vars=VARS # 示例:ansible-playbook test.yml --extra-vars "name=nginx user=root" Note:命令行的变量比playbook中的变量优先级高 (b) 在playbook中定义变量的方法: vars: - var1: value1 - var2: value2 变量引用:{{ variable }} (3) 通过roles传递变量; (4) Host Inventory Note:变量优先级低 (a) 用户自定义变量 (i) 向不同的主机传递不同的变量; IP|HOSTNAME varaiable=value var2=value2 (ii) 向组中的主机传递相同的变量; [groupname:vars] variable=value (b) invertory参数 用于定义ansible远程连接目标主机时使用的参数,而非传递给playbook的变量; ansible_ssh_host ansible_ssh_port ansible_ssh_user ansible_ssh_pass ansbile_sudo_pass ... # 模板 # 基于模板的方式生成一个文件复制到远程主机。基于ansible的templates模块实现。将模板文件中的变量替换成变量的值后复制到远程主机。 模板:templates 文本文件,嵌套有脚本(使用模板编程语言编写) Jinja2: 字面量: 字符串:使用单引号或双引号; 数字:整数,浮点数; 列表:[item1, item2, ...] 元组:(item1, item2, ...) 字典:{key1:value1, key2:value2, ...} 布尔型:true/false 算术运算: +, -, *, /, //, %, ** 比较操作: ==, !=, >, >=, <, <= 逻辑运算: and, or, not 示例: - hosts: websrvs remote_user: root tasks: - name: install nginx yum: name=nginx state=present - name: install conf file template: src=files/nginx.conf.j2 dest=/etc/nginx/nginx.conf notify: restart nginx tags: instconf - name: start nginx service service: name=nginx state=started handlers: - name: restart nginx service: name=nginx state=restarted 模板配置文件 :nginx.conf.j2 worker_processes {{ ansible_processor_vcpus }}; listen {{ http_port }}; ================================================================================================================================================ 实践 # 部署 jsp 应用运行环境 yum install java-1.8.0-openjdk # 检测 jdk 是否安装成功 java -version # 安装 tomcat yum -y install tomcat tomcat-webapps tomcat-admin-webapps tomcat-docs-webapp {default,file,handlers,meta,tasks,templates,vars} 1、部署redis ansible-playbook redis.yml 2、部署Haproxy ansible-playbook haproxy.yml 3、部署varnish ansible-playbook varnish.yml 4、部署tomcat环境 ansible-playbook tomcat.yml