Ansible-笔记

Ansible-学习笔记

简介:
  • Ansible—自动化运维工具,通过ssh协议实现配置管理、应用部署、任务执行等功能。
  • ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。
Ansible架构

Ansible工作原理

Ansible主要组成部分
  • ANSIBLE PLAYBOOKS:任务剧本(任务集),编排定义Ansible任务集的配置文件,由Ansible顺序依次执行,通常是JSON格式的YML文件
  • INVENTORY:Ansible管理主机的清单/etc/anaible/hosts
  • MODULES:Ansible执行命令的功能模块,多数为内置的核心模块,也可自定义
  • PLUGINS:模块功能的补充,如连接类型插件、循环插件、变量插件、过滤插件等,该功能不常用
  • API:供第三方程序调用的应用程序编程接口
  • ANSIBLE:组合INVENTORY、 API、 MODULES、 PLUGINS的绿框,可以理解为是ansible命令工具,其为核心执行工具
利用ansible实现管理的方式:
  • Ad-Hoc 即ansible命令,主要用于临时命令使用场景
  • Ansible-playbook 主要用于长期规划好的,大型项目的场景,需要有前提的规划
Ansible-playbook(剧本)执行过程:
  • 将已有编排好的任务集写入Ansible-Playbook
  • 通过ansible-playbook命令分拆任务集至逐条ansible命令,按预定规则逐条执行
安装
  • rpm包安装: EPEL源
yum install ansible
复制代码
  • 编译安装
yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto
tar xf ansible-1.5.4.tar.gz
cd ansible-1.5.4
python setup.py build
python setup.py install
mkdir /etc/ansible
cp -r examples/* /etc/ansible 
复制代码
  • Git方式:
git clone git://github.com/ansible/ansible.git --recursive
cd ./ansible
source ./hacking/env-setup
复制代码
  • pip安装: pip是安装Python包的管理器,类似yum
yum install python-pip python-devel
yum install gcc glibc-devel zibl-devel rpm-bulid openssl-devel
pip install --upgrade pip
pip install ansible --upgrade
复制代码
  • 确认安装: ansible --version
相关文件
配置文件
/etc/ansible/ansible.cfg 主配置文件,配置ansible工作特性
/etc/ansible/hosts 主机清单
/etc/ansible/roles/ 存放角色的目录(可自定义)
复制代码
程序
/usr/bin/ansible 主程序,临时命令执行工具
/usr/bin/ansible-doc 查看配置文档,模块功能查看工具
/usr/bin/ansible-galaxy Roles模块的官网平台
/usr/bin/ansible-playbook 定制自动化任务,编排剧本工具(常用)
/usr/bin/ansible-pull 远程执行命令的工具
/usr/bin/ansible-vault 文件加密工具
/usr/bin/ansible-console 基于Console界面与用户交互的执行工具
复制代码
主机清单Inventory
/etc/ansible/hosts  #默认的inventory file,inventory file可以有多个
inventory文件遵循INI文件风格,中括号中的字符为组名。可以将同一个主机同时归并到多个不同的组中;此外,若目标主机使用了非默认的SSH端口,还可以在主机名称之后使用冒号加端口号来标明
ntp.fool.com
[websrvs]
www1.abc.com:2333
www2.xyz.com
[dbsrvs]
db1.bigdata.com
复制代码
ansible配置文件
/etc/ansible/ansible.cfg (一般保持默认)
[defaults]
#inventory = /etc/ansible/hosts # 主机列表配置文件
#library = /usr/share/my_modules/ # 库文件存放目录
#remote_tmp = $HOME/.ansible/tmp #临时py命令文件存放在远程主机目录
#local_tmp = $HOME/.ansible/tmp # 本机的临时命令执行目录
#forks = 5 # 默认并发数
#sudo_user = root # 默认sudo 用户
#ask_sudo_pass = True #每次执行ansible命令是否询问ssh密码
#ask_pass = True
#remote_port = 22
#host_key_checking = False # 检查对应服务器的host_key,建议取消注释
#log_path=/var/log/ansible.log #日志文件
复制代码
ansible命令
ansible-doc: 显示模块帮助
ansible-doc [options] [module...]
-a 显示所有模块的文档
-l, --list 列出可用模块
-s, --snippet 显示指定模块的playbook片段
复制代码
ansible命令执行过程
ansible命令执行过程
1. 加载自己的配置文件 默认/etc/ansible/ansible.cfg
2. 加载自己对应的模块文件,如command
3. 通过ansible将模块或命令生成对应的临时py文件,并将该 文件传输至远程服务器的对应执行用户$HOME/.ansible/tmp/ansible-tmp-数字/XXX.PY文件
4. 给文件+x执行
5. 执行并返回结果
6. 删除临时py文件,sleep 0退出
执行状态:
绿色:执行成功并且不需要做改变的操作
黄色:执行成功并且对目标主机做变更
红色:执行失败
复制代码
ansible常用模块

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

ansible all -m command -a ‘service vsftpd start’
ansible all -m command -aecho user1 |passwd --stdin 12345678’ #fail
#此命令不支持 $VARNAME < > | ; & 等
复制代码

Shell:和command相似,用shell执行命令

ansible all -m shell -aecho user1 |passwd --stdin 12345678’ #success
#与本地运行shell差不多
复制代码

Script:运行脚本

ansible websrvs -m script -a f1.sh 
复制代码

Copy:从服务器复制文件到客户端

ansible all -m copy -a “src=/root/f1.sh dest=/tmp/f2.sh owner=user1 mode=600
backup=yes”
如目标存在,默认覆盖,此处指定先备份
ansible all -m copy -a “content=‘test content\n’ dest=/tmp/f1.txt” 利用内容,直接生成
目标文件
复制代码

Cron:计划任务。 支持时间:minute,hour,day,month,weekday

ansible all -m cron -a “minute=*/5 job=‘/usr/sbin/ntpdate 172.16.0.1 &>/dev/null’
name=Synctime” 创建任务
ansible all -m cron -a ‘state=absent name=Synctime’ 删除任务 
复制代码

