角色功能说明:
- 该角色主要实现对操纵主机的各项下载,如ssh公私钥和各个会用到的部署软件包
- 需要预先生成ssh公私钥并存储文件共享服务器相应URL,注意公私钥文件默认权限需要更改
- 需要预先在文件共享服务器中下载好相应的部署软件包
- 该角色可以反复执行,可以更改defaults_var.yml中的软件包版本,再次执行,完成软件包的下载
角色部署:
- 创建目录结构,创建软连接,关联默认变量文件
WorkDir=~/devops/ansible/os_init && cd ${WorkDir}
RoleName=localhost
mkdir -pv roles/${RoleName}/{defaults,files,handlers,meta,tasks,templates,vars}
ln -s ${WorkDir}/defaults_var.yml roles/${RoleName}/defaults/main.yml
- 创建角色任务
cat >roles/${RoleName}/tasks/main.yml<<EOF
---
- name: "删除.ssh目录"
file:
path: ~/.ssh
state: absent
- name: "重建.ssh目录"
file:
path: ~/.ssh
state: directory
owner: root
group: root
mode: 0700
- name: "下载ssh私钥"
get_url:
url: "{{ private_key }}"
validate_certs: no
dest: ~/.ssh
owner: root
group: root
mode: 0600
- name: "下载ssh公钥"
get_url:
url: "{{ public_key }}"
validate_certs: no
dest: ~/.ssh
owner: root
group: root
mode: 0644
- name: "下载 zabbix agent centos6 rpm"
get_url:
url: "{{ zabbix_agent_6_url }}"
validate_certs: no
dest: ${WorkDir}/software
owner: root
group: root
mode: 0644
- name: "下载 zabbix agent centos7 rpm"
get_url:
url: "{{ zabbix_agent_7_url }}"
validate_certs: no
dest: ${WorkDir}/software
owner: root
group: root
mode: 0644
- name: "下载 java6 部署包"
get_url:
url: "{{ java6_url }}"
validate_certs: no
dest: ${WorkDir}/software
owner: root
group: root
mode: 0644
- name: "下载 java7 部署包"
get_url:
url: "{{ java7_url }}"
validate_certs: no
dest: ${WorkDir}/software
owner: root
group: root
mode: 0644
- name: "下载 java8 部署包"
get_url:
url: "{{ java8_url }}"
validate_certs: no
dest: ${WorkDir}/software
owner: root
group: root
mode: 0644
- name: "下载 tomcat6 部署包"
get_url:
url: "{{ tomcat6_url }}"
validate_certs: no
dest: ${WorkDir}/software
owner: root
group: root
mode: 0644
- name: "下载 tomcat7 部署包"
get_url:
url: "{{ tomcat7_url }}"
validate_certs: no
dest: ${WorkDir}/software
owner: root
group: root
mode: 0644
- name: "下载 tomcat8 部署包"
get_url:
url: "{{ tomcat8_url }}"
validate_certs: no
dest: ${WorkDir}/software
owner: root
group: root
mode: 0644
- name: "下载 logstash rpm"
get_url:
url: "{{ logstash_url }}"
validate_certs: no
dest: ${WorkDir}/software
owner: root
group: root
mode: 0644
EOF
- 创建任务playbook并执行
cat >os-init-1-${RoleName}.yml<<EOF
---
- hosts: localhost
remote_user: root
gather_facts: false
roles:
- ${RoleName}
EOF
ansible-playbook -i inventory/hosts os-init-1-${RoleName}.yml
[TOC]