• ansible的playbook剧本


    如果用模块形式一般有幂等性,如果用shell或者command没有幂等性
    playbooks相当于shell的脚本,可以吧执行的任务写到文件当一次执行,方便调用
    tasks:一个task相当于是一个play
    varobles:变量,一个定义,多次调用
    template:模板。可以区分不同主机的特点
    handlers:触发器,依赖于前一个任务,前一个任务如果执行改变,那么就会触发handlers
    1.vim /etc/ansible/hosts
    [zxw]
    192.168.126.7 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=123
    192.168.126.6 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=123
    ~

    基本格式

    - hosts: zxw
    remote_user: root
    tasks:
    - name: copy httpd.conf
    copy: src=name dest=name
    - name: restarted httpd
    service: name=httpd state=restarted

    Vim name.yaml
    - hosts: zxw 定义的组 (IP,all,组名)
    remote_user: root 主机执行
    tasks: 任务
    - name: copy httpd.conf 任务名字
    copy: src=/etc/httpd/conf/httpd.conf 模块 本地文件 dest=/etc/httpd/conf/httpd.conf 目标文件
    - name: restarted httpd
    service: name=httpd state=restarted
    [root@zxw8 ~]# ansible-playbook host.yaml

    192.168.126.7 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    执行3个| 修给2个| 不可达=0 | 失败=0| 跳过=0| 获救=0| 忽略=0

    定义变量:
    第一种:
    - hosts: zxw
    remote_user: root
    vars:
    - file: /etc/httpd/conf/httpd.conf
    tasks:
    - name: copy httpd.conf
    copy: src={{ file }} dest={{ file }}
    - name: restarted httpd
    service: name=httpd state=restarted
    第二种:
    vim /etc/ansible/hosts
    [zxw:vars]
    file= /etc/httpd/conf/httpd.conf
    packages=tree2
    第三种:
    [root@zxw8 ~]# ansible-playbook host.yaml --extra-vars "file=/etc/httpd/conf/httpd.conf "

    注册变量:
    register注册变量:把date命令输出的结果赋予给date_output

    - hosts: zxw
    remote_user: root
    tasks:
    - name: get date
    command: date
    register: date_output
    - name: echo date_output
    shell: "echo {{date_output.stdout}} > /root/zxw.txt"
    [root@zxw8 ~]# ansible-playbook date.yaml

    when语句:
    when条件语句:可以根据setup显示出客户端信息为依据来判断
    [root@zxw8 ~]# ansible zxw -m setup | more
    列出客户端详细信息
    - hosts: zxw
    remote_user: root
    tasks:
    - name: mkdir when
    command: 'mkdir when'
    when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"

    异常处理:
    ignore_errors:如果任务出错,直接跳过,不会影响其他任务

    - hosts: zxw
    remote_user: root
    tasks:
    - name: touch zhao
    command: 'touch zhao'
    ignore_errors: yes
    - name: restarted httpd
    service: name=httpd state=restarted
    ignore_errors: yes

    循环语句:
    第一种:
    {{ item }}:循环创建
    创建
    - hosts: zxw
    remote_user: root
    tasks:
    - name: add many users
    user: name={{ item }} state=present
    with_items:
    - user1
    - user2
    - user3
    删除:
    user: name={{ item }} state=absent
    - hosts: zxw
    remote_user: root
    tasks:
    - name: yum many rujian
    yum: name={{ item }} state=latest
    with_items:
    - samba
    - vsftpd
    - nfs-utils
    - rpcbind


    第二种:
    - hosts: 192.168.254.12
    remote_user: root
    tasks:
    - name: add several user
    user: name={{item.name}} state=present groups={{item.groups}}
    with_items:
    - { name: 'testuser1', groups: 'wheel'}
    - { name: 'testuser2', groups: 'root'}
    触发器:
    handlers:如果执行的任务被改变那么会触发handlers的任务
    - hosts: zxw
    remote_user: root
    tasks:
    - name: copy httpd.conf
    copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify:
    - service httpd restarted
    handlers:
    - name: service httpd restarted
    service: name=httpd state=restarted

    模板拷贝:
    template,用来区分不同客户端上的特性
    - hosts: zxw
    remote_user: root
    tasks:
    - name: copy httpd.conf
    template: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify:
    - service httpd restarted
    handlers:
    - name: service httpd restarted
    service: name=httpd state=restarted
    [root@zxw8 ~]]# cat /etc/ansible/hosts
    [zxw]
    192.168.126.7 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=123 prot=80
    192.168.126.6 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=123 prot=80


    [root@zxw8 ~]# cat /etc/redhat-release 查看版本
    1 .对剧本语法检测:
    ansible-playbook --syntax-check  /root/ansible/httpd.yaml
     
    2.-C模拟执行剧本:
    ansible-playbook  -C /root/ansible/httpd.yaml
     
    3.执行剧本:
    ansible-playbook   /root/ansible/httpd.yaml
     

    示例1:基础

     

     


    示例2:变量

     

     


    示例3:迭代

     

     


     示例4:触发器notify

     

    示例5:模板templates

     

     

     

  • 相关阅读:
    <亲测>CentOS7yum安装PHP7.2
    linux开机出现一下错误Give root password for maintenance (or type Control-D to continue):
    解决js输出汉字乱码问题
    <亲测>.NET Core项目在Linux上使用QRCoder时出错"Unable to load DLL 'gdiplus'"
    <亲测>阿里云centos7 挂载数据盘配置
    centos 磁盘清理 /dev/vda1系统盘满了
    两种方式:mysql查看正在执行的sql语句
    adb调试android设备 说的比较清楚的一篇文章
    <亲测>window+iis+letsencrypt+ssl 手动续期(通配符域名)
    申请免费通配符证书(Let's Encrypt)并绑定IIS
  • 原文地址:https://www.cnblogs.com/itzhao/p/11262016.html
Copyright © 2020-2023  润新知