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

[经验分享] ansible详解

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-2-1 08:45:56 | 显示全部楼层 |阅读模式
                      ansible:

   特性:

    模块化,调用特定的模块来完成特定任务;

    基于Python语言实现,由Paramiko,PyYAML和Jinja2三个关键模块实现;

    部署简单,agentless;
    主从模式;

    支持自定义模块;
    支持playbook

  (支持幂等性)
   组成部分:
    ansible core
    host inventory

    connection plugins

    modules:
        custom modules;

        core modules;

    playbooks

   配置文件:
    主配置文件:/etc/ansible/ansible.cfg

    Host Inventory:/etc/ansible/hosts

ansible命令:
     ansible  <host-pattern>  [-f forks]  [-m module_name]  [-a args]
-i PATH, --inventory=PATH:指明使用的host inventory文件路径;


注:在使用ansible命令在director主机上统一管控后端集群主机时,director基于ssh协议与后端主机进行管控,所以要提前生成director的密钥对copy给集群中需要被ansible管控的主机,使director主机与集群中的主机可以基于密钥对方式连接ssh。

在director主机中安装ansible:
1
[iyunv@localhost ~]# yum install ansible



1
2
3
4
5
[iyunv@localhost ~]# vim /etc/ansible/hosts   #在hosts文件中定义要管控的主机
[websrvs]
172.16.61.2
172.16.61.3
...





常用模块:
[-a args]:
args: key=value  键值类型

    ①command:默认模块:在远程节点上运行一个命令;
-a 'COMMAND'
   注:command模块的参数非为kv格式,而是直接给出要执行的命令即可;
1
2
3
4
5
6
7
8
9
10
11
12
13
[iyunv@localhost ~]# ansible all -m command -a 'ls'  #在远程节点上运行ls命令
172.16.61.2 | success | rc=0 >>
anaconda-ks.cfg

172.16.61.3 | success | rc=0 >>
anaconda-ks.cfg

[iyunv@localhost ~]# ansible all -a 'ls'      #command为默认模块不-m指明也能默认使用
172.16.61.3 | success | rc=0 >>
anaconda-ks.cfg

172.16.61.2 | success | rc=0 >>
anaconda-ks.cfg




    ②user:
-a 'name=  state={present|absent}  force=   system=  uid=  shell=  home='
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
[iyunv@localhost ~]# ansible websrvs -m user -a 'name=tz state=present'  #在远程节点创建该用户
172.16.61.3 | success >> {
    "changed": true,
    "comment": "",
    "createhome": true,
    "group": 1001,
    "home": "/home/tz",
    "name": "tz",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001
}

172.16.61.2 | success >> {
    "changed": true,
    "comment": "",
    "createhome": true,
    "group": 1001,
    "home": "/home/tz",
    "name": "tz",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001
}

[iyunv@localhost ~]# ansible websrvs -m user -a 'name=tz state=absent force=true' #删除该用户及家目录
172.16.61.3 | success >> {
    "changed": true,
    "force": true,
    "name": "tz",
    "remove": false,
    "state": "absent"
}

172.16.61.2 | success >> {
    "changed": true,
    "force": true,
    "name": "tz",
    "remove": false,
    "state": "absent"
}




    ③group:
-a 'name=  state={present|absent}  gid=  system='
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[iyunv@localhost ~]# ansible websrvs -m group -a 'name=grp state=present system=true' #创建grp系统组
172.16.61.2 | success >> {
    "changed": true,
    "gid": 992,
    "name": "grp",
    "state": "present",
    "system": true
}

172.16.61.3 | success >> {
    "changed": true,
    "gid": 992,
    "name": "grp",
    "state": "present",
    "system": true
}




    ④cron:
