Ansible Playbook

https://ansible-tran.readthedocs.io/en/latest/docs/playbooks_intro.html Ansible中文网址

  • 什么是Ansible Playbook
  • Playbook使用入门
  • Playbook实战

什么是Ansible Playbook

  • 是一门编程语言
  • 命令集合
  • YAML格式

功能列表

  • 声明配置
  • 编排复杂任务
  • 控制任务执行

与Adhoc关系

  • Playbook是对Adhoc的编排
  • Adhoc适合简单快速的任务
  • Playbook适合复杂异步的任务

支持的特性

  • 变量定义
  • 顺序结构
  • 选择结构
  • 循环结构
Playbook起步
  • 选定Host
  • 指定登录用户
  • 使用Shell模块输出Hello World

hosts行的内容是一个或多个组或主机patterns

remote_user就是帐号名:

---
- hosts: test
remote_user: root
tasks:
- name: hello world
shell: ls /root

Playbook基本结构

  • host: 被操作的机器的正则
  • remote_user: 登录机器的用户
  • tasks: 需要在机器上执行的任务

变量定义:

  • 以字母、数字以及下划线组成
  • 始终应该以字母开头

变量位置

  • Inventory

  • Playbook

    每一个 task 必须有一个名称 name,这样在运行 playbook 时,从其输出的任务执行信息中可以很好的辨别出是属于哪一ra个 task 的. 如果没有定义 name,‘action’ 的值将会用作输出信息中标记特定的 task.

    ---
    - hosts: test
    remote_user: root
    vars:
    com: ls /root
    tasks:
    - name: hello world
    shell: "{{ com }}"

    YAML语法要求如果值以{{ foo }}开头的话我们需要将整行用双引号包起来.这是为了确认你不是想声明一个YAML字典

系统变量

  • ansible hostname -m setup
  • {{ ansible_devices.sda.model }}
  • jinjia2模版

