Ansible-playbook

yum
state:状态(present,absent,latest),表示是安装还卸载
   present:默认的,表示为安装
   latest: 安装为最新的版本
   absent:表示删除

 确认一个软件包已经安装,但不去升级它:
            $ ansible webservers -m yum -a "name=lftp state=present"
            确认一个软件包的安装版本:
            $ ansible webservers -m yum -a "name=lftp  state=present"

            确认一个软件包还没有安装:
            $ ansible webservers -m yum -a "name=lftp  state=absent"

service 
            确认某个服务在所有的webservers上都已经启动:
            $ ansible webservers -m service -a "name=httpd state=started"
            在所有的webservers上重启某个服务
            $ ansible webservers -m service -a "name=httpd state=restarted"
            确认某个服务已经停止:
            $ ansible webservers -m service -a "name=httpd state=stopped"
            使服务开机自启:
            $ ansible webservers -m service -a "name=httpd enabled=yes"


user  group:  创建或删除用户及组
archive    : 压缩文件或目录    path   dest    format
unarchive  : 解压  

synchronize
    需要系统rsync软件支持,如果没有,则yum安装    
    因为rsync传文件时也需要用户密码,所以最好配置免密登录。
    目的:将主控方/root/a目录推送到指定节点的/tmp目录下
        命令:ansible webservers  -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes'
        执行效果:
    delete=yes   使两边的内容一样(即以推送方为主)
    compress=yes  开启压缩,默认为开启
    --exclude=.git  忽略同步.git结尾的文件
    由于模块,默认都是推送push。因此,如果你在使用拉取pull功能的时候,可以参考如下来实现
    mode=pull   更改推送模式为拉取模式。将webservers 节点的/tmp/a目录拉取到主控节点的/root目录下命令:
    ansible webservers  -m synchronize -a 'mode=pull src=/tmp/a dest=/root/'
    ansible minis -m synchronize -a 'src=/root/tmp1/ dest=/root/tmp1/ compress=yes recursive=yes delete=yes'

cron   计划任务模块 (name对ansible生效,如果名字一样,则会修改)
    # ansible nginx -m cron -a 'name="ansible test1" job="/usr/bin/date >> /tmp/a.txt" minute=* hour=* day=22 month=4'
debug  调试使用
    # ansible nginx -m debug -a 'msg="haha  666"'
template:  可以渲染模板
    它和copy模块操作相同,只是在远程复制文件时会先填坑

YAML

YAML 是一门标记性语言,专门用来写配置文件的语言,非常简洁和强大,远比 JSON 格式方便。
YAML 语言(发音 /ˈjæməl/ )的设计目标,就是方便人类读写。它实质上是一种通用的数据串行化格式。

基本语法规则:
  1.大小写敏感
  2.使用缩进表示层级关系
  3.不允许使用TAB键来缩进,只允许使用空格键来缩进
  4.缩进的空格数量不重要,空格数量要一致,建议使用两个空格
  5.使用"#"来表示注释

YAML 支持的数据结构有三种。
对象(字典):键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
数组(列表):一组按次序排列的值,又称为序列(sequence) / 列表(list)
纯量(scalars):单个的、不可再分的值

YAML 还有一个小的怪癖. 所有的 YAML 文件(无论和 Ansible 有没有关系)开始行都应该是 ---. 这是 YAML 格式的一部分, 表明一个文件的开始.
后缀名最好为: yaml    yml

例1:第一个playbook脚本

cat first.yml

---
- hosts: nginx
  gather_facts: no
  tasks:
    - name: install ntpdate software
      yum: name=ntpdate state=present
    - name: sync time to ntp1.aliyun.com
      command: ntpdate -u ntp1.aliyun.com
    - name: use crontab sync time
      cron: name="sync time" job="/sbin/ntpdate -u ntp1.aliyun.com" minute=5
例2:假如有一台新服务器,需要对其初始化
    新服务器 -》 安装系统--> 配置防火墙 selinux --> 配置时区和时间同步  ---> 配置常用软件或公司内部的监控  --> 需要配置内部yum源
    安装系统,配置ip 一般可以使用cobbler或手工完成,接下来使用ansible
一个完整的系统初始化脚本
---
- hosts: nginx
  vars:
    ntp_server: ntp2.aliyun.com
  gather_facts: no
  tasks:
    - name: mkdir for backup old yum repo
      file: path=/etc/yum.repos.d/bak  state=directory
    - name: backup old yum repo
      raw: mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
    - name: config 163 yum repo
      copy: src=/etc/ansible/CentOS7-Base-163.repo dest=/etc/yum.repos.d/
    - name: make cache for 163 repo
      raw: yum clean all && yum makecache
    - name: disable selinux  (reboot manuel)
      copy: src=/etc/ansible/config dest=/etc/selinux/config
    - name: start firewalld
      service: name=firewalld state=restarted
    - name: enable firewalld when reboot
      service: name=firewalld enabled=yes
    - name: open 80 port
      command: firewall-cmd --add-service=http
    - name: set timezone
      command: timedatectl set-timezone  Asia/Shanghai
    - name: install ntpdate software
      yum: name=ntpdate state=present
    - name: sync time to {{ ntp_server }}
      command: ntpdate -u {{ ntp_server }}
    - name: use crontab sync time
      cron: name="sync time" job="/sbin/ntpdate -u {{ ntp_server }}" minute=5
使用变量,实例使用facts变量
---
- hosts: nginx
  tasks:
    - name: test variable facts
      debug: msg={{ ansible_default_ipv4.address }}
    - name: test variable facts
      debug: msg={{ ansible_default_ipv4["address"] }}

安装并配置nginx

---
- hosts: nginx
  gather_facts: no
  tasks:
    - name: install epel-release
      yum: name=epel-release state=present
    - name: install nginx
      yum: name=nginx state=present
      notify:
        - restart nginx
    - name: config nginx
      template: src=/etc/ansible/nginx.j2 dest=/etc/nginx/nginx.conf
      notify:
        - restart nginx
    - name: open port
      command: firewall-cmd --add-port {{ http_port }}/tcp
  handlers:
    - name: restart nginx
      service: name=nginx state=restarted
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值