-a 'name=  state=  minute=  hour=  day=  month=  weekday=  job='
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
[iyunv@localhost ~]# ansible websrvs -m cron -a "name=timesync minute='*/5' job='/usr/sbin/ntpdate 172.16.0.1 &> /dev/null'"   #在远程节点上定义周期性任务,每五分钟执行一次同步时间的操作
172.16.61.3 | success >> {
    "changed": true,
    "jobs": [
        "timesync"
    ]
}

172.16.61.2 | success >> {
    "changed": true,
    "jobs": [
        "timesync"
    ]
}

[iyunv@localhost ~]# crontab -l    #远程节点上可以查看出该周期任务。
#Ansible: timesync
*/5 * * * * /usr/sbin/ntpdate 172.16.0.1 &> /dev/null

[iyunv@localhost ~]# ansible websrvs -m cron -a "name=timesync state=absent"  #删除该周期性任务
172.16.61.3 | success >> {
    "changed": true,
    "jobs": []
}

172.16.61.2 | success >> {
    "changed": true,
    "jobs": []
}





    ⑤ping:
没有参数
1
2
3
4
5
6
7
8
9
10
[iyunv@localhost ~]# ansible websrvs -m ping
172.16.61.3 | success >> {
    "changed": false,
    "ping": "pong"
}

172.16.61.2 | success >> {
    "changed": false,
    "ping": "pong"
}




    ⑥file:
-a 'path=  mode=  owner=  group=  state={file|directory|link|hard|touch|absent}  src='
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
[iyunv@localhost ~]# ansible websrvs -m file -a 'path=/tmp/test state=touch mode=600'  #在远程节点上/tmp目录下创建权限为600,名称为test的文件
172.16.61.3 | success >> {
    "changed": true,
    "dest": "/tmp/test",
    "gid": 0,
    "group": "root",
    "mode": "0600",
    "owner": "root",
    "secontext": "unconfined_u:object_r:user_tmp_t:s0",
    "size": 0,
    "state": "file",
    "uid": 0
}

172.16.61.2 | success >> {
    "changed": true,
    "dest": "/tmp/test",
    "gid": 0,
    "group": "root",
    "mode": "0600",
    "owner": "root",
    "secontext": "unconfined_u:object_r:user_tmp_t:s0",
    "size": 0,
    "state": "file",
    "uid": 0
}
[iyunv@localhost ~]# ansible websrvs -m file -a 'path=/tmp/test state=absent' #删除该文件
172.16.61.3 | success >> {
    "changed": true,
    "path": "/tmp/test",
    "state": "absent"
}

172.16.61.2 | success >> {
    "changed": true,
    "path": "/tmp/test",
    "state": "absent"
}
[iyunv@localhost ~]# ansible websrvs -m file -a 'path=/tmp/test state=link src=/etc/fstab' #在远程节点上创建/etc/fstab的符号链接指向/tmp/test
172.16.61.3 | success >> {
    "changed": true,
    "dest": "/tmp/test",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "secontext": "unconfined_u:object_r:user_tmp_t:s0",
    "size": 10,
    "src": "/etc/fstab",
    "state": "link",
    "uid": 0
}

172.16.61.2 | success >> {
    "changed": true,
    "dest": "/tmp/test",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "secontext": "unconfined_u:object_r:user_tmp_t:s0",
    "size": 10,
    "src": "/etc/fstab",
    "state": "link",
    "uid": 0
}





    ⑦copy:把管理端的文件给远程节点各复制一份
-a 'dest=  src=\'#\'"   owner=  group=  mode='
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
[iyunv@localhost ~]# ansible websrvs -m copy -a 'src=/etc/fstab dest=/tmp/fstab mode=660' #将本地主机/etc/fstab文件复制到远程节点的/tmp目录下,权限为660
172.16.61.3 | success >> {
    "changed": true,
    "checksum": "37ed7ee7a0cb241d01cf18351d2c541d12003937",
    "dest": "/tmp/fstab",
    "gid": 0,
    "group": "root",
    "md5sum": "8c466190fc6993d65baeeb0beff52de4",
    "mode": "0660",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 619,
    "src": "/root/.ansible/tmp/ansible-tmp-1454251002.67-64682182817600/source",
    "state": "file",
    "uid": 0
}

