3天学习 Ansible

Mar 5, 2019 15:30 · 1635 words · 4 minute read Linux DevOps Ansible

把自己的笔记整理了下,其实一天也勉强够。

首先要知道什么是 Ansible,Google 一下:

Ansible is the simplest way to automate apps and IT infrastructure. Application Deployment + Configuration Management + Continuous Delivery.

官方给出的定义简单明了:

  • automate 自动化
  • application deployment 应用部署
  • continuous delivery 持续交付

相关视频

相关文档

常用的自动化运维工具

  • Ansible:基于 Python 开发,无代理,中小型应用环境
  • Saltstack:Python,需要部署代理,执行效率更高
  • Puppet:Ruby,配置复杂,重型,适合大型环境
  • Fabric:Python,无代理

1. 主要组件

  • playbooks:任务脚本,YAML 文件
  • inventory:管理的主机清单 /etc/ansible/hosts
  • modules:执行命令的功能模块,多数为内置核心模块
  • plugins:模块功能的补充,如连接类型插件、循环插件、变量插件、过滤插件,不常用
  • API:供第三方程序调用

实现管理的方式

  • Ad-Hoc:单条命令,临时使用
  • Ansible-playbook:长期规划,用于大型项目场景
    • 将已有的编排好的任务集写入 playbook
    • 通过 ansible-playbook 命令分拆任务集至逐条 ansible 命令,按预定规则逐条执行

所以 Ansible 本身只是一套命令行工具,主要的知识点在于常用模块的使用(我觉得就是查 help 文档 + 背一些选项用法)和 playbook 的编写(YAML 语法),并不是语言,也不是 Express、Vue 这样的框架,而且有足够详细的资料,对自学友好。

2. 学习环境

有个问题,如果用 Homebrew 安装 Ansible,不会自动创建 /etc/ansible 路径,要自己来创建。

几个 Ubuntu/CentOS Linux 虚拟机实例当做服务器节点

3. 相关文件

  • /etc/ansible/ansible.cfg 主配置文件,配置 Ansible 工作特性

  • /etc/ansible/hosts 主机清单

  • /etc/ansible/role/ 存放角色的路径

  • ansible 主程序

  • ansible-doc 查看配置文档

  • ansible-playbook 定制自动化任务

主机清单

路径:/etc/ansible/host

example: https://github.com/ansible/ansible/blob/devel/examples/hosts

官方文档: https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html

mail.example.com

[websvrs] # websvrs 组
foo.example.com
bar.example.com

[dbsvrs] # dbsvrs 组
one.example.com
two.example.com
three.example.com

相似的主机名甚至可以这么写:

[websvrs]
www[01:50].example.com

[dbsvrs]
db-[a:f].example.com

Inventory 参数说明

Ansible 配置文件

路径:/etc/ansible/ansible.cfg

example: https://github.com/ansible/ansible/blob/devel/examples/ansible.cfg

#library        = /usr/share/my_modules/ # 库文件存放目录
#remote_tmp     = ~/.ansible/tmp
#local_tmp      = ~/.ansible/tmp
#forks          = 5 # 默认并发数
#sudo_user      = root # 默认 root 用户
##ask_sudo_pass = True # 每次执行 ansible 命令是否询问 ssh 密码

Ansible 将指令在 ~/.ansible/tmp 目录中生成为脚本,复制到远程的被控制主机的 ~/.ansible/tmp 目录。

配置字段说明

4. Ansible 命令

ansible <host-pattern> [options]

  • -m 指定模块,默认为 command
  • –list-hosts 列出主机列表
  • -k 提示输入 ssh 连接密码
  • -K 提示输入 sudo 操作时的口令
  • -u 执行远程操作的用户
  • -b sudo 切换

host-pattern

  • all 所有主机
  • &*; 通配符
  • : 或关系

ansible-doc 显示模块帮助(非常有用)

ansible-doc [options] [module...]

  • -a 显示所有模块的文档
  • -l 列出可用模块
  • -s 显示指定模块的 playbook 片段
$ ansible-doc -l | wc -l
2078

统计了一下目前总共两千多个模块。。。

命令执行过程

  1. 加载配置 ansible.cfg
  2. 加载模块
  3. 生成对应的临时 .py 文件并传输至远程服务器
  4. 执行并返回结果
    • 绿色:成功但不修改目标主机
    • 黄色:成功并更新目标主机
    • 红色:失败
  5. 删除临时文件,sleep 0退出
$ ansible all -m ping -vvv

带上 -v-vv-vvv 参数可以查看命令的详细执行过程

5. 常用模块

command 在远程主机执行命令,默认模块,可忽略 -m 选项

$ ansible all -a 'df -h'

带管道、变量、重定向的命令执行会有问题,要用 shell 模块实现!

shell

$ ansible all -m shell -a 'echo $HOSTNAME'

command 支持的命令 shell 都支持

script 运行脚本

$ ansible all -m script -a 'path/to/script_file.sh'

copy 从控制端复制文件到服务器

$ ansible all -m copy -a 'src=path/to/src dest=path/to/dest'

fetch 从服务器拉取文件到控制端

单个文件,不能是目录

$ ansible all -m fetch -a 'src=path/to/src dest=path/to/dest'

如果要拉取某个路径下的所有文件,先打包后再拉取。

archive

file

  • 创建文件 touch

    $ ansible all -m file -a 'name=path/to/new_file state=touch'
    
  • 删除文件 absent

    $ ansible all -m file -a 'name=path/to/file state=absent'
    
  • 创建目录 directory

    $ ansible all -m file -a 'name=path/to/dir state=directory'
    
  • 删除目录 absent

    $ ansible all -m file -a 'name=path/to/dir state=absent'
    
  • 创建软链接 link

    $ ansible all -m file -a 'src=path/to/src dest=path/to/dest.link state=link'
    
  • 删除软链接 absent

    $ ansible all -m file -a 'dest=path/to/dest.link state=absent'
    

hostname

一台一台改。。。

$ ansible <ip> -m hostname -a 'name=new_hostname'

apt 适用于 Ubuntu

$ ansible all -m apt -a 'name=pkg_name state=present'

service 管理服务

$ ansible srv -m services -a 'name=httpd state=stopped'
$ ansible srv -m services -a 'name=httpd state=started'
$ ansible srv -m services -a 'name=httpd state=reloaded'
$ ansible srv -m services -a 'name=httpd state=restarted'

user 管理用户

  • 创建用户

    $ ansible all -m user -a 'name=nginx shell=/sbin/nologin system=yes groups=root,bin'
    
  • 删除用户

    $ ansible all -m user -a 'name=nginx state=absent remove=yes'
    

group 管理组

  • 创建组

    $ ansible all -m group -a 'name=group_name system=yes'
    
  • 删除组

    $ ansible all -m group -a 'name=group_name state=absent'
    

6. Playbook

playbook 相当于脚本,YAML 语法

example: https://github.com/ansible/ansible-examples

  • playbook 是由多个 play 组成的列表
  • play 是任务的集合,而任务是对 ansible 模块的调用的集合
---
- hosts: all
  remote_user: root

  tasks:
    - name: hello
      command: hostname
  • 检查 playbook:

    $ ansible-playbook -C path/to/playbook.yml
    
  • 运行 playbook:

    $ ansible-playbook path/to/playbook.yml
    

核心元素

  • hosts 执行的远程主机列表
  • tasks 任务集
  • vars 变量
  • templates 模版
  • handlers 和 notify 结合使用,由特定条件触发

详细使用说明