zoukankan      html  css  js  c++  java
  • 自动化运维--ansible(2)

    问题一:如何在多台服务器中配置Web项目上线的所有环境

    解答:

      1.使用ansible配置nginx服务

    在安装前了解rpm与yum的区别  rpm是压缩包安装依赖包需要自己手动安装,yum安装解决依赖包的问题

    安装pip的前提是要给所有控制机配置epel源
    ansible web -m shell -a 'wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo'
    • [epel]
      name=Extra Packages for Enterprise Linux 7 - $basearch #名字
      baseurl=http://mirrors.aliyun.com/epel/7/$basearch  #rpm源的地址,可以写http,https,ftp,Samba,file:
      failovermethod=priority
      enabled=1 # 是否开启,1代表开启,0表示关闭
      gpgcheck=0  #是否校验签名,1代表校验,0表示校验
      gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
    • yum 安装包组

      yum grouplist # 查看包组信息
      yum groupinstall # 安装包组
      disablerepo #禁用源
      enablerepo #启用源
      name #包名
      state install (`present' or `installed', `latest'), or remove (`absent' or `removed')
      ansible web -m yum -a 'name=wget' # 安装wget
      安装好之后其他被控机直接复制就可以
      ansible web -m copy -a 'src=/etc/yun.repos.d/epel.repo dest=/etc/yun.repos.d/epel.repo'

      给全部被控制机安装pip
      # 安装python2-pip
      ansible web -m yum -a 'name=wget state=absent' # 卸载软件包
      ansible web -m yum -a 'name="@Development Tools"' # 安装包组

    pip

    pip install 安装包
    pip freeze > a.txt 将python的环境打包到文件中
    pip install -r a.txt 安装文件中的包
    pip list 查看所有的以安装成功的包
    ansible web -m pip -a 'name=flask' # 安装flask模块

    service

    ps -ef|grep nginx #查看进程
    ss -tnlp # 查看端口信息
    systemctl start nginx # centos7
    service nginx start  # centos6
    systemctl enabled nginx # centos7 开机自启动
    chkconfig nginx on # centos6开机自启动
    ansible web -m service -a 'name=nginx state=started' # 启动nginx
    ansible web -m service -a 'name=nginx state=stopped' # 关闭nginx

    计划任务

    cron

    * * * * * job 
    分 时 日 月 周 任务
    0 */2 * * * job 每隔两个小时
    0 12,13 * * * job 12点和13点
    0 12-17 * * * job 12点到17点
    0 12-17/2 * * 1,3,6,0 周1,周3,周6,周7 12点到17点每隔两个小时
    crontab -e # 编辑计划任务
    crontab -l # 查看计划任务
    crontab -r # 删除计划任务
    day  天
    disabled 禁用
    hour 小时
    job 任务
    minute 分钟
    month 月
    name 任务名字
    weekday 周
    ansible db -m cron -a 'minute=26 job="touch /tmp/xzmly.txt" name=touchfile' # 新建一个计划任务
    ansible db -m cron -a 'name=touchfile state=absent' # 删除一个计划任务
    ansible db -m cron -a 'minute=26 job="touch /tmp/xzmly.txt" name=touchfile disabled=yes'  # 禁用计划任务,以#表示禁用

    用户相关

    user

    用户:
    管理员 root 0
    普通用户
    系统用户 不能登录  1-999 centos7 1-499 centos6
    登录用户 可以登录  1000-65535 centos7 500-65535 centos6
    用户组:
      管理员组 root 0
      系统用户组 1-999 centos7 1-499 centos6
      登录用户组 1000-65535 centos7 500-65535 centos6
       
    -d 指定用户的家目录
    -g 指定用户的组
    -G 执行用户的附加组
    -s 指定登录后使用的shell
    -r 创建一个系统组
    useradd -r wusir 创建系统用户, 从999倒序
    useradd -s /sbin/nologin alexsb 创建的是普通用户,从1000开始升序
    useradd -d /opt/alexsb2 alexsb2 创建用户时指定用户的家目录
      useradd -u 3000 alexsb6 # 创建用户并指定用户的uid
    userdel alex 删除用户
    userdel -r alexsb2 删除用户并删除用户的家目录
     
    groupadd yuchao 创建用户组
    groupdel yuchao 删除用户组
    group 组
    groups 附加组
    home 家目录
    name 用户名
    password 密码
    remove ?
    shell 用户登录后使用的shell
    system 创建一个系统用户
    uid 用来指定用户的id
    state 状态
    ansible db -m user -a 'name=wulaoshi uid=4000 home=/opt/wulaoshi groups=root shell=/sbin/nologin' #创建一个用户,并指定用户的id,用户的家目录,用户的附加组,用户的shell
    ansible db -m user -a 'name=wulaoshi state=absent' #删除用户但是不删除用户的家目录
    ansible db -m user -a 'name=wulaoshi3 state=absent remove=yes' # 删除用户并删除用户的家目录

    group

    gid 组的id
    name 组名
    system 系统组
    state
    ansible db -m group -a 'name=wulaoshi system=yes' #创建系统组
    ansible db -m group -a 'name=wulaoshi state=absent' # 删除组

    web

    创建一个用户组alex10

    ansible web -m group -a 'name=alex10'

    创建一个用户wusir10

    ansible web -m user -a 'name=wusir10'

    把/etc/fstab文件复制到远程主机上/tmp/f

    ansible web -m copy -a 'src=/etc/fstab dest=/tmp/f'

    安装nginx,并启动,设置开机自启动

    ansible web -m yum -a 'name=nginx'

    ansible web -m service -a 'name=nginx enabled=yes'

    ansible 剧本

    yaml

    是一个编程语言
    xml 是用来写配置文件的一个语言
    ini
    yaml

    字典: key: value

    列表: [] -

    后缀名 yaml yml

    ansible-playbook命令格式

    执行顺序: 从上往下

    特性:幂等性 不管执行多少遍,结果都是一样的

    ansible-playbook [options] playbook.yml [playbook2 ...] 
    -C, --check   # 检查,白跑,干跑
    -f FORKS, --forks=FORKS #用来做并发
    --list-hosts # 列出主机列表
    --syntax-check # 语法检查

    简单用法(格式比较严格)

    • 冒号后面必须有空格

    • 等号后面不能有空格

    • - 后面也要有空格

    • 严格对齐

    实例:
    - hosts: web tasks: - name: creategroup group: name=alex10 - name: cretaeuser user: name=wusir10

    传参(五种方式)

    - hosts: web
    tasks:
     - name: create{{ user }}
      user: name={{ user}}

    第一种方式

    ansible-playbook -e 'user=alexsb10' p2.yml

    第二种方式

    [db]
    192.168.107.132 user=alexsb11
    192.168.107.133 user=alexsb12

    第三种方式

    [db:vars] #表示组的参数
    user=alexsb13

    第四种方式

    - hosts: db
    vars:
     - user: alexsb14
    tasks:
     - name: create{{ user }}
      user: name={{ user}}

    第五种传参方式

    - hosts: db
    tasks:
     - name: sum
      shell: echo 7+8|bc
      register: user
     - name: createuser
      user: name={{user.stdout}}

    传参方式的优先级

    -e > playbook vars > hosts文件

     

     

    内容总结

    模块

    • yum 安装包

      • @

    • pip 安装python的模块

      • requirements

    • service

      • enabled=yes

    • corn

      • disabled=yes

    • user

      • system=yes

    • group

      • system=yes

    剧本

    • yaml 格式 (格式要求比较严格)

    • 传参

      • -e

      • hosts文件 主机列表后面

      • hosts文件[groupname:vars]

      • playbook里面写vars

      • register

    • 传参的优先级

      • -e > playbook > hosts

  • 相关阅读:
    【类似N^N做法的斐波那契数列】【HDU1568】 Fibonacci
    【取对数+科学计数法】【HDU1060】 N^N
    【枚举+数学】【HDU1271】整数对 难度:五颗星
    【欧拉函数】【HDU1286】 找新朋友
    【筛素数表证明】【O[n]】
    【沙茶了+筛选保存最大质因数】【HDU2136】Largest prime factor
    【gcd+数学证明】【HDU1722】 CAKE
    【贪心】【HDU3177】 搬家问题
    HDU2093 字符串2种不错的读入思路
    tf.argmax()
  • 原文地址:https://www.cnblogs.com/RootEvils/p/10409536.html
Copyright © 2011-2022 走看看