zoukankan      html  css  js  c++  java
  • ansible剧本之playbook操作

    ansible 剧本

    yaml介绍:

    是一个编程语言
    文件后缀名 yaml yml
    数据对应格式:
    字典: key: value
    列表: [] -
    

    ansible-playbook命令格式

    执行顺序: 从上往下

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

    #命令格式:ansible-playbook [options] playbook.yml [playbook2 ...] 
    #参数:
    -C, --check # 检查,白跑,干跑
    -f FORKS, --forks=FORKS #用来做并发
    --list-hosts # 列出主机列表
    --syntax-check # 语法检查 
    -e #指定参数
    
     
    
    

    简单用法

    #vi p1.yml 写入如下内容:
    
    - hosts: web #主机列表
    tasks: #任务
    - name: creategroup #任务名称
    group: name=alex10 #模块名:参数
    - name: cretaeuser #任务名称
    user: name=wusir10 #模块名:参数
    
    
    #检查p1.yml语法的正确性
    [root@localhost ~]# ansible-playbook --syntax-check p1.yml
    
    playbook: p1.yml
    #执行
    [root@localhost ~]# ansible-playbook p1.yml
    
    
    
    hosts: gb
    tasks:
    - name: 第san个姑娘
    dong: 第san个姑娘
    
    

    传参

    - 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文件
    

    setup查看配置参数

    [root@localhost ~]# ansible cache -m setup #查看cache组的配置
    ansible_all_ipv4_addresses # ipv4的所有地址
    ansible_all_ipv6_addresses # ipv6的所有地址
    ansible_date_time # 获取到控制节点时间
    ansible_default_ipv4 # 默认的ipv4地址
    ansible_distribution # 系统
    ansible_distribution_major_version # 系统的大版本
    ansible_distribution_version # 系统的版本号
    ansible_domain #系统所在的域
    ansible_env #系统的环境变量
    ansible_hostname #系统的主机名
    ansible_fqdn #系统的全名
    ansible_machine #系统的架构
    ansible_memory_mb #系统的内存信息
    ansible_os_family # 系统的家族
    ansible_pkg_mgr # 系统的包管理工具
    ansible_processor_cores #系统的cpu的核数(每颗)
    ansible_processor_count #系统cpu的颗数
    ansible_processor_vcpus #系统cpu的总个数=cpu的颗数*CPU的核数
    ansible_python # 系统上的python
    #命令:
    ansible cache -m setup -a 'filter=*processor*' # 用来搜索
    
    

    条件判断

    适用于以下情况

    • 不同的系统
    • 不同的版本
    • 不同的环境
    • 不同的用户
    #y2.yml文件内容如下:
    - hosts: db
    remote_user: root
    tasks:
    - name: createfile
    copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt
    when: a=="3"
    - name: cratefile
    copy: content="小弦切切如私语" dest=/tmp/a.txt
    when: a=="4"
    #执行命令
    [root@localhost yaml]# ansible-playbook -e 'a="3"' p2.yml
    

    Ubuntu 安装包的方式是apt-get

    tags 单独执行某一条指令时

    - hosts: web
    tasks:
    - name: installnginx
    yum: name=nginx
    - name: copyfile
    copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
    tags: copyfile
    - name: start
    service: name=nginx state=started
    
    
    ansible-playbook -t copyfile p7.yml 
    

    循环 with_item

    一次性创建多个

    - hosts: web
    tasks:
    - name: crateuser
    user: name={{item}}
    with_items:
    - feng20
    - feng21
    - feng22
    ~ 
    
    - hosts: web
    tasks:
    - name: crateuser
    user: name={{item}}
    with_items:
    - feng30
    - feng31
    - feng32
    - name: crategroup
    group: name={{item}}
    with_items:
    - wulaoshi20
    - wulaoshi21
    - wulaoshi22
    ~ 
    

    嵌套循环

    - hosts: web
    tasks:
    - name: crategroup
    group: name={{item}}
    with_items:
    - wulaoshi30
    - wulaoshi31
    - wulaoshi32
    - name: createuser
    user: name={{item.name}} group={{item.group}}
    with_items:
    - {'name':feng40,'group':wulaoshi30}
    - {'name':feng41,'group':wulaoshi31}
    - {'name':feng42,'group':wulaoshi32}
    
    

    template:

    jinja2

    - hosts: web
    tasks:
    - name: installredis
    yum: name=redis
    - name: copyfile
    template: src=/etc/redis.conf dest=/etc/redis.conf
    - name: start
    service: name=redis state=started
    配置文件: bind {{ ansible_default_ipv4.address }} 
    

    copy和tamplate的区别

    • copy模块不替代参数
    • template模块替代参数
    - hosts: web
    tasks:
    - name: installredis
    yum: name=redis
    - name: copyfile
    template: src=redis.conf dest=/etc/redis.conf
    - name: start
    service: name=redis state=started
    

    ps:写相对路径: 在当前目录下新建一个templates目录,然后把redis.conf配置文件放在templates目录里面

    handlers

    修改配置文件,自动触发执行

    - hosts: web
    tasks:
    - name: installredis
    yum: name=redis
    - name: copyfile
    template: src=redis.conf dest=/etc/redis.conf
    tags: copyfile
    notify: restart
    - name: start
    service: name=redis state=started
    handlers:
    - name: restart    #此name名称和notify后面的名称保持一致
    service: name=redis state=restarted
    #命令:
    [root@localhost yaml]# ansible-playbook -t copyfile p7.yml
    
    
  • 相关阅读:
    windows phone 独立存储空间的操作 (2)(转)
    Windows Phone笔记(9)使用独立存储(上)(转)
    Windows Phone笔记(8)页面间数据共享
    windows phone 使用相机并获取图片(3)(转)
    Windows Phone笔记(11)使用独立存储(下)
    Windows Phone笔记(7)页面间导航以及数据传递(转)
    Windows Phone笔记(6)使用地图服务
    Windows Phone笔记(12)XAML基础知识(转)
    Mysql基础知识
    C语言中 数组作为函数形参传递相当于指针,在函数中不能得到数组长度,只能得到指针长度4
  • 原文地址:https://www.cnblogs.com/fengchong/p/10436890.html
Copyright © 2011-2022 走看看