基于ansible的生产环境部署构建(二) 角色localhost

角色功能说明:

  • 该角色主要实现对操纵主机的各项下载,如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]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值