Fetch:从客户端取文件至服务器端,copy相反,目录可先tar

ansible websrvs -m fetch -a ‘src=/root/a.sh dest=/data/scripts’
复制代码

File:设置文件属性

ansible websrvs -m file -a "path=/root/a.sh owner=user1 mode=755“
ansible websrvs -m file -a ‘src=/app/testfile dest=/app/testfile-link state=link’
复制代码

Hostname:管理主机名

ansible node1 -m hostname -a “name=websrvs”
复制代码

Yum:管理包

ansible websrvs -m yum -a ‘name=httpd state=latest’ 安装
ansible websrvs -m yum -a ‘name=httpd state=absent’ 删除 
复制代码

Service:管理服务

ansible websrvs -m service -a 'name=httpd state=stopped'
ansible websrvs -m service -a 'name=httpd state=started'
ansible websrvs –m service –a ‘name=httpd state=reloaded’
ansible websrvs -m service -a 'name=httpd state=restarted'
复制代码

User:管理用户

ansible test -m user -a 'name=user1 comment=“test user” uid=1234 home=/app/user1 group=root‘
ansible test -m user -a 'name=sysuser1 system=yes home=/app/sysuser1'
ansible test -m user -a ‘name=user1 state=absent remove=yes' 删除用户及家目录等数据
复制代码

Group:管理组

ansible test -m group -a "name=testgroup system=yes“
ansible test -m group -a "name=testgroup state=absent"
复制代码
ansible-galaxy 命令
  • 连接 https://galaxy.ansible.com 下载相应的roles
#列出所有已安装的galaxy
ansible-galaxy list
#安装galaxy
ansible-galaxy install geerlingguy.redis
#删除galaxy
ansible-galaxy remove geerlingguy.redis 
复制代码
Ansible-playbook

示例1:

ansible-playbook hello.yml
cat hello.yml
#hello world yml file
- hosts: websrvs
  remote_user: root
  
  tasks:
    - name: hello world
      command: /usr/bin/wall hello world
