分析家 发表于 2018-7-30 12:56:19

Ansible 安装 简单使用

  安装ansible
  只需要将管理节点安装ansible ,被管理节点不需要安装
  但如果使用yum安装的话,必须配置epel源
  rpm -Uvhhttp://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvhhttp://dl.fedoraproject.org/pub/epel/5Server/x86_64/epel-release-5-4.noarch.rpm  rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
  安装依赖关系包
  yum install python-dev python-yaml python-paramikopython-jinja2 git
  使用yum安装absible
  # yum installansible
  Loaded plugins: fastestmirror, product-id,security, subscription-manager
  This system is not registered to Red HatSubscription Management. You can use subscription-manager to register.
  Loading mirror speeds from cached hostfile
  *epel: mirrors.yun-idc.com
  Setting up Install Process
  Resolving Dependencies
  --> Running transaction check
  ---> Package ansible.noarch 0:1.7-1.el6will be installed
  --> Processing Dependency:python-setuptools for package: ansible-1.7-1.el6.noarch
  --> Processing Dependency: python-paramikofor package: ansible-1.7-1.el6.noarch
  --> Processing Dependency: python-keyczarfor package: ansible-1.7-1.el6.noarch
  --> Processing Dependency: python-httplib2for package: ansible-1.7-1.el6.noarch
  --> Processing Dependency: python-crypto2.6for package: ansible-1.7-1.el6.noarch
  --> Running transaction check
  ---> Package python-crypto2.6.x86_640:2.6.1-1.el6 will be installed
  ---> Package python-httplib2.noarch0:0.7.7-1.el6 will be installed
  ---> Package python-keyczar.noarch 0:0.71c-1.el6will be installed
  --> Processing Dependency: python-pyasn1for package: python-keyczar-0.71c-1.el6.noarch
  ---> Package python-paramiko.noarch0:1.7.5-2.1.el6 will be installed
  ---> Package python-setuptools.noarch0:0.6.10-3.el6 will be installed
  --> Running transaction check
  ---> Package python-pyasn1.noarch0:0.0.12a-1.el6 will be installed
  --> Finished Dependency Resolution
  Dependencies Resolved
  ===============================================================================

  Package                                             Arch                                       Version                                              Repository                              >  ===============================================================================
  Installing:
  ansible                                             noarch                                    1.7-1.el6                                          epel                                     874 k
  Installing for dependencies:
  python-crypto2.6                                    x86_64                                     2.6.1-1.el6                                          epel                                     530 k
  python-httplib2                                        noarch                                     0.7.7-1.el6                                           epel                                       70 k
  python-keyczar                                        noarch                                     0.71c-1.el6                                          epel                                     219 k
  python-paramiko                                       noarch                                     1.7.5-2.1.el6                                       base                                    728 k
  python-pyasn1                                       noarch                                     0.0.12a-1.el6                                        base                                       70 k
  python-setuptools                                     noarch                                     0.6.10-3.el6                                       base                                     336 k
  Transaction Summary
  ===============================================================================
  Install      7 Package(s)

  Total download>
  Installed>  Is this ok : y
  Downloading Packages:
  (1/7): ansible-1.7-1.el6.noarch.rpm                                                                                                                                             |874 kB   00:18
  (2/7):python-crypto2.6-2.6.1-1.el6.x86_64.rpm                                                                                                                                    | 530 kB   00:08
  (3/7):python-httplib2-0.7.7-1.el6.noarch.rpm                                                                                                                                     |70 kB   00:01
  (4/7):python-keyczar-0.71c-1.el6.noarch.rpm                                                                                                                                    | 219 kB   00:03
  (5/7):python-paramiko-1.7.5-2.1.el6.noarch.rpm                                                                                                                                    |728 kB   00:15
  (6/7):python-pyasn1-0.0.12a-1.el6.noarch.rpm                                                                                                                                     |70 kB    00:01
  (7/7):python-setuptools-0.6.10-3.el6.noarch.rpm                                                                                                                                  | 336 kB   00:09
  -------------------------------------------------------------------------------
  Total                                                                                                                                                                     40 kB/s | 2.8 MB   01:11
  Running rpm_check_debug
  Running Transaction Test
  Transaction Test Succeeded
  Running Transaction
  Installing : python-setuptools-0.6.10-3.el6.noarch                                                                                                                                                 1/7
  Installing : python-paramiko-1.7.5-2.1.el6.noarch                                                                                                                                                2/7
  Installing : python-pyasn1-0.0.12a-1.el6.noarch                                                                                                                                                    3/7
  Installing : python-keyczar-0.71c-1.el6.noarch                                                                                                                                                      4/7
  Installing : python-crypto2.6-2.6.1-1.el6.x86_64                                                                                                                                                   5/7
  Installing : python-httplib2-0.7.7-1.el6.noarch                                                                                                                                                    6/7
  Installing : ansible-1.7-1.el6.noarch                                                                                                                                                            7/7
  Verifying:python-keyczar-0.71c-1.el6.noarch                                                                                                                                                   1/7
  Verifying:python-httplib2-0.7.7-1.el6.noarch                                                                                                                                                    2/7
  Verifying:ansible-1.7-1.el6.noarch                                                                                                                                                      3/7
  Verifying : python-crypto2.6-2.6.1-1.el6.x86_64                                                                                                                                              4/7
  Verifying:python-pyasn1-0.0.12a-1.el6.noarch                                                                                                                                                    5/7
  Verifying:python-paramiko-1.7.5-2.1.el6.noarch                                                                                                                                                   6/7
  Verifying:python-setuptools-0.6.10-3.el6.noarch                                                                                                                                                 7/7
  Installed:
  ansible.noarch 0:1.7-1.el6
  Dependency Installed:
  python-crypto2.6.x86_64 0:2.6.1-1.el6    python-httplib2.noarch 0:0.7.7-1.el6python-keyczar.noarch 0:0.71c-1.el6python-paramiko.noarch 0:1.7.5-2.1.el6python-pyasn1.noarch 0:0.0.12a-1.el6
  python-setuptools.noarch 0:0.6.10-3.el6
  Complete!
  
  
  软件包:
  ansible
  PyYAML
  libyaml
  python-babel
  python-crypto
  python-crypto2.6
  python-httplib2 python-jinja2
  python-keyczar
  python-paramiko
  python-pyasn1
  python-setuptools
  
  
  ansbible配置文件
  安装后的配置文件位于/etc/ansible
  # pwd
  /etc/ansible
  # ll
  -rw-r--r--. 1 root root 5507 May1 17:53 ansible.cfg   #主配置文件
  -rw-r--r--. 1 root root965 Mar 15 13:07 hosts         #目标主机
  目标被管理主机要定义在host中,否则只能一个一个去手写了
  配置文件ansible.cfg:
  hostfile= /etc/ansible/hosts
  #默认目标主机文件
  library = /usr/share/ansible
  #库文件,装载模块时候其模块装载的路径,因为ansible是模块化的工具
  module_name =command
  #模块的名称,意思是没有使用指定模块的时候其默认使用的模块,默认是命令,意为只执行命令
  forks = 5
  #启动的子进程
  remote_port= 22
  #被管理主机的端口,默认为22,如果ssh端口有变动则需要修改此参数
  sudo_user = root
  #如果以普通用户登录进行sudo那么sudo的默认账户是root
  
  host配置文件
  一般按照安全性要求,应该禁止 root 登录,禁用密码登录,一律使用证书登录。
  ansible 支持通过 sudo 来执行
  所有管理主机,每个被管理主机都可以使用主机名或者ip地址都可以
  使用中括号可以组,紧跟着中括号下面的都是同一组内被管理的主机
  #grep -v "^#" hosts | grep -v "^$"
  green.example.com
  blue.example.com
  192.168.100.1
  192.168.100.10
  
  alpha.example.org
  beta.example.org
  192.168.1.100
  192.168.1.110
  www.example.com          #定义组的时候还可以使用通配符
  
  db01.intranet.mydomain.net
  db02.intranet.mydomain.net
  10.25.1.56
  10.25.1.57
  db--node.example.com
  使用absible批量管理节点
  假如这个配置文件不符合我们的意向,可以cp一份作为参考模板,方便以后借鉴
  这里我们定义2台被管理节点
  #使用[]指定分组
  # grep -v '#'hosts
  
  10.10.18.184
  10.10.18.185
  ssh秘钥
  要基于ssh工作,所以本地要生成一对ssh秘钥,然后让其基于秘钥认证方式来管理节点
  # ssh-keygen -t rsa -P ''
  Generatingpublic/private rsa key pair.
  Enterfile in which to save the key (/root/.ssh/id_rsa):
  /root/.ssh/id_rsaalready exists.
  Overwrite(y/n)? y
  Youridentification has been saved in /root/.ssh/id_rsa.
  Yourpublic key has been saved in /root/.ssh/id_rsa.pub.
  Thekey fingerprint is:
  52:74:7e:88:0a:b1:4f:22:a0:12:d8:79:b3:84:15:63root@node013
  Thekey's randomart image is:
  +------+
  |+.+E.. .      |
  |oo+.++. + .   |
  |o.o+o. o o .    |
  |...= o.   |
  |      + S       |
  |       .      |
  +-----------------+
  传送至被管理控节点上去
  cd /root/                                          #注意.ssh目录在ROOT家目录中
  #因为本地也是被监控节点之一
  #ssh-copy-id -i .ssh/id_rsa.pub root@10.10.18.184
  Warning: Permanentlyadded '10.10.18.184' (RSA) to the list of known hosts.
  root@10.10.18.184'spassword:
  Now try logging intothe machine, with "ssh 'root@10.10.18.184'", and check in:
  .ssh/authorized_keys
  to make sure we haven'tadded extra keys that you weren't expecting.
  ssh-copy-id -i .ssh/id_rsa.pub root@10.10.18.185
  ssh-copy-id -i .ssh/id_rsa.pub root@10.10.18.186
  
  尝试ansible是否可用
  先来man一下帮助手册
  ANSIBLE(1)   System administration commands   ANSIBLE(1)
  NAME
  ansible - run acommand somewhere else
  SYNOPSIS   #使用格式
  ansible <host-pattern> [-f forks] [-mmodule_name][-a args]
  主机模式,可以使用通配
  -f forks      表示我们可以明确指明启动多少个子进程去连接这些主机默认不定义为5个forks,如果节点比较多,可以将forks参数调大
  -m         指定使用的模块,每个模块都需要接受特定参数
  -a         来指定使用参数
  比如我们想让所有主机显示当前系统时间
  #ansible all -m command -a 'date'
  -mcommand 为指定模块,默认为command,如果默认执行命令的话,可以不加这个模块,如下所示
  #ansible all -a 'date'
  可以看到,时间不同步,但是命令是同时进行的,因为会发起N个子进程分别与每个被监控节点进行交互的
  如果节点比较多,可以将forks参数调大
  现在期望多个节点自动的去ntp服务器同步时间
  #ansible all -a 'crontab -l'
  可以看到只有一个节点存在,那么我们只要同步其余两台机器即可
  定义单独组XK
  #cat hosts
  
  10.10.18.184
  10.10.18.185
  10.10.18.186
  首先先来手动同步
  #ansible xk -a 'ntpdatetime.neusoft.com'
  将时间同步加入计划任务,我们可以使用命令追加的方式来写入计划任务
  #ansible crontab -a 'echo '00 * * * * * ' >>/var/spool/cron/root'
  这样一来可以实现,但是实在是太麻烦了,为此,ansible有专门计划任务管理的模块,可以使用内置模块来实现以最简单的命令方式来轻松批量管理
  定义crontab的参数有很多,可以在官方去查找帮助文档,这里不再一一介绍
  明确指定xk组,-m指定cron模块 -a指定参数
  # ansible xk -m cron -a 'name="sync time from time.neusoft.com" minute='*/3' job="/usr/sbin/ntpdate time.neusoft.com" '
  # ansible all -a 'crontab -l'
  参数说明:
  name=synctime from time.windows" 这是任务的名称注释会写到crontab里面去,也可以不指定,效果如下所示
  #crontab -l
  #Ansible:synctime from time.windows.com                     #因此上面多了一个注释信息
  */3* * * */usr/sbin/ntpdate time.windows.com
  minute='*/3 我们这里只需要关注分钟,只需要提供分钟的定义,省去的统统默认为*
  job="/usr/sbin/ntpdatetime.windows.com"   此为我们定义的计划任务执行的命令
  使用ansible批量安装程序
  我们工作场景中,如果想安装程序的话,只要我们的yum源无问题,那么则可以完全使用yum来安装
  那我们来man一下其模块
  man的使用方法比较独特如果想man查看ansible模块的话必须加符号.
  #man ansible.yum
  安装lrzsz
  #ansible xk -m yum -a 'name=lrzsz state=present'
  state=present 状态判断其是安装还是卸载的,这里也可以使用install
  #ansible xk -m yum -a 'name=lrzsz state=present'
  对于ansible来讲的话,很难生成yum配置文件,但可以使用wget将其批量下载
  #可以看到只有定义个没用装,因为本来就没有安装。。
  #ansible all -a 'rpm -q lrzsz '
页: [1]
查看完整版本: Ansible 安装 简单使用