Roles介绍
roles基于一个已知的文件结构来自动加载tasks handlers templates vars files
roles下的目录里面只能写相关的tasks或者handlers。然后再通过playbook文件来调用相关roles
创建角色的两种方式
1.手动创建相关目录
mkdir redis/{tasks,handlers,templates,vars,files} -pv
2.ansible-galaxy init redis roles
redis是角色名称
ansible galaxy 介绍
galaxy是一个免费网站,类似于github网站,网站上基本都是共享的roles角色。从Galaxy下载roles角色是快速启动自动化项目方式之一
ansible提供了ansible-galaxy命令行工具,可以使用init search install remove等操作
1.ansible-galaxy search nginx
2.ansible-galaxy install nginx
把角色安装包下载到/root/.ansible/roles目录下
ansible角色调试
ansible-playbook -i hosts site.yml -vvv
ansible playbook输出所有节点信息
- name: "get all nodes status"
shell: "supervisorctl status"
register: result
tags: checknodes
- name: "show all node status"
debug:
msg: "{{ result }}"
tags: checknodes
ansible命令输出批量节点信息
ansible只对hosts文件中的[group]进行分组操作,没有角色这个对象
ansible -i hosts all -m shell -a "supervisorctl status" -i hosts all 在hosts文件中必须定义一个all组
site.yml是ansible-playbook命令才会被定义.ansible命令只要定义hosts文件即可
ansible-playbook -i hosts site.yml 这样就相当于在all的节点上执行命令
ansible > hosts文件 > 具体节点
ansible-playbook > site.yml > hosts文件 > 具体节点
ansible模块同时管理多个service
使用循环列表的指定服务名称方式启停服务
ansible拷贝整个目录下的文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
- name: "copy the flink-lib jars" copy: src=../../common/packages/flink/lib/ dest="{{ taishi_dir }}/tmp/flinklib/" owner={{ taishi_user }} group={{ taishi_user }} mode=0755 tags: flink-master-jar - name: show the jars in the lib command: ls {{ taishi_dir }}/tmp/flinklib/ register: dir_out tags: flink-master-jar - name: "upload the flink-lib jars" copy: src=/{{ taishi_dir }}/tmp/flinklib/{{item}} dest={{ taishi_dir }}/flink/flink-1.12.2/lib/{{item}} owner={{ taishi_user }} group={{ taishi_user }} mode=0755 with_items: "{{ dir_out.stdout_lines }}" tags: flink-master-jar
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
- name: "supervisor start flink" shell: "supervisorctl update" ignore_errors: True tags: flink-master - name: "check flink master" wait_for: port: 8081 delay: 10 timeout: 300 tags: flink-master-jar - name: "upload flink web jar" shell: "curl -F 'file=@/tmp/analysis-entrypoint.jar' http://{{ groups['flink-master'][0] }}:8081/jars/upload" ignore_errors: True tags: flink-master-jar
自动实现上传jar包
ansible使用ssh用户名和密码登录主机
1.不再通过ssh免密登录,而是直接通过hosts中配置用户名和密码来部署
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[privilege_escalation] become=True become_method=sudo become_user=root become_ask_pass=False [defaults] host_key_checking = False
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[nfsserver] 192.168.30.105 [nfsclient] 192.168.30.105 [elastic-master] 192.168.30.105 [elastic] 192.168.30.105 [mysql] 192.168.30.105 [redis-master] 192.168.30.105 [zookeeper] 192.168.30.105 [kafka] 192.168.30.105 [flink-master] 192.168.30.105 [app-master] 192.168.30.105 [Tlog] 192.168.30.105 [nginx] 192.168.30.105 [monitor] 192.168.30.105 [all] 192.168.30.105 [all:vars] ansible_ssh_user=admin ansible_ssh_pass=123456
2.清空受控端主机known_hosts文件
3.执行部署
ansible-playbook -i hosts_userpasswd site_standalone.yml
ssh无免密登录部署
ansible把多个主机组合并成一个主机组
设置hosts文件父分组和子分组
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[flink-master] 192.168.30.105 [flink-worker] 192.168.30.110 [flinkcluster:children] flink-master flink-worker
循环遍历合并后分组主机
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
{% set flinkhosts = [] %} {% for host in groups["flinkcluster"] %} {{ flinkhosts.append(host | string+":9213")}} {{ flinkhosts.append(host | string+":9214")}} {% endfor %} - targets: {{ flinkhosts | to_json }} labels: group: 'flink-exporter'
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
{% set hosts = [] %} {% for host in groups["all"] %} {{ hosts.append(host|string+":9100") }} {% endfor %} - targets: {{ hosts | to_json }} labels: group: 'node-exporter' - job_name: 'system_app' scrape_interval: 60s static_configs: - targets: ['{{ groups["elastic-master"][0] }}:9114'] labels: group: 'elastic-exporter' {% set flinkhosts = [] %} {% for host in groups["flinkcluster"] %} {{ flinkhosts.append(host | string+":9213")}} {% if host == groups["flink-master"][0] %} {{ flinkhosts.append(host | string+":9214")}} {% endif %} {% endfor %} - targets: {{ flinkhosts | to_json }} labels: group: 'flink-exporter'
ansible调试jinjia2语法
ansible-playbook -i hosts --tags monitor-test2 site.yml
ansible playbook变量定义嵌套
es_path_data: "{{ taishi_dir }}/es-cluster/data"
es_path_logs: "{{ taishi_dir }}/es-cluster/logs"