ansible介绍:
- 不需要安装客户端,通过sshd去通信
- 基于模块工作,模块可以由任何语言开发
- 不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读 安装十分简单,centos上可直接yum安装
- 有提供UI(浏览器图形化)www.ansible.com/tower,收费的
- 官方文档 http://docs.ansible.com/ansible/latest/index.html
- ansible已经被redhat公司收购,它在github上是一个非常受欢迎的开源软件,github地址https://github.com/ansible/ansible
- 一本不错的入门电子书 https://ansible-book.gitbooks.io/ansible-first-book/
ansible安装:
1.首先安装,因为redhat已经收购了ansible所有镜像基本都有
yum install -y ansible
2.把中控中心的公钥放另一台机器上
ssh-keygen -t rsa //-t指定类型rsa
3.编辑安装了ansible的机器的配置文件
vim /etc/ansible/hosts
加入如下内容
[one_group] //这里定义组,我们可以通过组来批量处理机器
192.168.195.132 //对方IP
4.远程执行w命令
ansible one_group -m command -a 'w' //-m指定模块,-a后面是要执行的命令
过程如下:
[root@one .ssh]# ansible one_group -m command -a 'w'
The authenticity of host '192.168.195.132 (192.168.195.132)' can't be established.
ECDSA key fingerprint is SHA256:bmaeeb4Iz6HNVp2ihovKQp6N5J/mZ8f4A8QaChS4Kww.
ECDSA key fingerprint is MD5:bf:3e:4d:29:65:99:28:81:6a:01:4c:eb:8d:f4:be:8c.
Are you sure you want to continue connecting (yes/no)? yes
192.168.195.132 | SUCCESS | rc=0 >>
21:51:19 up 3:38, 2 users, load average: 0.00, 0.02, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.195.1 21:40 2:39 0.06s 0.06s -bash
root pts/1 192.168.195.129 21:51 0.00s 0.09s 0.01s w
Ansible拷贝文件或者目录
这里使用copy模块
//src来源文件或者目录,dest目地目录,owner,group属组和用户,mode权限
ansible one_group -m copy -a "src=/tmp/123.txt dest=/home/abc.txt owner=root group=root mode=0755"
执行过程如下:
192.168.195.132 | SUCCESS => {
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/home/abc.txt",
"gid": 0,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "0755",
"owner": "root",
"secontext": "unconfined_u:object_r:user_home_dir_t:s0",
"size": 0,
"src": "/root/.ansible/tmp/ansible-tmp-1537970595.91-157668548220691/source",
"state": "file",
"uid": 0
}
Ansible远程执行脚本
1.在对方机器编写一个脚本,或者在中控中心写完远程传输
#!/bin//bash
mkdir /home/qwe
2.远程执行脚本
ansible one_group -m shell -a ‘sh /home/1.sh’
过程如下:
[root@one tmp]# ansible one_group -m shell -a 'sh /home/1.sh'
192.168.195.132 | SUCCESS | rc=0 >>
Ansible管理任务计划
1.增加任务计划,这里是以一个组的进行,你也可以单写一个IP
-m 指定cron模块,name指定任务计划标题 job指定任务,weekday指定时间
ansible one_group -m cron -a "name='plan' job='/bin/touch /tmp/pop.txt' weekday=6"
过程如下:
[root@one ~]# ansible one_group -m cron -a "name='plan' job='/bin/touch /tmp/pop.txt' weekday=6"
192.168.195.132 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"plan"
]
}
关于时间的表示:分钟 minute=小时 hour=日期 day=月份 month weekday=星期
2.检查另外一台机器是否有设置任务计划
[root@two ~]# crontab -l
# Lines below here are managed by Salt, do not edit
#Ansible: plan
* * * * 6 /bin/touch /tmp/pop.txt
可以看到上面已经加入了。注意:带#号的标签不能修改也不能删除
1.如何删除任务计划:
删除只需要加上state=absent即可
ansible one_group -m cron -a "name='plan' state=absent"
过程如下:
[root@one ~]# ansible one_group -m cron -a "name='plan' state=absent"
192.168.195.132 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": []
}
2.检查另一台机器是否删除任务计划:
[root@two ~]# crontab -l
# Lines below here are managed by Salt, do not edit
Ansible安装rpm包/管理服务
类似远程使用yum安装/删除
安装:
ansible one_group -m yum -a "name=httpd state=installed"
删除:
ansible one_group -m yum -a "name=httpd state=removed"
可以看到以上2者的区别在于state的状态
远程启动服务:
ansible one_group -m service -a "name=httpd state=started enabled=yes"
enabled表示是否开机启动,name表示centos服务的名
列出ansible的所有模块
ansible-doc -l
查看指定模块的相关参数文档
ansible-doc 模块名(如yum,cron,shell,command,service,copy)
Ansible playbook的使用
1.利用playbook批量传输文件
1.编写yml后缀的文件
--- //这里3个杠
- hosts: one_group //定义主机名或者组,
remote_user: root //执行的用户
tasks: //要执行的任务
- name: test1 //自定义任务名
shell: touch /home/abc.txt //指定模块需要执行什么命令
这里非常注重格式,少了个空格都不能执行!
2.执行
ansible-playbook create_file.yml
2.利用playbook批量创建用户
1.编写配置文件
---
- hosts: one_group
user: root
gather_facts: false 是否获取系统属性
vars:
- a: "etc" 定义变量
tasks:
- name: create user 自定义名字
user: name="{{a}}" 使用user模块
2.执行
ansible-playbook create_user.yml
Ansible playbook中的循环
下面示例一个批量创建用户命令:
1.编写子配置文件
vim while.yml
---
- hosts: one_group
user: root
tasks:
- name: create user
user: name="{{item}}" item指向items,也是固定的
with_items: //固定格式
- qq
- bb
- aa
2.执行
ansible-playbook while.yml
Ansible playbook中的条件判断
1.编写配置文件:
vim when.yml
加入如下内容:
---
- hosts: one_group
user: root
gather_facts: True
tasks:
- name: use when
shell: touch /tmp/when.txt
when: ansible_distribution == "CentOS" //这是判断语法:当XX等于什么时候执行shell
关于when那个格式,其实就是通过对方机器的item来做判断可以是IP可以是主机名:
查看item命令
ansible 组名或者主机名 -m setup //注意这里是要添加了ansible的host才能看到
2.执行
ansible-playbook when.yml
Ansible playbook中的handlers
这个handlers跟&&有点像,意思是先执行完前面然后执行后面的意思:
1.编写配置文件:
vim handlers.yml
加入如下内容:
---
- hosts: one_group
user: root
tasks:
- name: touch file
shell: touch /home/anb.txt
notify: echo //这个命令就是类似跳转到handlers这里执行,名字要跟下面定义的一样
handlers:
- name: echo
shell: echo "123" >> /home/anb.txt
2.执行:
ansible-playbook handlers.yml