复制代码

示例2:

Playbook定义
---
- hosts: all
tasks:
- name: "安装Apache"
command: yum install -q -y httpd
- name: "复制配置文件"
command: cp /tmp/httpd.conf /etc/httpd/conf/httpd.conf
command: cp /tmp/httpd-vhosts.conf /etc/httpd/conf/httpd-vhosts.conf
- name: "启动Apache,并设置开机启动"
service: name=httpd state=started enabled=yes
复制代码

shell脚本实现上面的操作

SHELL脚本
#!/bin/bash
# 安装Apache
yum install httpd-y
# 复制配置文件
cp /path/to/config/httpd.conf /etc/httpd/conf/httpd.conf
cp/path/to/httpd-vhosts.conf  /etc/httpd/conf/httpdvhosts.conf
# 启动Apache,并设置开机启动
service httpd start
chkconfig httpd on
复制代码
roles
tasks]# pwd&&ll
/root/ansible/roles/nginx/tasks
total 28
-rw-r--r--. 1 root root  52 Nov  7 13:55 group.yml
-rw-r--r--. 1 root root 148 Nov  7 14:52 main.yml
-rw-r--r--. 1 root root  66 Nov  7 13:58 restart.yml
-rw-r--r--. 1 root root  74 Nov  7 13:57 start.yml
-rw-r--r--. 1 root root  79 Nov  7 14:01 templ.yml
-rw-r--r--. 1 root root  93 Nov  7 13:54 user.yml
-rw-r--r--. 1 root root  46 Nov  7 13:56 yum.yml

复制代码
ansible]# cat nginx_role.yml 
---
- hosts: websrvs
  remote_user: root
  roles:
    - role: nginx

复制代码
roles]# tree
.
└── nginx
    ├── tasks
    │   ├── group.yml
    │   ├── main.yml
    │   ├── restart.yml
    │   ├── start.yml
    │   ├── templ.yml
    │   ├── user.yml
    │   └── yum.yml
    └── templates
        └── nginx.conf.j2
复制代码
nginx]# cat tasks/*
---
- name: create group
  group: name=nginx gid=80
---
- include: group.yml
- include: user.yml
- include: yum.yml
- include: templ.yml
- include: start.yml
- include: roles/httpd/tasks/copyfile.yml
---
- name: restart service
  service: name=nginx state=restarted
---
- name: start service
  service: name=nginx state=started enabled=yes
---
- name: copy conf
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
---
- name: create user
  user: name=nginx uid=80 group=nginx system=yes shell=/sbin/nologin
---
- name: install package
  yum: name=nginx
复制代码
nginx]# cat templates/nginx.conf.j2
...
nginx 配置文件。该文件作为模版,用在被控主机上
复制代码
[root@localhost ansible]# pwd&&ll&&ansible-playbook nginx_role.yml
/root/ansible
total 32
-rw-r--r--. 1 root root  24 Nov  7 15:39 app.retry
-rw-r--r--. 1 root root  62 Nov  7 15:28 app.yml
drwxr-xr-x. 7 root root  72 Nov  7 20:46 bak
-rw-r--r--. 1 root root  24 Nov  7 14:41 httpd_role.retry
-rw-r--r--. 1 root root  64 Nov  7 14:40 httpd_role.yml
-rw-r--r--. 1 root root  67 Nov  7 16:05 memcached_role.yml
-rw-r--r--. 1 root root  24 Nov  7 14:53 nginx_role.retry
-rw-r--r--. 1 root root  68 Nov  7 14:04 nginx_role.yml
drwxr-xr-x. 3 root root  19 Nov  7 20:46 roles
drwxr-xr-x. 8 root root  85 Nov  7 20:45 roles.bak
-rw-r--r--. 1 root root 173 Nov  7 14:59 some_role.yml
...
PLAY [websrvs] *****************************************************************
...
TASK [Gathering Facts] *********************************************************
...
PLAY RECAP *********************************************************************
复制代码

感谢:mr·wang && http://www.ansible.com.cn/index.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值