设为首页 收藏本站
查看: 1160|回复: 0

[经验分享] ansible的原理与初步使用

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-1-12 09:26:39 | 显示全部楼层 |阅读模式

1、Ansible基本简介:

1 Ansible是什么?
Ansible是一个适用于成百上千规模的受控节点的配置管理、应用程序部署、内部服务编排等诸多功能于一身的极为简单的IT运维自动化工具引擎,基于Python开发。她无需代理,很容易部署,除SSH外没有其他安全基础设施/配置要求。她使用了一个非常简单的语言(YAML),让你可以编写自己的自动化作业脚本。

2 Ansible是怎样工作的?
Ansible连接到受控机,并推送一个称为“Modules”的应用程序到受控机上,Ansible然后在受控机上执行这些模块(默认情况下通过SSH),并在完成时删除她们。

3 Ansible的优点
1、Playbooks基于YAML,简单易学
2、基于推送,无需在受控机上安装任何程序
3、受控机可以成百上千,管理范围越大成本效能越好
4、内建大量Modules,并可使用“任何语言”开发自定义模块

ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:

(1)、连接插件connection plugins:负责和被监控端实现通信;

(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;

(3)、各种模块核心模块、command模块、自定义模块;

(4)、借助于插件完成记录日志邮件等功能;

(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。


ansible的核心组件:
ansible core
host iventory
core modules
custom modules
playbook (yaml, jinjia2)
connect plugin

特性:

nsible的特性:
基于Python语言实现,由Paramiko, PyYAML和Jinjia2三个关键模块;
部署简单, agentless
默认使用SSH协议;
(1) 基于密钥认证;
(2) 在inventory文件中指定账号和密码;
主从模式:
master: ansible, ssh client
slave: ssh server
支持自定义模块:支持各种编程语言
支持Playbook
基于“模块”完成各种“任务”

2.安装

先安装epel源

wget http://mirrors.aliyun.com/epel/6 ... ease-6-8.noarch.rpm

1
yum -y install ansible




配置文件:/etc/ansible/ansible.cfg
Invertory: /etc/ansible/hosts

准备4台主机初步试用一下:
首先定义其他主机的信息:
1
2
3
4
5
6
[iyunv@localhost ansible]# vim /etc/ansible/hosts
[webservs]
192.168.1.100
192.168.1.101
[dbservs]
192.168.1.102





将密钥发送到要管理的3台主机上:
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.100

查看ansible支持的模块:
1
[iyunv@localhost ansible]# ansible-doc -l




查看某一个模块:
1
[iyunv@localhost ansible]# ansible-doc -s yum




ansible命令应用基础:
语法: ansible <host-pattern> [-f forks] [-m module_name] [-a args]
-f forks:启动的并发线程数;
-m module_name: 要使用的模块;
-a args: 模块特有的参数;


基本测试一下:
command模块:
命令模块,默认
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[iyunv@localhost ansible]# ansible 192.168.1.101 -m command -a 'date'
192.168.1.101 | success | rc=0 >>
Mon Jan 11 03:54:34 CST 2016

[iyunv@localhost ansible]# ansible 192.168.1.102 -m command -a 'date'
192.168.1.102 | success | rc=0 >>
Mon Jan 11 03:54:39 CST 2016

[iyunv@localhost ansible]# ansible all -m command -a 'date'
192.168.1.102 | success | rc=0 >>
Mon Jan 11 03:54:47 CST 2016
192.168.1.101 | success | rc=0 >>
Mon Jan 11 03:54:47 CST 2016
192.168.1.100 | success | rc=0 >>
Mon Jan 11 03:54:49 CST 2016

[iyunv@localhost ansible]# ansible webservs -m command -a 'date'
192.168.1.100 | success | rc=0 >>
Mon Jan 11 03:54:58 CST 2016
192.168.1.101 | success | rc=0 >>
Mon Jan 11 03:54:58 CST 2016
[iyunv@localhost ansible]#




查看下passwd文件:
1
2
3
4
5
6
7
8
9
10
[iyunv@localhost ansible]# ansible all -m command -a 'tail -2 /etc/passwd'
192.168.1.101 | success | rc=0 >>
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
192.168.1.100 | success | rc=0 >>
ntp:x:38:38::/etc/ntp:/sbin/nologin
mogilefs:x:498:498::/home/mogilefs:/bin/bash
192.168.1.102 | success | rc=0 >>
ntp:x:38:38::/etc/ntp:/sbin/nologin
mogilefs:x:305:305::/home/mogilefs:/bin/bash




cron模块:
让被管理节点生成定期自动运维计划:
让2台主机每10分钟运行一次echo hell
1
2
3
4
5
6
7
8
9
10
11
12
13
[iyunv@localhost ansible]# ansible webservs -m cron -a 'minute="*/10" job="/bin/echo hell" name="test cron job"'
192.168.1.100 | success >> {
    "changed": true,
    "jobs": [
        "test cron job"
    ]
}
192.168.1.101 | success >> {
    "changed": true,
    "jobs": [
        "test cron job"
    ]
}




查看是否生成:
1
2
3
4
5
6
7
[iyunv@localhost ansible]# ansible webservs -a 'crontab -l'
192.168.1.101 | success | rc=0 >>
#Ansible: test cron job
*/10 * * * * /bin/echo hell
192.168.1.100 | success | rc=0 >>
#Ansible: test cron job
*/10 * * * * /bin/echo hell




移除任务:absent
1
[iyunv@localhost ansible]# ansible webservs -m cron -a 'minute="*/10" job="/bin/echo hell" name="test cron job" state=absent'




创建用户:user  删除用户后面跟上 state=absent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[iyunv@localhost ansible]# ansible all -m user -a 'name="user1"'
192.168.1.102 | success >> {
    "changed": true,
    "comment": "",
    "createhome": true,
    "group": 500,
    "home": "/home/user1",
    "name": "user1",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 500
}
192.168.1.100 | success >> {
    "changed": true,
    "comment": "",
    "createhome": true,
    "group": 501,
    "home": "/home/user1",
    "name": "user1",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 501
}
192.168.1.101 | success >> {
    "changed": true,
    "comment": "",
    "createhome": true,
    "group": 500,
    "home": "/home/user1",
    "name": "user1",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 500
}




组管理:group
创建mysql组账户:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[iyunv@localhost ansible]# ansible webservs -m group -a 'name=mysql gid=306 system=yes'
192.168.1.100 | success >> {
    "changed": true,
    "gid": 306,
    "name": "mysql",
    "state": "present",
    "system": true
}
192.168.1.101 | success >> {
    "changed": true,
    "gid": 306,
    "name": "mysql",
    "state": "present",
    "system": true
}





创建mysql用户并且加入mysql组里:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[iyunv@localhost ansible]# ansible webservs -m user -a 'name=mysql uid=306 system=yes group=mysql'
192.168.1.101 | success >> {
    "changed": true,
    "comment": "",
    "createhome": true,
    "group": 306,
    "home": "/home/mysql",
    "name": "mysql",
    "shell": "/bin/bash",
    "state": "present",
    "system": true,
    "uid": 306
}
192.168.1.100 | success >> {
    "append": false,
    "changed": true,
    "comment": "MySQL Server",
    "group": 306,
    "home": "/var/lib/mysql",
    "move_home": false,
    "name": "mysql",
    "shell": "/bin/bash",
    "state": "present",
    "uid": 306
}




copy:
src=: 定义本地源文件路径
dest=: 定义远程目标文件路径
content=: 取代src=,表示直接用此处指定的信息生成为目标文件

复制/etc/fstab文件到/tmp目录下:
注意如果开启selint的话需要安装libselinux-python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
[iyunv@localhost ansible]# ansible all -m copy -a 'src=/etc/fstab dest=/tmp/fstab.ansible owner=root mode=640'
192.168.1.101 | success >> {
    "changed": true,
    "checksum": "7e5f7c54a01bda71579a35224bbac8016a46c5ae",
    "dest": "/tmp/fstab.ansible",
    "gid": 0,
    "group": "root",
    "md5sum": "ad7f86cff32f69c4ebe056de9663c4c9",
    "mode": "0640",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 805,
    "src": "/root/.ansible/tmp/ansible-tmp-1449182354.52-55405501135097/source",
    "state": "file",
    "uid": 0
}
192.168.1.102 | success >> {
    "changed": true,
    "checksum": "7e5f7c54a01bda71579a35224bbac8016a46c5ae",
    "dest": "/tmp/fstab.ansible",
    "gid": 0,
    "group": "root",
    "md5sum": "ad7f86cff32f69c4ebe056de9663c4c9",
    "mode": "0640",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 805,
    "src": "/root/.ansible/tmp/ansible-tmp-1449182354.55-90702109372767/source",
    "state": "file",
    "uid": 0
}
192.168.1.100 | success >> {
    "changed": false,
    "checksum": "7e5f7c54a01bda71579a35224bbac8016a46c5ae",
    "dest": "/tmp/fstab.ansible",
    "gid": 0,
    "group": "root",
    "mode": "0640",
    "owner": "root",
    "path": "/tmp/fstab.ansible",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 805,
    "state": "file",
    "uid": 0
}




content:直接生成文件内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[iyunv@localhost ansible]# ansible all -m copy -a 'content="hello ansible" dest=/tmp/test.ansible'
192.168.1.102 | success >> {
    "changed": true,
    "checksum": "7b320b1dc0c867516cf00728df488daa3532bc1f",
    "dest": "/tmp/test.ansible",
    "gid": 0,
    "group": "root",
    "md5sum": "37bc018071eae9a0e879c31b2f9aa554",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 13,
    "src": "/root/.ansible/tmp/ansible-tmp-1449182800.34-155397933435096/source",
    "state": "file",
    "uid": 0
}
192.168.1.100 | success >> {
    "changed": true,
    "checksum": "7b320b1dc0c867516cf00728df488daa3532bc1f",
    "dest": "/tmp/test.ansible",
    "gid": 0,
    "group": "root",
    "md5sum": "37bc018071eae9a0e879c31b2f9aa554",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 13,
    "src": "/root/.ansible/tmp/ansible-tmp-1449182800.43-92444335436660/source",
    "state": "file",
    "uid": 0
}
192.168.1.101 | success >> {
    "changed": true,
    "checksum": "7b320b1dc0c867516cf00728df488daa3532bc1f",
    "dest": "/tmp/test.ansible",
    "gid": 0,
    "group": "root",
    "md5sum": "37bc018071eae9a0e879c31b2f9aa554",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 13,
    "src": "/root/.ansible/tmp/ansible-tmp-1449182800.38-52532138760213/source",
    "state": "file",
    "uid": 0
}




file:
将复制的文件的属主,属组改为mysql:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[iyunv@localhost ansible]# ansible all -m file -a 'owner=mysql group=mysql mode=644 path=/tmp/fstab.ansible'
192.168.1.100 | success >> {
    "changed": true,
    "gid": 306,
    "group": "mysql",
    "mode": "0644",
    "owner": "mysql",
    "path": "/tmp/fstab.ansible",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 805,
    "state": "file",
    "uid": 306
}
192.168.1.101 | success >> {
    "changed": true,
    "gid": 306,
    "group": "mysql",
    "mode": "0644",
    "owner": "mysql",
    "path": "/tmp/fstab.ansible",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 805,
    "state": "file",
    "uid": 306
}
192.168.1.102 | success >> {
    "changed": true,
    "gid": 306,
    "group": "mysql",
    "mode": "0644",
    "owner": "mysql",
    "path": "/tmp/fstab.ansible",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 805,
    "state": "file",
    "uid": 306
}




ping模块:

1
2
3
4
5
6
7
8
9
10
11
12
13
[iyunv@localhost ansible]# ansible all -m ping
192.168.1.100 | success >> {
    "changed": false,
    "ping": "pong"
}
192.168.1.102 | success >> {
    "changed": false,
    "ping": "pong"
}
192.168.1.101 | success >> {
    "changed": false,
    "ping": "pong"
}




service:
管理节点服务的启动状态
1
2
3
4
5
6
7
8
9
10
11
12
13
[iyunv@localhost ansible]# ansible webservs -m service -a 'enabled=true name=httpd state=started'
192.168.1.101 | success >> {
    "changed": true,
    "enabled": true,
    "name": "httpd",
    "state": "started"
}
192.168.1.100 | success >> {
    "changed": true,
    "enabled": true,
    "name": "httpd",
    "state": "started"
}




shell:
用到管道复杂命令功能时建议用shell
1
2
3
4
5
6
7
8
9
10
[iyunv@localhost ansible]# ansible all -m shell -a 'echo 123..com | passwd --stdin user1'
192.168.1.102 | success | rc=0 >>
Changing password for user user1.
passwd: all authentication tokens updated successfully.
192.168.1.101 | success | rc=0 >>
Changing password for user user1.
passwd: all authentication tokens updated successfully.
192.168.1.100 | success | rc=0 >>
Changing password for user user1.
passwd: all authentication tokens updated successfully.




script:将本地脚本复制到远程主机

1
[iyunv@localhost ansible]# ansible all -m script -a "/tmp/a.sh"




yum:安装程序包 卸载的话 state=absent
1
[iyunv@localhost ansible]# ansible all -m yum -a "name=zsh"





setup:远程主机的facts
每个被管理节点在接受并运行管理命令之前,会将自己主机相关信息,如操作版本,ip等报告给远程的ansible主机。
1
[iyunv@localhost ansible]# ansible all -m yum -a "name=zsh"



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-163327-1-1.html 上篇帖子: ansible 原理及详解 下篇帖子: ansible原理及简单应用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表