自动化运维工具-Ansible集中化管理-Ansible操作

定义主机清单

  • 第一次连接时不输入yes

    vim /etc/ansible/ansible.cfg
    inventory = /etc/ansible/hosts
    host_key_checking = False
    
  1. 基于端口,用户,密码定义主机清单

    ansible_ssh_port:指定ssh端口 ansible_ssh_user:指定 ssh 用户
    ansible_ssh_pass:指定 ssh 用户 登录是认证密码(明文密码不安全)
    ansible_sudo_pass:指明 sudo时候的密码

    vim  /etc/ansible/hosts
    [web-servers]                 #主机组名
    192.168.66.241  ansible_ssh_port=22  ansible_ssh_user=root  ansible_ssh_pass=a
    
  • 简单测试下主机的连通性

     ssh 192.168.66.241
     ansible -i /etc/ansible/hosts web-servers -m ping  
    

    -i----指定 host 文件的路径,默认是在/etc/ansible/hosts
    -m----指定使用的ping模块

  1. 基于ssh密钥来访问定义主机清单

    一般来说,使用明文密码不安全,所以增加主机无密码访问。
    在Ansible服务端生成密钥,并且复制公钥到节点中。

    • 生产密钥

       ssh-keygen
      
    • 传输公钥

       ssh-copy-id  root@192.168.66.22 
       ssh-copy-id root@192.168.66.241
      
    • 定义主机清单

      vim  /etc/ansible/hosts
      [web-servers]
      192.168.66.22
      192.168.66.241
      

在控制端运行命令

  • ping模块检查网络连通性

     ansible -i /etc/ansible/hosts 'web-servers'  -m ping
    
  • command模块执行shell命令,不支持管道,没法批量执行命令

     ansible -i /etc/ansible/hosts 'web-servers' -m command -a "uptime"
    

    案例:将df命令在所有节点执行后,重定向输出到本机的/tmp/command-output.txt文件中

     ansible -i /etc/ansible/hosts 'web-servers' -m command -a "df -Th" > /tmp/command-output.txt
     cat /tmp/command-output.txt 
    
  • shell模块:使用shell模块,在远程命令通过/bin/sh来执行;所以终端输入的各种命令方式,都可以使用

     ansible -i /etc/ansible/hosts  web-servers -m shell -a "free -m"
    
  • scripts模块:使用scripts模块可以在本地写一个脚本,在远程服务器上执行

    vim  /etc/ansible/net.sh
    #!/bin/bash
    date
    hostname
    ansible -i /etc/ansible/hosts  web-servers -m script -a "/etc/ansible/net.sh"
    
  • copy模块:实现主控端向目标主机拷贝文件,类似scp功能

     ansible -i /etc/ansible/hosts web-servers -m copy -a "src=/etc/hosts dest=/tmp/ owner=root 		group=root mode=0755"
    
  • file模块设置文件属性

     ansible -i /etc/ansible/hosts web-servers -m file -a "path=/tmp/hosts mode=0777"
    
  • stat模块获取远程文件信息

     ansible -i /etc/ansible/hosts web-servers -m stat -a "path=/tmp/hosts"
    
  • get_url模块实现远程主机下载指定url到本地,支持sha256sum文件校验。

     ansible -i /etc/ansible/hosts web-servers -m get_url -a "url=https://...  dest=/tmp/ mode=0440 force=yes"
    

    查看force=yes的作用
    如果force=yes,当下载文件时,如果所下的内容和原目录下的文件内容不一样,则替换原文件,如果一样,就不下载了。
    如果为“force=no”,则仅在目标不存在时才下载文件。 一般来说,只有小型本地文件才应该为“是”。 在0.6之前,该模块表现为默认为“是”。

  • yum模块可以提供的status状态: latest 、present、installed 这3个代表安装;removed、absent 这2个是卸载

     ansible -i /etc/ansible/hosts web-servers -m yum -a "name=httpd  state=latest"
    
  • cron模块远程主机crontab配置

     增加每30分钟执行ls /tmp
     ansible -i /etc/ansible/hosts web-servers -m cron -a "name='list dir' minute='*/30' job='ls /tmp'"
    
  • sysctl模块远程主机sysctl配置

     开启路由转发功能
     ansible -i /etc/ansible/hosts web-servers -m sysctl -a "name=net.ipv4.ip_forward value=1 reload=yes"
    
  • user模块远程主机用户管理

     ansible -i /etc/ansible/hosts web-servers -m user -a "name=xuegod6 state=present"
    

使用Playbook批量部署多台LAMP环境

Playbook常用文件夹作用:
files:存放需要同步到异地服务器的源码文件及配置文件;
handlers:当服务的配置文件发生变化时需要进行的操作,比如:重启服务,重新加载配置文件;
meta:角色定义,可留空;
tasks:需要进行的执行的任务;
templates:用于执行lamp安装的模板文件,一般为脚本;
vars:本次安装定义的变量

使用playbook创建一个LAMP构建的任务

  1. 创建相关文件

    mkdir -pv /etc/ansible/lamp/roles/{prepare,httpd,mysql,php}/{tasks,files,templates,vars,meta,default,handlers}
    
  2. 准备相关配置文件

    cp /etc/httpd/conf/httpd.conf  /etc/ansible/lamp/roles/httpd/files/
    mv /var/www/html/index.php /etc/ansible/lamp/roles/httpd/files/
    cp /etc/my.cnf  /etc/ansible/lamp/roles/mysql/files/
    
  3. 准备yaml

    vim /etc/ansible/lamp/roles/prepare/tasks/main.yml 
    - name: delete yum config
      #删除原有的yum配置文件  shell: rm -rf /etc/yum.repos.d/* 
    - name: provide yumrepo file
      #下载新的yum配置文件
      shell: wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo  
    - name: clean the yum repo
      #清除原有的yum缓存信息
      shell: yum clean all
    - name: clean the iptables
      #清除原有防火墙规则,不然后可能上不了网
      shell: iptables -F
    
    vim /etc/ansible/lamp/roles/httpd/tasks/main.yml
    - name: web server install
      yum: name=httpd state=present
    - name: provide test page
      copy: src=index.php dest=/var/www/html
    - name: delete apache config
      shell: rm -rf  /etc/httpd/conf/httpd.conf
    - name: provide configuration file
      copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
      notify: restart httpd
    

    notify和handlers
    notify:这个action可用于在每个play的最后被触发,这样可以避免多次有改变发生时 ,每次都执行指定的操作,取而代之,仅在所有的变化发生完成后一次性地执行指定操作。
    handler:在notify中列出的操作称为handler,也即notify中调用handler中定义的操作。

喜欢的亲可以关注点赞评论哦!以后每天都会更新的哦!本文为小编原创文章;
文章中用到的文件、安装包等可以加小编联系方式获得;
欢迎来交流小编联系方式VX:CXKLittleBrother
进入运维交流群

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

含义小哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值