172.16.61.2 | success >> {
    "changed": true,
    "checksum": "37ed7ee7a0cb241d01cf18351d2c541d12003937",
    "dest": "/tmp/fstab",
    "gid": 0,
    "group": "root",
    "md5sum": "8c466190fc6993d65baeeb0beff52de4",
    "mode": "0660",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 619,
    "src": "/root/.ansible/tmp/ansible-tmp-1454251002.66-162169360284592/source",
    "state": "file",
    "uid": 0
}





    ⑧yum:
-a 'name=  conf_file=  state={present|latest|absent}  enablerepo= disablerepo='
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@localhost ~]# ansible websrvs -m yum -a 'name=httpd state=present'  #在远程节点上安装httpd程序
172.16.61.2 | success >> {
    "changed": false,
    "msg": "",
    "rc": 0,
    "results": [
        "httpd-2.4.6-31.el7.centos.x86_64 providing httpd is already installed" #提示已经被安装了
    ]
}

172.16.61.3 | success >> {
    "changed": false,
    "msg": "",
    "rc": 0,
    "results": [
        "httpd-2.4.6-31.el7.centos.x86_64 providing httpd is already installed"
    ]
}



     
    ⑨service:启动远程节点的服务
      -a  'name=  state={started|stopped|restarted}  enabled=  runlevel='
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[iyunv@localhost ~]# ansible websrvs -m service -a 'name=httpd state=started enabled=true' #开启远程节点的httpd服务,并且设定开机启动
172.16.61.2 | success >> {
    "changed": true,
    "enabled": true,
    "name": "httpd",
    "state": "started"
}

172.16.61.3 | success >> {
    "changed": true,
    "enabled": true,
    "name": "httpd",
    "state": "started"
}





    ⑩shell:在shell环境中运行命令
      -a  'COMMAND'
1
2
3
4
5
6
7
8
[iyunv@localhost ~]# ansible websrvs -m shell -a 'echo "tianzhuang" | passwd --stdin user1'
172.16.61.3 | success | rc=0 >>
Changing password for user user1.
passwd: all authentication tokens updated successfully.

172.16.61.2 | success | rc=0 >>
Changing password for user user1.
passwd: all authentication tokens updated successfully.



     
            
            script:将主机上的脚本复制到远程节点上并运行
            -a  '/PATH/TO/SCRIPT'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[iyunv@localhost ~]# vim hello.sh  #在本地创建一个脚本

#!/bin/bash
echo "hello"

[iyunv@localhost ~]# ansible websrvs -m script -a '/root/hello.sh' #在远程节点上运行该脚本
172.16.61.2 | success >> {
    "changed": true,
    "rc": 0,
    "stderr": "OpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 56: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug1: mux_client_request_session: master session id: 2\r\nShared connection to 172.16.61.2 closed.\r\n",
    "stdout": "hello\r\n"
}

172.16.61.3 | success >> {
    "changed": true,
    "rc": 0,
    "stderr": "OpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 56: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug1: mux_client_request_session: master session id: 2\r\nShared connection to 172.16.61.3 closed.\r\n",
    "stdout": "hello\r\n"
}



                               
                    setup:
           获取指定主机的facts;

       注: ansible-doc命令:获取模块列表,及模块使用格式;               
                 ansible-doc  -l
             ansible-doc  -s  module_name


ansible playbooks:
核心元素:
        Tasks
        Variables
        Templates
        Handlers
        Roles
  组织格式:YAML
    语法:

    列表:-
    字典:k:v数据,键值对
                   


运维网声明 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-172088-1-1.html 上篇帖子: ansible-playbook 下篇帖子: 求助,关于ansible管理windows
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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