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

[经验分享] ansible自动化工具部署实例

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-3-27 09:14:10 | 显示全部楼层 |阅读模式
一、安装准备
1
2
3
4
5
6
7
8
9
#关闭防火墙iptables
service iptables stop
chkconfig iptables off

#并且修改SELINUX=disabled
sed -i's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

#使用yum安装python等软件
yum install PyYAML.x86_64 python-paramiko.noarch python-jinja2.x86_64 python-devel –y




二、下载安装包
1
2
3
#下载ansible和setuptools安装包
wget https://pypi.python.org/packages ... nsible-1.7.2.tar.gz
wget https://pypi.python.org/packages ... tuptools-7.0.tar.gz



三、解压安装软件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#解压安装setuptools
tar zfxv setuptools-7.0.tar.gz
cd setuptools-7.0
python setup.py install
cd ..

#解压安装ansible
tar fzvx ansible-1.7.2.tar.gz
cd ansible-1.7.2
python setup.py build
python setup.py install
mkdir /etc/ansible
cp examples/ansible.cfg /etc/ansible/
cp examples/hosts /etc/ansible/
cd ..




四、修改配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#配置ansible,我这里默认是不需要修改的
vi /etc/ansible/ansible.cfg
hostfile       = /etc/ansible/hosts
library        = /usr/share/ansible
remote_tmp     = $HOME/.ansible/tmp
pattern        = *
forks          = 5
poll_interval  = 15
sudo_user      = ansible
#ask_sudo_pass = True
#ask_pass      = True
transport      = smart
remote_port    = 22
module_lang    = C
  
#修改/etc/ansible/hosts文件
#local_Server
[localhost]
127.0.0.1
#client
[client]
192.168.1.232



五、SSH通信设置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ssh互信
[iyunv@ansibleserver ~]#ssh-keygen –b 1024 -t rsa
#不断的回车即可
The key's randomart image is:
+--[ RSA 2048]----+
|    o  o.        |
|     +=o .       |
|     .=+* o      |
|      o* OE.     |
|       .S.=      |
|       +..       |
|      . +        |
|       .         |
|                 |
+-----------------+



1
2
3
4
5
6
7
8
9
#然后进入.ssh/目录下
[iyunv@ansibleserver ~]#cd .ssh/
cat *.pub > authorized_keys
chmod –R 700 .
#再将authorized_keys拷贝到客户端192.168.1.232
scp authorized_keys root@192.168.1.232:/root/.ssh/authorized_keys
#执行scp出现的错误以及解决方法:
scp: /root/.ssh/authorized_keys: No such file or directory
#解决方法:在客户端也直接生成一个ssh-keygen –b 124 –t rsa文件即可,这样的做法主要是生成.ssh/的目录,服务端才能够将公钥拷贝过去



1
2
3
4
5
6
7
#测试互信是否成功
[iyunv@ansibleserver ~]# ssh 192.168.1.232
Last login: Tue Mar 17 22:56:26 2015 from 192.168.1.231
#无需密码,直接登录成功!说明互信已经成功了!
[iyunv@localhost ~]# ifconfig
eth1      Link encap:Ethernet  HWaddr 08:00:27:41:28:38  
          inetaddr:192.168.1.232 Bcast:192.168.1.255 Mask:255.255.255.0



六、模块测试
模块命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-i 设备列表路径,可以指定一些动态路径
-f 并发任务数
-private-key 私钥路径
-m 模块名称
-M 模块夹的路径
-a 参数
-k 登陆密码
-K sudo密码
-t 输出结果保存路径
-B 后台运行超时时间
-P 调查后台程序时间
-u 执行用户
-U sudo用户
-l 限制设备范围
-s 是此用户sudo无需输入密码




ping模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#使用ansible的ping模块测试client是否能够通信!
#注意:all 代表所有client的意思
[iyunv@ansibleserver ~]# ansible all -m ping
192.168.1.232 | success >> {
    "changed": false,
    "ping":"pong"
}
127.0.0.1 | success >> {
    "changed": false,
    "ping":"pong"
}

