zoukankan      html  css  js  c++  java
  • Ansible Roles编排实现Httpd角色的部署

    设置SSH免密码登陆
    v
    im ansible_ssh.sh

    #!/bin/bash
    [ -f ~/.ssh/id_rsa ] || ssh-keygen -f ~/.ssh/id_rsa -P "" -q
    rpm -q sshpass &> /dev/null || yum -y install sshpass &> /dev/null
    prefix=10.0.0.
    passwd=tianze


    for i in {12..13};do
    {
        sshpass -p $passwd ssh-copy-id -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa root@$prefix$i &> /dev/null
    }&
    done
    wait

    bash ansible_ssh.sh
    安装ansible
    yum -y install ansible  ##事先需要配置好epel源  
    vim /etc/ansible/hosts  ##文件末尾添加以下内容 主机清单
    [webservers]  ##主机组
    10.0.0.8

    10.0.0.18

    创建角色相关的目录: 

    mkdir -pv 、/data/ansible/roles/httpd/{tasks,handlers,files}

    创建角色相关的文件:

    cd /data/ansible/roles/httpd/

    main.yml是task的入口文件

    [root@centos8-1 httpd]$vim tasks/main.yml
    - include: group.yml
    - include: user.yml
    - include: install.yml
    - include: config.yml
    - include: index.yml
    - include: service.ym

    [root@centos8-1 httpd]$vim tasks/group.yml
    - name:create group
    group: name=apache system=yes gid=80

    [root@centos8-1 httpd]$vim tasks/user.yml

    - name: create apache user
    user: name=apache system=yes shell=/sbin/nologin home=/var/www/ uid=80 group=apache

    [root@centos8-1 httpd]$vim tasks/install.yml

    - name: install httpd package
    yum: name=httpd

    [root@centos8-1 httpd]$vim tasks/config.yml

    - name: config file
    copy: src=httpd.conf dest=/etc/httpd/conf/ backup=yes
    notify: restart

    root@centos8-1 httpd]$vim tasks/index.yml

    - name: index.html
    copy: src=index.html dest=/var/www/html/

    [root@centos8-1 httpd]$vim tasks/service.yml

    - name: start service
    service: name=httpd state=started enabled=yes

    [root@centos8-1 httpd]$vim handlers/main.yml

    - name: restart
    service: name=httpd state=restarted

    在files目录下准备两个文件(可也用yum -y install httpd安装后把文件copy到files目录下)

    [root@centos8-1 ansible]$ls files/
    httpd.conf index.html

    yum -y install httpd

    [root@centos8-1 ansible]$cp /etc/httpd/conf/httpd.conf /roles/httpd/files/

    [root@centos8-1 ansible]$echo The is httpd based roles > /roles/httpd/files/index.html

    [root@centos8-1 ansible]$tree /data/ansible/roles/httpd/
    /data/ansible/roles/httpd/
    ├── files
    │   ├── httpd.conf
    │   └── index.html
    ├── handlers
    │   └── main.yml
    └── tasks
    ├── config.yml
    ├── group.yml
    ├── index.yml
    ├── install.yml
    ├── main.yml
    ├── service.yml
    └── user.yml

    3 directories, 10 files

    在playbook中调用角色

    [root@centos8-1 ansible]$vim /data/ansible/role_httpd.yml

    ---
    - hosts: db
    remote_user: root

    roles:
    - httpd

    运行playbook

    [root@centos8-1 ansible]$ansible-playbook /data/ansible/role_httpd.yml

    把生命浪费在美好的事物上
  • 相关阅读:
    Linux Bash管理
    Linux文件查看
    Linux文件目录
    yum Linux软件安装工具
    第十六、十七天,关于面向对象
    第十四天,内置函数
    第十二天
    第十一天
    第十天
    第九天(开始函数)
  • 原文地址:https://www.cnblogs.com/tz66/p/13512484.html
Copyright © 2011-2022 走看看