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

[经验分享] ansible的简单使用

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-3-20 10:20:17 | 显示全部楼层 |阅读模式
一、ansible是基于python paramiko开发,分布式,无需安装客户端,轻量级的自动化运维管理工具。
二、ansible的安装和配置
yum安装ansible
1
yum install ansible -y



vim /etc/ansible/hosts
1
2
[test]  \\主机组名,可以任意命名
192.168.2.101 \\管理节点主机,也可以是主机名



修改/etc/ansible/ansible.cfg

  • 去掉host check


1
host_key_checking = False



  • 加快ssh的速度,强制用ssh,防止自动选择用python的

1
2
3
4
#找到transport,修改成ssh
transport      = ssh
#找到ssh_args,去掉注释,修改成空,在[ssh_connection]的后面
ssh_args =



三、ansible的常用模块
1. commom模块,默认模块,用于在各被管理节点运行指令的命令
1
2
3
4
5
6
7
8
9
[iyunv@test ~]# ansible all  -m command -a 'ifconfig eth0'
192.168.2.101 | success | rc=0 >>
eth0      Link encap:Ethernet  HWaddr 00:0C:29:F8:D4:88  
    inet addr:172.16.2.13  Bcast:172.16.255.255  Mask:255.255.0.0
    inet6 addr: fe80::20c:29ff:fef8:d488/64 Scope:Link
    UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
    RX packets:8046 errors:0 dropped:0 overruns:0 frame:0
    TX packets:2165 errors:0 dropped:0 overruns:0 carrier:0    collisions:0 txqueuelen:1000
    RX bytes:1392192 (1.3 MiB)  TX bytes:201703 (196.9 KiB)



2. user模块,用户模块,用于在各被管理节点管理用户所使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[iyunv@test ~]# ansible all  -m user -a 'name=test'
192.168.2.101 | success >> {  
    "changed": true,
    "comment": "",
    "createhome": true,
    "group": 500,
    "home": "/home/test",
    "name": "test",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 500}
[iyunv@test ~]# tail  -1 /etc/passwd
test:x:500:500::/home/test:/bin/bash



3. group模块,用户组模块,用于在各被管理节点管理用户组所使用
1
2
3
4
5
6
7
8
[iyunv@test ~]# ansible all  -m group  -a 'name=mylinux gid=1000'
192.168.2.101 | success >> {  "changed": true,
    "gid": 1000,
    "name": "mylinux",
    "state": "present",
    "system": false}
[iyunv@test ~]# tail -1 /etc/gshadow
mylinux:!::



4. cron模块,计划任务模块,用于在各被管理节点管理计划任务
1
2
3
4
5
6
7
8
9
[iyunv@test ~]# ansible all -m cron -a  "name=time  minute='*/2' job='/usr/sbin/ntpdate 192.168.2.100'"
192.168.2.101 | success >> {  "changed": true,
    "jobs": [      
    "time"
    ]
}
[iyunv@test ~]# crontab  -l  \\在管理节点查看cron任务
#Ansible: time
*/2 * * * * /usr/sbin/ntpdate 192.168.2.100



5. copy模块:复制模块,复制文件至各管理节点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[iyunv@test ~]# ansible all -m copy -a 'src=/root/test dest=/tmp mode=600'
192.168.2.101 | success >> {  "changed": true,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/tmp/test",
    "gid": 0,
    "group": "root",
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
    "mode": "0600",
    "owner": "root",
    "size": 0,
    "src": "/root/.ansible/tmp/ansible-tmp-1439189042.77-131108212586927/source",
    "state": "file",
    "uid": 0}
[iyunv@test ~]# ls -l /tmp/test
-rw------- 1 root root 0 Aug 10 14:44 /tmp/test



6. file模块:文件模块,修改各个节点指定的文件属性
1
2
3
4
5
6
7
8
9
10
11
12
13
[iyunv@test ~]# ansible all -m file -a 'path=/tmp/test mode=644 owner=test'
192.168.2.101 | success >> {  "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "test",
    "path": "/tmp/test",
    "size": 0,
    "state": "file",
    "uid": 500
}
[iyunv@test ~]# ls -l /tmp/test
-rw-r--r-- 1 test root 0 Aug 10 14:44 /tmp/test



7. ping模块:测试模块,测试各个被管理节点是否在线
1
2
3
4
5
[iyunv@test ~]# ansible all  -m ping
192.168.2.101 | success >> {
    "changed": false,
    "ping": "pong"
    }



8. service模块:管理各个节点的服务
1
2
3
4
5
6
[iyunv@test ~]# ansible all -m service -a 'name=ntpd  enabled=true'
192.168.2.101 | success >> {
    "changed": true,
    "enabled": true,
    "name": "ntpd"
    }



9. shell模块:与command模块功能相同,但比command的模块功能强大
1
2
3
4
[iyunv@test ~]# ansible all -m shell -a 'cat /etc/passwd | grep root'
192.168.2.101 | success | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin



10. script模块:自动复制脚本至远程节点并运行
1
2
3
4
5
6
7
8
9
10
11
[iyunv@test ~]# cat ansible.sh
#!/bin/bashecho  "hello word" >> /tmp/test
[iyunv@zqq ~]# ansible all -m script -a '/root/ansible.sh'
192.168.2.101 | success >> {
    "changed": true,
    "rc": 0,
    "stderr": "...",
    "stdout": ""
    }
[iyunv@test ~]# cat /tmp/test
hello word



11. setup模块:收集ansible的facters
1
2
3
4
5
6
7
[iyunv@test ~]# ansible all -m setup
192.168.2.101 | success >> {
    "ansible_facts": {
            "ansible_all_ipv4_addresses": [
                        "192.168.2.101"
        ],
       ...............



12. yum模块:用于在各个管理节点安装软件所使用
1
2
3
4
5
6
7
8
9
[iyunv@test ~]# ansible all -m yum -a 'name=httpd state=present'
192.168.2.101 | success >> {
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "...
[iyunv@test ~]# rpm -q httpd
httpd-2.2.15-45.el6.centos.x86_64



运维网声明 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-352167-1-1.html 上篇帖子: centos7离线安装ansible 下篇帖子: ansible之playbook自动化安装nginx
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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