#查看时间
[iyunv@ansibleserver ~]# ansible all -m command -a "date"
192.168.1.232 | success | rc=0 >>
Tue Mar 17 23:06:43 EDT 2015
127.0.0.1 | success | rc=0 >>
Tue Mar 17 23:06:44 EDT 2015




安装软件测试
1
2
3
4
5
6
7
8
9
[iyunv@ansibleserver ~]# ansible all -m command -a "yum install unzip -y"
192.168.1.232 | success | rc=0 >>
Loaded plugins: fastestmirror
Setting up Install Process
Determining fastest mirrors
…………
Installed:
  unzip.x86_64 0:6.0-1.el6                                                      
Complete!




copy模块
1
2
3
4
5
6
7
8
9
#拷贝文件到远程主机
相关选项如下:
backup:在覆盖之前,将源文件备份,备份文件包含时间信息。有两个选项:yes|no
content:用于替代“src”,可以直接设定指定文件的值
dest:必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录
directory_mode:递归设定目录的权限,默认为系统默认权限
force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes
others:所有的file模块里的选项都可以在这里使用
src:被复制到远程主机的本地文件,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用“/”来结尾,则只复制目录里的内容,如果没有使用“/”来结尾,则包含目录在内的整个内容全部复制,类似于rsync。



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
实例:拷贝本地的/root/script目录所有内容到192.168.1.232的/tmp目录下
注意:因为script后面没有加/ ,所以拷贝的是整个目录
ansible 192.168.1.232 -m copy -a "src=/root/script dest=/tmp/owner=root group=root mode=0644"
#拷贝成功的返回信息
192.168.1.232 | success >> {
   "changed": true,
   "dest": "/tmp/",
   "src": "/root/script"
}

