Ansible是2013年推出的一种通用自动化工具,可用于配置管理或工作流程自动化。配置管理是一种“基础架构代码”实践,它将事物编码,例如应该在系统上安装什么包和版本,或者应该运行什么守护进程。工作流自动化可能是从配置基础架构到部署软件的任何事情。Ansible在2015年时被Redhat公司收购。
  Ansible是用Python编写的,它使用SSH在不同的机器上执行命令。Ansible是无代理的,这使得入手更容易。您只需要在相关机器上安装SSH访问和Python。Ansible使用声明式YML"playbook"
将一组主机(从“hosts”)映射到定义明确的角色。声明性用于指示Ansible如何设置或更改事物,Ansible才进行必要的更改。

  200-500台服务器,用ansible。更多的则使用saltstack

  ansible 无需安装客户端,依赖ssh服务。 -->ssh 认证

ansible 部署

安装ansible

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

#ansible 管理端
yum install ansible -y
yum install libselinux-python -y

#backup nfs01 被管理端
yum install libselinux-python -y
[root@m01 ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg   # ansible的配置文件
├── hosts       # ansible管理了 哪些服务器 服务器列表
└── roles      # 角色 [root@m01 ~]# cat /etc/ansible/hosts
[lewen]
172.16.1.31
172.16.1.41

ansible lewen -m command -a "hostname"
ansible lewen -m command -a "yum install cowsay -y"

-m 后边是模块的名字
-m MODULE_NAME,--module-name=MODULE_NAME module name to execute(default=command).

-a 后面是要执行的命令  -a MODULE_ARGS,-args=MODULE_ARGS. module arguments.

复制文件

利用ansible远程批量拷贝文件或目录。
语法:
ansible lewen -m copy -a "sre=/etc/passwd dest=/tap/oldgirl.txt owner=lewen group=lewen sode=0755"
注意:
1)如果指定的目标目录不存在,系统会自动创建,否则源目录会放到目标目录下面去:
2)如果copy的是文件,dest指定的名字和源如果不同,并且它不是已经存在的目录,相当于copy过去后再重命名:
3)若果dest是目标机器上已经存在的目录,则会直接把文件copy 到该目录下面。
4)设定的用户和组lewen在所有客户端必须存在。

[root@m01 ~]# ansible lewen -m copy -a "src=/etc/hosts dest=/tmp owner=lewen mode=0755"  # backup=yes 已存在的文件就复制备份
172.16.1.41 | SUCCESS => {
"changed": true,
"checksum": "bc07bb4d3a780f4fd8cae94ec7bff04edb1a5a4e",
"dest": "/tmp/hosts",
"gid": ,
"group": "root",
"md5sum": "55ee21bf1168f9be70abd35bf29d8e4a",
"mode": "",
"owner": "lewen",
"size": ,
"src": "/root/.ansible/tmp/ansible-tmp-1517744820.18-259504826638509/source",
"state": "file",
"uid":
}
172.16.1.31 | SUCCESS => {
"changed": true,
"checksum": "bc07bb4d3a780f4fd8cae94ec7bff04edb1a5a4e",
"dest": "/tmp/hosts",
"gid": ,
"group": "root",
"md5sum": "55ee21bf1168f9be70abd35bf29d8e4a",
"mode": "",
"owner": "lewen",
"size": ,
"src": "/root/.ansible/tmp/ansible-tmp-1517744820.17-14642605512978/source",
"state": "file",
"uid":
}


[root@m01 ~]# ansible lewen -m command -a "ls -l /tmp/hosts"
172.16.1.31 | SUCCESS | rc= >>
-rwxr-xr-x lewen root Feb : /tmp/hosts
172.16.1.41 | SUCCESS | rc= >>
-rwxr-xr-x lewen root Feb : /tmp/hosts
ansible lewen -m copy -a "src=/etc/hosts dest=/tmp backup=yes"

ansible-doc -l|wc -l
ansible-doc -s copy # 查看文档 其他常用模块命令
ansible lewen -m copy -a "src=/server/scripts/yum-htop.sh dest=/server/scripts/ "
ansible lewen -m shell -a "/bin/sh /server/scripts/yum-htop.sh"
ansible lewen -m script -a "/server/scripts/yum.sh"

定时任务

linux 定时任务。
分,时,日,月,周   执行的命令。

# 创建定时任务
[root@m01 scripts]# ansible lewen -m cron -a "name='restart network' minute=00 hour=00 job=' /etc/init.d/network restart >/dev/null 2>&1'"
172.16.1.31 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"restart network"
]
}
172.16.1.41 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"restart network"
]
}
​# 查看定时任务
[root@m01 scripts]# ansible lewen -a "crontab -l"
172.16.1.41 | SUCCESS | rc= >>
#time sync by lidao at --
*/ * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null >&
#check & send result lee at --
* * * /bin/sh /server/scripts/check.sh >/dev/null >&
#Ansible: restart network
* * * /etc/init.d/network restart >/dev/null >&
172.16.1.31 | SUCCESS | rc= >>
#time sync by lidao at --
*/ * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null >&
#Ansible: restart network
* * * /etc/init.d/network restart >/dev/null >&​
​# 取消定时任务
[root@m01 ~]# ansible oldboy -m cron -a "name='restart network' state=absent "
172.16.1.31 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": []
}
172.16.1.41 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": []

常用模块