Ansible常用模块

  • ping 检查指定节点机器是否还能连通,主机如果在线,则回复pong
  • raw 执行原始的命令
  • yum RedHat和CentOS的软件包安装和管理工具。
  • apt Ubuntu/Debian的软件包安装和管理工具。
  • pip 用于管理Python库依赖项,为了使用pip模块,必须提供参数name或者requirements。
  • synchronize 使用rsync同步文件,将主控方目录推送到指定节点的目录下
  • template 基于模板方式生成一个文件复制到远程主机(template使用Jinjia2格式作为文件模版,进行文档内变量的替换的模块
  • copy 在远程主机执行复制操作文件
  • user 和group user模块是请求的是useradd, userdel, usermod三个指令,goup模块请求的是groupadd, groupdel, groupmod 三个指令
  • service 用于管理远程主机的服务
  • get_url 该模块主要用于从http、ftp、https服务器上下载文件(类似于wget)
  • fetch 它用于从远程机器获取文件,并将其本地存储在由主机名组织的文件树中
  • file 主要用于远程主机上的文件操作
  • lineinfile 远程主机上的文件编辑模块
  • unarchive 用于解压文件
  • command 和 shell 用于在各被管理节点运行指定的命令. shell和command的区别:shell模块可以特殊字符,而command是不支持
  • hostname 修改远程主机名的模块
  • script 在远程主机上执行主控端的脚本,相当于scp+shell组合
  • stat 获取远程文件的状态信息,包括atime,ctime,mtime,md5,uid,gid等信息
  • cron 远程主机crontab配置
  • mount 挂载文件系统
  • find 帮助在被管理主机中查找符合条件的文件,就像 find 命令一样
  • selinux 远程管理受控节点的selinux的模块

常用参数配置:

  • ansible_ssh_host # 目标主机地址
  • ansible_ssh_port #目标主机端口
  • ansible_ssh_user # 目标主机用户
  • ansible_ssh_pass #目标主机ssh密码
  • ansible_sudo_pass # sudo密码
  • ansible_sudo_exe
  • ansible_connection # 与主机的连接类型,比如:local, ssh,paramiko
  • ansible_ssh_private_key_file # 私钥地址
  • ansible_shell_type # 目标系统的shell类型
  • ansible_python_interpreter # python版本

when语句

tasks:
- name: "shutdown Debian flavored systems"
command: /sbin/shutdown -t now
when: ansible_os_family == "Debian"

bool值

vars:
epic: true tasks:
- shell: echo "This certainly is epic"
when: epic
- shell: echo "This certainly is epic"
when: not epic

with_items循环语句

- name: add several users
user: name={{ item }} state=present groups=wheel
with_items:
- testuser1
- testuser2

with_nested 嵌套循环

- name: users access control
mysql_user: name={{ item[0] }}
priv={{ item[1] }}.*:All
append_privs=yes
password=foo with_nested:
- ['alice','bob']
- ['clientdb','employeedb','providerdb']

有条件的循环

tasks:
- command: echo {{item}}
with_items: [0,2,4,6,8,10]
when: item > 5

Playbook实战

需求分析

  • python Flask开发环境
  • 具备数据库和缓存的功能
---
- hosts: test
remote_user: root
become: true # root用户可以省去这部
tasks:
- name: install python for centos
yum:
name: "{{ item }}"
state: installed
with_items:
- python-devel
- python-setuptools
when: ansible_distribution == 'CentOS'
- name: install python for ubuntu
apt:
name: "{{ item }}"
state: lastest
update_cache: yes
with_items:
- libpython-dev
- python-setuptools
when: ansible_distribution == 'Ubuntu'
- name: install pip
shell: easy_install pip
- name: pip install flask and redis
pip:
name: "{{ item }}"
with_items:
- flask
- redis

出现报错,报错信息写的很明白,版本问题在网上也没有找到答案,只能根据不同版本先这么写,后面再看看资料补充

[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a
loop to supply multiple items and specifying `name: "{{ item }}"`, please use `name: ['python-devel', 'python-setuptools']`
and remove the loop. This feature will be removed in version 2.11. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
ansible-playbook --version
ansible-playbook 2.8.4
---
- hosts: test
remote_user: root
become: true
tasks:
- name: install python for centos
yum:
name: ['python-devel','python-setuptools']
state: installed
when: ansible_distribution == 'CentOS'
- name: install python for ubuntu
apt:
name: ['libpython-dev','python-setuptools']
state: lastest
update_cache: yes
when: ansible_distribution == 'Ubuntu'
- name: install pip
shell: easy_install pip
- name: pip install flask and redis
pip:
name: ['flask','redis']

4.实战-Zabbix安装

zabbix安装需求

  • zabbix server 安装
  • Master和Client,Centos和Ubuntu各一个
  • Zabbix进程启动正常
---
- hosts: test
become: true
tasks:
- name: install zabbix rpm source
yum:
name: http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm
state: installed
when: ansible_distribution == 'CentOS'
- name: donwload ubuntu deb
get_url:
url: http://repo.zabbix.com/zabbix/3.4/ubuntu/pool/main/z/zabbix-release/zabbix-release_3.4-1+xenial_all.deb
dest: /tmp/zabbix.deb
when: ansible_distribution == 'Ubuntu'
- name: install zabbix source
apt:
deb: /tmp/zabbix.deb
when: ansible_distribution == 'Ubuntu'
- name: install centos zabbix package
yum:
name: ['zabbix-server-mysql','zabbix-proxy-mysql','zabbix-web-mysql']
state: installed
when: ansible_distribution == 'CentOS'
- name: install ubuntu zabbix package
yum:
name: zabbix-agent
update_cache: yes
when: ansible_distribution == 'Ubuntu'
- name: modify zabbix config
replace:
path: /etc/zabbix/zabbix_server.conf
regexp: DBUser=zabbix
replace: DBUser=root
when: ansible_distribution == 'CentOS'
- name: import zabbix sql table
shell: zcat /usr/share/doc/zabbix-server-mysql-3.4.7/create.sql.gz | mysql -uroot zabbix
when: ansible_distribution == 'CentOS'
- name: disable selinux
selinux:
state: disabled
when: ansible_distribution == 'CentOS'
- name: start zabbix-server
systemd:
name: zabbix-server
state: started
when: ansible_distribution == 'CentOS'
- name: start zabbix-agent
systemd:
name: zabbix-agent
state: started
when: ansible_distribution == 'CentOS'

自动化运维-Ansible-playbook的更多相关文章

  1. 自动化运维 Ansible

    自动化运维 Ansible 特性 (1).no agents:不需要在被管控主机上安装任何客户端: (2).no server:无服务器端,使用时直接运行命令即可: (3).modules in an ...

  2. 自动化运维Ansible安装篇

    Ansible自动化工具之--部署篇 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了 ...

  3. Linux centosVMware 自动化运维Ansible介绍、Ansible安装、远程执行命令、拷贝文件或者目录、远程执行脚本、管理任务计划、安装rpm包/管理服务、 playbook的使用、 playbook中的循环、 playbook中的条件判断、 playbook中的handlers、playbook实战-nginx安装、管理配置文件

    一.Ansible介绍 不需要安装客户端,通过sshd去通信 基于模块工作,模块可以由任何语言开发 不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读 安装十分简单,ce ...

  4. 自动化运维—Ansible(上)

    一:为什么选择Ansible 相对于puppet和saltstack,ansible无需客户端,更轻量级 ansible甚至都不用启动服务,仅仅只是一个工具,可以很轻松的实现分布式扩展 更强的远程命令 ...

  5. 服务器/网络/虚拟化/云平台自动化运维-ansible

    ansible与netconf的对比 首先明确一个概念,netconf是协议,ansible是python编写的工具 netconf 使用YANG建模,XML进行数据填充,使用netconf协议进行传 ...

  6. 自动化运维-ansible入门篇

    1.ansible配置 什么是Ansible IT自动化工具 依赖于现有的操作系统凭证来访问控制远程机器 简单易用.安全可靠 Ansible可以完成哪些任务 配置系统 开发软件 编排高级的IT任务 A ...

  7. 自动化运维--ansible(2)

    问题一:如何在多台服务器中配置Web项目上线的所有环境 解答: 1.使用ansible配置nginx服务 在安装前了解rpm与yum的区别  rpm是压缩包安装依赖包需要自己手动安装,yum安装解决依 ...

  8. 自动化运维Ansible之常用模块

    目录 0.Ansible模块语法 1.Command模块 2.Shell模块 3.Scripts模块 4.Copy模块 5.File模块 6.Yum模块 7.Service模块 8.Cron模块 9. ...

  9. Python自动化运维ansible从入门到精通

    1. 下载安装 在windows下安装ansible:

  10. 自动化运维--ansible(1)

    前戏 ansible 批量在远程主机上执行命令 openpyxl 操作excel表格 puppet ansible slatstack ansible epel源 第一步: 下载epel源 wget ...

随机推荐

  1. LeetCode 108. Convert Sorted Array to Binary Search Tree (将有序数组转换成BST)

    108. Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascendin ...

  2. Python - Django - 中间件 process_exception

    process_exception(self, request, exception) 函数有两个参数,exception 是视图函数异常产生的 Exception 对象 process_except ...

  3. EasyNVR网页摄像机无插件H5、谷歌Chrome直播方案使用详情功能-通道配置Excel导入导出

    使用EasyNVR的用户都有知道,由于EasyNVR是将设备与EasyNVR的通道进行绑定的,因此EasyNVR是通过手动的通道配置来进行设备接入的,这样可以做到将设备的和通道对应的接入.但是,如果手 ...

  4. [ ceph ] 基本介绍及硬件配置

    1. Ceph简介 所有的 Ceph 存储集群的部署都始于一个个 Ceph节点.网络和 Ceph存储集群.Ceph 存储集群至少需要一个 Ceph Monitor.一个 Manager和一个Ceph ...

  5. [xsy3553]游戏

    题意:交互题,交互库有长为$n$的$01$串$S$,你可以用字符串$T$询问$\sum\limits_{i=1}^n[S_i=T_i]$,要求用$1030$次询问问出$S$,$n=5000$ 首先我们 ...

  6. Redis Sentinel 高可用部署实践集群

    一.Redis Sentinel 介绍    1.Sentinel     数据库环境搭建,从单机版到主备.再到多数据库集群,我们需要一个高可用的监控:比如Mysql中,我们可能会采用MHA来搭建我们 ...

  7. 解决vue项目在ie浏览器缓存问题。

    ie浏览器一直是程序员的噩梦.项目在谷歌浏览器上完美运行.在ie浏览器上,缓存问题真心恶心.后台查看了资料说在接口上加上时间戳或随机数就行了.要是这样干,工作量真心大啊.后来我对我们公司大神封装的ax ...

  8. Openresty 健康检查

    ## 指定共享内存 lua_shared_dict healthcheck 1m; ## 在worker初始化过程中,启动定时器,进行后端结点的检查 init_worker_by_lua_block ...

  9. 【剑指offer】数组在排序数组中出现的次数

    题目描述 统计一个数字在排序数组中出现的次数. 分析:数组有序,采用二分查找无疑 两种方法,时间复杂度差不多,都是利用二分查找,不过统计k出现的次数有所不同而已 方法1:二分查找k,找到任意一个k的下 ...

  10. springboot异步线程(二)

    前言 本篇文章针对上篇文章springboot异步线程,有一位大佬在评论中提出第一点是错误的,当时看到了这个问题,最近刚好有空,针对第一点的问题去搜索了不少的文章: 问题 我在文章中第一点去验证:Sc ...