#切换到192.168.1.232机器中查看
[iyunv@localhost ~]# ls /tmp/script/
a.txt  b.txt
#拷贝script目录的文件实例:
注意:这里的script后面是加了/ ,所以只拷贝script目录下的文件
[iyunv@ansibleserver script]# ansible 192.168.1.232 -m copy -a"src=/root/script/ dest=/tmp/script/ owner=root group=root mode=0644"
192.168.1.232 | success >> {
    "changed": true,
    "dest":"/tmp/script/",
    "src":"/root/script"
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
在192.168.1.232的script目录下查看内容
[iyunv@localhost script]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 17 23:38 a.txt
-rw-r--r-- 1 root root 0 Mar 17 23:38 b.txt
-rw-r--r-- 1 root root 0 Mar 1723:38 c.txt
-rw-r--r-- 1 root root 0 Mar 1723:38 d.txt
#backup参数:有yes|no两个选项
ansible 192.168.1.232 -m copy -a "src=/root/script/ dest=/tmp/script/owner=root group=root mode=0644 backup=yes"
提示:例如,src和dest同时有个a.txt文件,如果在src修改了a.txt  再执行copy的时候,dest就会生成一个备份

[iyunv@localhost script]# ll
total 4
-rw-r--r-- 1 root root 12 Mar 18 03:09 a.txt
-rw-r--r-- 1 root root  0 Mar 18 03:08 a.txt.2015-03-18@03:09~
#因为a.txt被修改过了,所以生成了一个备份

[iyunv@localhost script]# cat a.txt  #这里被修改过,然后copy过来的
hello world
[iyunv@localhost script]# cat a.txt.2015-03-18\@03\:09~ #备份的a.txt默认没有内容



file模块
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
#使用file模块,更改文件的用户和权限
[iyunv@ansibleserver ~]# ansible 192.168.1.232 -m file -a "dest=/tmp/a.txtmode=600"
#查看更改情况
[iyunv@localhost tmp]# ll
total 8
-rw-------  1 root root    0 Mar 17 23:38 a.txt

#创建目录,类似mkdir –p
[iyunv@ansibleserver ~]# ansible 192.168.1.232 -m file -a"dest=/tmp/to/c mode=755 owner=root group=root state=directory"

#查看创建情况
[iyunv@localhost c]# pwd
/tmp/to/c
#删除文件或者目录
[iyunv@ansibleserver ~]# ansible 192.168.1.232 -m file -a"dest=/tmp/a.txt state=absent"
192.168.1.232 | success >> {
    "changed": true,
    "path":"/tmp/a.txt",
    "state":"absent"
}

#查看删除情况
[iyunv@localhost tmp]# ll
total 8
-rw-r--r--  1 root root    0 Mar 17 23:38 b.txt
drwxr-xr-x  2 root root 4096 Mar 1723:38 script
drwxr-xr-x  3 root root 4096 Mar 1723:53 to
-rw-------. 1 root root    0 Dec 2819:45 yum.log



cron模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#注意:cron是为远程主机定义任务计划的
#批量定义远程主机上的定时任务
#首先我们在本地的/etc/ansible/目录下定义一个cron.yml文件
- hosts: 192.168.1.232  #远程主机IP
  remote_user: root    #指定执行的用户
  tasks:                #任务
    - name: cron       #任务名称
      cron: name='cp file' minute=1job='/usr/bin/tmp/script/test.sh'
提示:name 为注释名称,minute为执行任务的时间间隔,job为执行的脚本

#定义好之后,我们执行下ansible-playbook命令
[iyunv@ansibleserver ansible]# ansible-playbook cron.yml
PLAY RECAP********************************************************************
192.168.1.232              :ok=2    changed=1    unreachable=0    failed=0
#出现ok=2 change=1,代表已经在远程机子上做好定时任务了

#在远程主机上查看:
[iyunv@ansible-client script]# crontab -l
#Ansible: cp file
1 * * * * /usr/bin/tmp/script/test.sh



1
2
3
实例2:
目的:在指定节点上定义一个计划任务,每隔3分钟到主控端更新一次时间
命令:ansible all-m cron -a 'name="custom job" minute=*/3 hour=* day=* month=*weekday=* job="/usr/sbin/ntpdate 172.16.254.139"'



synchronize模块
1
2
3
4
5
6
7
8
#先声明下,使用rsync 模块,远程主机系统必须安装rsync 包,否则无法使用这个模块
#先给远程机装个rsync吧
[iyunv@ansibleserver ~]# ansible 192.168.1.232 -m yum -a 'name=rsyncstate=latest'
#再次验证下rsync是否安装成功
[iyunv@ansibleserver ~]# ansible 192.168.1.232 -a "which rsync"
192.168.1.232 | success | rc=0 >>
/usr/bin/rsync
#看来没问题了!



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#看下使用的参数
[iyunv@ansibleserver ~]# ansible-doc -s synchronize
- name: Uses rsync to make synchronizing file paths in your playbooks quickand easy.
action: synchronize
archive                # 是否采用归档模式同步,即以源文件相同属性同步到目标地址
checksum               # 是否效验
compress               # 是否压缩
copy_links             # 同步的时候是否复制连接
delete                 # 删除源中没有而目标存在的文件
dest=                  # 目标地址
dest_port              # 目标接受的端口
dirs                   # 以非递归的方式传输目录
existing_only          # Skipcreating new files on receiver.
group                  # Preservegroup
links                  # Copysymlinks as symlinks.
mode                   # 模式,rsync 同步的方式 PUSH\PULL
recursive              # 是否递归 yes/no
rsync_opts             # 使用rsync 的参数
rsync_path             # 服务的路径(源码编译时需指定)
rsync_timeout          # Specify a--timeout for the rsync command in seconds.
set_remote_user        # put user@for the remote paths. If you have a custom ssh config to define the remote userfor
src=\'#\'" 实例:将ansible端/tmp/目录下的script同步到232机子的/tmp/目录下面
[iyunv@ansibleserver ~]# ansible 192.168.1.232 -m synchronize -a 'src=/tmp/scriptdest=/tmp/'
192.168.1.232 | success >> {
    "changed": true,
    "cmd": "rsync--delay-updates -FF --compress --archive --rsh 'ssh  -o StrictHostKeyChecking=no'--out-format='<<CHANGED>>%i %n%L' \"/tmp/script\"\"root@192.168.1.232:/tmp/\"",
    "msg":"cd+++++++++ script/\n<f+++++++++ script/a.txt\n",
    "rc": 0,
    "stdout_lines": [
        "cd+++++++++script/",
        "<f+++++++++script/a.txt"
    ]
}
#注意:要想ansible端于远程端的文件保持一致,最好用delete=yes参数
因为,有时候在ansible的目录下删除了某个文件,若不加delete=yes参数的话,远程端的目录下仍然保留有旧的文件!服务管理#启动client的httpd服务
[iyunv@ansibleserver ~]# ansible 192.168.1.232 -m service -a"name=httpd state=started"
192.168.1.232 | success >> {
    "changed": true,
    "name":"httpd",
    "state":"started"
}
#注意:state的状态有:started   restarted   stoped
#client端查看情况
[iyunv@localhost ~]# netstat -lntup|grep httpd
tcp        0      0 :::80                       :::*                        LISTEN      1565/httpd
收集系统信息 #收集主机的所有系统信息
[iyunv@ansibleserver ~]# ansible 192.168.1.232 -m setup
#收集系统信息并以主机名为文件名分别保存在/tmp/facts目录
[iyunv@ansibleserver facts]# ansible 192.168.1.232 -m setup --tree/tmp/facts
[iyunv@ansibleserver facts]# ll
total 12
-rw-r--r-- 1 root root 8656 Mar 18 00:25 192.168.1.232
#收集系统内存相关信息
[iyunv@ansibleserver ~]# ansible 192.168.1.232 -m setup -a'filter=ansible_*_mb'
192.168.1.232 | success >> {
    "ansible_facts": {
       "ansible_memfree_mb": 299,
       "ansible_memtotal_mb": 490,
       "ansible_swapfree_mb": 2047,
       "ansible_swaptotal_mb": 2047
    },
    "changed": false
}
#收集网卡信息
[iyunv@ansibleserver ~]# ansible 192.168.1.232 -m setup -a'filter=ansible_eth[0-2]'
playbook
管理复杂任务
对于需反复执行的、较为复杂的任务,我们可以通过定义 Playbook 来搞定。Playbook 是 Ansible 真正强大的地方,它允许使用变量、条件、循环、以及模板,也能通过角色及包含指令来重用既有内容。下面我们来看看一些具体的实例。安装php软件实例#首先在/etc/ansible目录下建立一个php.yaml的文件
[iyunv@ansibleserver ansible]# vim php.yaml
- hosts: 192.168.1.232  #主机名,如果是全部主机,可以用all
  remote_user: root    #指定执行操作的用户
  tasks:                #任务
    - name: php installing   #起个任务的名字
      yum: name=php state=present #利用yum模块,安装软件的包名为php
参数:present为安装  absent为卸载
提示:注意对齐的格式,不然会出错
  
#用ansible-playbook 参数调用php.yaml
[iyunv@ansibleserver ansible]# ansible-playbook php.yaml
PLAY [192.168.1.232]**********************************************************
GATHERING FACTS***************************************************************
ok: [192.168.1.232]
TASK: [php installing]********************************************************
changed: [192.168.1.232]
PLAY RECAP ********************************************************************
192.168.1.232             : ok=2    changed=1    unreachable=0    failed=0
#看到结果,ok=2   changed=1 说明客户机(232)上的php安装成功了!创建cron定时计划#建立一个cron.yaml文件,然后每月10号来运行/root/dd.sql脚本
#cron定时任务参数
# Ensure a job that runs at 2 and 5 exists.
# Creates an entry like "* 5,2 * * ls -alh > /dev/null"
- cron: name="check dirs" hour="5,2" job="ls -alh> /dev/null"
# Ensure an old job is no longer present. Removes any job that is prefixed
# by "#Ansible: an old job" from the crontab
- cron: name="an old job" state=absent
  
# Creates an entry like "@reboot /some/job.sh"
- cron: name="a job for reboot" special_time=rebootjob="/some/job.sh"
  
# Creates a cron file under /etc/cron.d
- cron: name="yum autoupdate" weekday="2" minute=0hour=12
        user="root"job="YUMINTERACTIVE=0 /usr/sbin/yum-autoupdate"
       cron_file=ansible_yum-autoupdate
# Removes a cron file from under /etc/cron.d
- cron:cron_file=ansible_yum-autoupdate state=absent
            



运维网声明 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-51018-1-1.html 上篇帖子: 自动化运维 Ansible 下篇帖子: 自动化运维工具ansible playbook和roles的使用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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