每个模块就是一个功能
command(默认的模块)  #执行命令模块****
shell          #执行shell 脚本模块****。 # 支持shell 管道更多的功能
script         #把脚本发到客户端,然后执行。****。
copy          #把本地文件发送到远端
file          # 设定文件属性模块。
service        #系统服务管理模块。
cron          #计划任务管理模块
yum          #yum软件包安装管理模块
synchronize     #使用rsync同步文件模块。

eg:
  ansible lewen -m service -a "name=crond state=started enabled=yes"

ssh 认证模块
authorized_key   #-Adds or removes an SSH authorized key

playbook

ansible 剧本

核心功能
1.PyYAML-剧本的语言。

2.paramiko-远程连接与数据传输。
3.Jinjia2

mkdir -p /server/playbook

[root@m01 playbook]# cat ifconfig.yml
- hosts: lewen
tasks:
- command: ifconfig
- shell: ifconfig >/tmp/ip.log


ansible-playbook -C ifconfig.yml # 检查剧本
ansible-playbook ifconfig.yml [root@m01 playbook]# cat print-ip.yml
- hosts: all
tasks:
- name: get ip address
shell: ifconfig eth0 |awk -F "[ :]+" 'NR==2{print $4}' >>/tmp/ip.log ansible-playbook -C print-ip.yml
ansible-playbook print-ip.yml
ansible all -a "tail -1 /tmp/ip.log"
​ ansible oldboy -m cron -a 'name="restart network" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present'

​# playbook添加定时任务
[root@m01 playbook]# cat add-cron.yml
- hosts: oldboy
tasks:
- name: add restart network cron
cron: name="restart network" minute= hour= job="/etc/init.d/network restart >/dev/null 2>&1" state=present

​ 查看
[root@m01 playbook]# ansible oldboy -a "crontab -l"
172.16.1.41 | SUCCESS | rc= >>
#time sync by lidao at --
*/ * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null >&
#check & send result lee at --
* * * /bin/sh /server/scripts/check.sh >/dev/null >&
172.16.1.31 | SUCCESS | rc= >>
#time sync by lidao at --
*/ * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null >&

playbook添加定时任务

两种书写格式
(1)
- hosts: oldboy
tasks:
- name: add restart network cron
cron: name="restart network" minute= hour= job="/etc/init.d/network restart >/dev/null 2>&1" state=present
(2)
- hosts: oldboy
tasks:
- name: add restart network cron
cron:
name: restart network
minute:
hour:
job: /etc/init.d/network restart >/dev/null >&
state: present

例3:对同一台机器配置多个任务

重启网络 service

安装软件 yum

显示时间信息到文件 date

[root@m01 playbook]# cat manage.yml
- hosts: all
tasks:
- name: restart network
service: #服务
name: network #服务器名
state: restarted #状态
- name: install tree nmap lrzsz iftop htop iotop nc
shell: yum install -y tree nmap lrzsz iftop htop iotop nc
- name: print date to file
shell: date +%F >>/tmp/date.log
yml 转化后的格式:
[ { hosts: 'all',
tasks:
[ { name: 'restart network',
service: { name: 'network', state: 'restarted' } },
{ name: 'install tree nmap lrzsz iftop htop iotop nc',
shell: 'yum install -y tree nmap lrzsz iftop htop iotop nc' },
{ name: 'print date to file',
shell: 'date +%F >>/tmp/date.log' } ] } ]

-

[root@m01 playbook]# cat hosts.yml
- hosts: 172.16.1.41
tasks:
- name: mkdir
shell: mkdir -p /oldboy/backup
- hosts: 172.16.1.31
tasks:
- name: find
shell: find /etc -type f -name "*.conf" >>/tmp/name.log ansible安装rsync服务器 nfs服务器 配置sersync数据同步 如何使用pssh (pssh pscp prsync)

view

最新文章

  1. [转]Tesseract 3.02中文字库训练
  2. 【转】一个lucene的官网例子
  3. repeater 相关问题
  4. HDU2084基础DP数塔
  5. Oracle中的User与Schema
  6. Yii PHP 框架分析(四)
  7. POJ 1724 ROADS(bfs最短路)
  8. 深入理解 JavaScript 异步系列(4)—— Generator
  9. 如何修改SVN客户端中保存的密码
  10. 学编程担心自己英语不好吗?(IT软件开发常用英语词汇)
  11. SQL的JOIN语法解析(inner join, left join, right join, full outer join的区别)
  12. OpenStack_I版 5.Nova部署
  13. 巧用UserAgent来解决浏览器的各种问题
  14. JS 小技巧整理
  15. whereis、which、find的区别
  16. 背水一战 Windows 10 (104) - 通知(Toast): 纯文本 toast, 短时 toast, 长时 toast, 图文 toast
  17. Go语言中的map
  18. JSON 全解
  19. 基于 Web 的 Go 语言 IDE - Wide 1.5.2 发布!
  20. autolayout UILabel 设置最大宽度

热门文章

  1. CentOS7.5下安装Mycat连接MySQL8.0
  2. SQLServer 清空某个库所有表
  3. Python高级技巧:用一行代码减少一半内存占用
  4. 微信小程序版本管理
  5. E0264 Unable to execute '"/usr/bin/codesign" ...'
  6. HTML - input(转)
  7. 使用PandoraBox时的软件源配置
  8. opencv setTo()
  9. k8s之创建etcd集群
  10. EasyUI DataGrid设置列宽为百分比导致表头和内容错位的解决方法