soul颠覆了执着

导航

ansible简介,简单实用

Ansible 

ansilbe是实现自动化运维的工具,基于python开发,实现批量系统配置,批量程序部署,批量运行命令等功能。

ansible是基于模块工作的,自身是没有批量部署的能力。真正具有批量部署能力的是ansible的模块,ansbile只是提供一种框架

ansbile特点

模块化:使用特定的模块,完成特定的工作

支持自定义模块

基于Python语言开发的

不需要再被控端安装组件,也就是无需客户端,直接在服务端执行命令。

批量任务执行可以用脚本,也不需要传递到远程就可以执行。

ansible的基本架构:

1.链接插件用于连接主机,连接受控端。

2.核心模块连接主机实现操作,依靠模块做具体的事情。

3.自定义模块:根据自己的需求编写模块

4.插件是对模块功能的补充。

5.playbook,ansible功能的形象化描述。

6.host inventory 定义ansible需要的操作主机的范围

Ansible使用

服务端:10.220.5.63

两个被控端:10.220.5.65

                      10.220.5.66

一、安装ansible

[root@63 ~]# yum install ansible

查看版本

[root@63 ~]# ansible --version
ansible 2.6.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 2015

相关文档

[root@63~]# rpm -qc ansible
/etc/ansible/ansible.cfg
/etc/ansible/hosts

二、生成密钥

[root@63 ~]# ssh-keygen -t rsa
[root@63 ~]#  ssh-copy-id -i ~/.ssh/id_rsa.pub 10.220.5.63
[root@63 ~]#  ssh-copy-id -i ~/.ssh/id_rsa.pub 10.220.5.65
[root@63~]#  ssh-copy-id -i ~/.ssh/id_rsa.pub 10.220.5.66
[root@63 ~]#  ssh-copy-id -i ~/.ssh/id_rsa.pub 10.220.5.63 <<<自己也作为受控端
[root@63 ~]#  ssh-copy-id -i ~/.ssh/id_rsa.pub 10.220.5.65
[root@63~]#  ssh-copy-id -i ~/.ssh/id_rsa.pub 10.220.5.66

在主控端/etc/hosts中添加受控端主机信息

[root@63 ~]# vim /etc/hosts

10.220.5.65 slave1
10.220.5.66 slave2

测试是否能免密码登录

[root@63 ~]# ssh slave1
Last login: Sat Nov 17 01:32:37 2018 from 10.220.5.63
[root@slave1 ~]# <<<为10.220.5.65主机

测试

[root@63 ~]# ssh slave1 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
    link/ether 00:0c:29:d1:a8:6a brd ff:ff:ff:ff:ff:ff
    inet 10.220.5.65/24 brd 10.220.5.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 10.220.5.233/24 scope global secondary ens33:1
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fed1:a86a/64 scope link 
       valid_lft forever preferred_lft forever

三、使用ansible管理其他节点

格式:ansible <host> [options]
选项:
         -m:指定模块名称
         -a:指定模块的具体参数
          -s:以sudo的方式运行操作
          -i:指定被管理节点的主机列表
          -f:一次连接几个主机进行操作(默认是5个主机)

查看ansible所支持的模块的信息

[root@63 ~]# ansible-doc -l
默认用command模块

查看一个模块的使用帮助

例:

[root@63 ~]# ansible-doc -s command
- name: Executes a command on a remote node
  command:
      argv:                  # Allows the user to provide the command as a list vs. a string.  Only the string
                               or the list form can be provided, not both.  One
                               or the other must be provided.
      chdir:                 # Change into this directory before running the command.
      creates:               # A filename or (since 2.0) glob pattern, when it already exists, this step will
                               *not* be run.
      free_form:             # (required) The command module takes a free form command to run.  There is no
                               parameter actually named 'free form'. See the
                               examples!
      removes:               # A filename or (since 2.0) glob pattern, when it does not exist, this step will
                               *not* be run.
      stdin:                 # Set the stdin of the command directly to the specified value.
      warn:                  # If command_warnings are on in ansible.cfg, do not warn about this particular
                               line if set to `no'.

 

模块

1. command

默认模块
作用:在各个远程主机上执行命令,但是不能传递参数和变量,不支持特殊字符

格式:ansible 目标 -m command -a "命令"
说明:
目标:目标可以是一个主机组,也可以是hosts文件中的一个或者多个主机,也可是all
-m command:表示这次ansible操作是基于command模块实现
-a "命令":表示是在管理节点上要执行的命令

例子:获取受控节点主机名

[root@63~]# ansible webserver -m command -a "hostname"
10.220.5.65 | SUCCESS | rc=0 >>
slave1

10.220.5.66 | SUCCESS | rc=0 >>
slave2

也可以单独对一个受控端执行命令

[root@63 ~]# ansible 10.220.5.65 -m command -a "hostname"
10.220.5.65 | SUCCESS | rc=0 >>
slave1

2.shell模块

也是在各个远程主机上执行命令,但shell模块可以支持特殊字符,command不支持

chdir:指定一个目录,在执行对应的命令之前,会先进入到 chdir 参数指定的目录中。

creates:数指定一个文件,当指定的文件存在时,就不执行对应命令

removes:指定一个文件,当指定的文件不存在时,就不执行对应命令

executable:默认情况下,shell 模块会调用远程主机中的 /bin/sh 去执行对应的命令,通常情况下,远程主机中的默认 shell 都是 bash。如果你想要使用其他类型的 shell 执行命令,则可以使用此参数指定某种类型的 shell 去执行对应的命令。指定 shell 文件时,需要使用绝对路径。

例子:获取65主机/tmp所有文件

[root@63 ~]# ansible 10.220.5.65 -m shell -a "ls /tmp/*"
10.220.5.65 | SUCCESS | rc=0 >>
/tmp/mysql3306.sock
/tmp/mysql3306.sock.lock

 3.script

               作用:在远程主机执行主控端存的shell脚本(相当于scp+shell)

[root@63 ~]# ansible all -m script -a "/root/shell.sh"
10.220.5.66 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 10.220.5.66 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 10.220.5.66 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}
10.220.5.65 | SUCCESS => {

4. cron

作用:在指定的时间运行指定命令(计划任务)

  cron:
      backup:       计划名称        
      cron_file:    替换客户端用户的计划任务的文件         
      day:          日      
      hour:         小时 
name: 设置计划任务的名称
disabled:当计划任务有名称时,我们可以根据名称使对应的任务”失效”(注释掉对应的任务)。
除了需要指定任务的名称,还需要同时指定任务的job 以及任务的时间设定,而且任务的时间设定必须和对应任务完全相同,
否则在注释任务的同时,任务的时间设定会被修改,除非你确定这样做。
      special_time  后面可加: reboot(重启后)、yearly(每年)、annually(每年,与yearly相同)、monthly(每月)、weekly(每周)、daily(每天)、hourly(每时)。             
      user:          设置当前计划任务属于哪个用户,如果不指定,默认为管理员用户       
      backup:        在修改或者删除计划任务时,会先对计划任务做备份,然后在进行修改或者删除            
      job:               用于指定计划任务中需要实际执行的命令或者脚本   
state: 当计划任务有名字时,可以根据名称修改或者删除对应的任务,如果需要删除计划任务时,需要将state的值设置为absent

例子1:每隔2分钟再tmp下的log.txt中追加一个信息

[root@63 ~]# ansible all -m cron -a 'minute="*/2" job="echo 123>>/tmp/log.txt" name="job1" state="present"'
10.220.5.66 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "job1"
    ]
}
10.220.5.65 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "job1"
    ]

例子2.在65主机上创建计划任务,任务名称为crontab day test ,任务每五天执行一次,任务内容输出为test

[root@63 ~]# ansible all -m cron -a " name='crontab day test' minute=1 day=*/3 job='echo test'"
10.220.5.65 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "job1", 
        "crontab day test"
    ]
}
10.220.5.66 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "job1", 
        "crontab day test"
    ]
}

 例子:.在 65主机上创建计划任务,任务名称为”special time test”,任务将在重启时执行,任务内容为输出 test 字符。

[root@63 ~]# ansible 10.220.5.65 -m cron -a " name='special time test' special_time=reboot job='echo test'"
10.220.5.65 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "job1", 
        "crontab day test", 
        "special time test"

例子:任务”special time test”已经存在于65主机中。,要删除这个计划任务,删除任务的同时可以进行备份。

[root@63~]# ansible 10.220.5.65 -m cron -a " name='special time test' state=absent backup=yes "
10.220.5.65 | SUCCESS => {
    "backup_file": "/tmp/crontabNaCVq8", 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "job1", 
        "crontab day test"

 检查一下在65主机上创建的计划任务

[root@63~]# ansible 10.220.5.65 -a "crontab -l"
10.220.5.65 | SUCCESS | rc=0 >>
#Ansible: job1
*/2 * * * * echo 123>>/tmp/log.txt
#Ansible: crontab day test
1 * */3 * * echo test

5.copy

作用:文件的复制
子命令:
src:原文件路径
dest:目标文件路径
mode:指定权限
backup:有目标文件存在,先对原始文件做备份,然后进行覆盖
force:强制执行覆盖
owner:指定复制后的文件的属主
content:替代src

 others:所有的file模块里的选项都可以在这里使用

 例子1:将主控端/etc/passwd 文件,复制到65主机/tmp/目录下,并该名为psswd1

[root@63 ~]# ansible 10.220.5.65 -m copy -a 'src="/etc/passwd" dest=/tmp/passwd1'
10.220.5.65 | SUCCESS => {
    "changed": false, 
    "checksum": "be868aa216508a02c3e0c64186e77f191f27eeca", 
    "dest": "/tmp/passwd1", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "path": "/tmp/passwd1", 
    "size": 1565, 
    "state": "file", 
    "uid": 0

6.user,group

home:指定用户的家目录,需要与createhome配合使用。
groups:指定用户的属组。
 uid:指定用的uid。
 password:指定用户的密码。
注意:指定password参数时,不能使用明文密码,因为后面这一串密码会被直接传送到被管理主机的/etc/shadow文件中,所以需要先将密码字符串进行加密处理。然后将得到的字符串放到password中即可。
name:指定用户名。
createhome:是否创建家目录 yes|no。
system:是否为系统用户。
 remove:当state=absent时,remove=yes则表示连同家目录一起删除,等价于userdel -r。
 state:是创建还是删除。(present,absent)
shell:指定用户的shell环境。
 generate_ssh_key:是否为相关用户生成SSH密钥。 这不会覆盖现有的SSH密钥。
ssh_key_bits:可选择指定要创建的SSH密钥中的位数。
 ssh_key_passphrase:设置SSH密钥的密码。 如果没有提供密码,SSH密钥将默认没有密码。
ssh_key_file:指定SSH密钥文件名(可选)。 如果这是一个相对的文件名,那么它将是相对于用户的主目录。
 ssh_key_type:指定要生成的SSH密钥的类型(可选)。 可用的SSH密钥类型将取决于目标主机上的实现。
例子:在65主机上创建用户名为qq,家目录为/home/,属组为redis,uid为1010

[root@63 ~]# ansible 10.220.5.65 -m user -a "name=qq home=/home/qq createhome=yes groups=redis uid=1010"
10.220.5.65 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 1010, 
    "groups": "redis", 
    "home": "/home/qq", 
    "name": "qq", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1010

例子:在65主机上创建用户组httpd

[root@66 ~]# ansible 10.220.5.65 -m group -a "name=httpd state=present"
10.220.5.65 | SUCCESS => {
    "changed": true, 
    "gid": 1012, 
    "name": "httpd", 
    "state": "present", 
    "system": fals

7.yum

RedHat / CentOS包管理工具
config_file:yum的配置文件 
disable_gpg_check:关闭gpg_check 
 disablerepo:不启用某个源 
enablerepo:启用某个源
 name:要进行操作的软件包的名字,默认最新的程序包,指明要安装的程序包,可以带上版本号,也可以传递一个url或者一个本地的rpm包的路径
state:状态(present,absent,latest),表示是安装还卸载
   present:默认的,表示为安装
   lastest: 安装为最新的版本
   absent:表示删除
例子:在65主机安装httpd

[root@63~]# ansible 10.220.5.65 -m yum -a "name=httpd state=present"
10.220.5.65 | SUCCESS => {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    
  ...................................
}

8.file模块

作用:实现文件的各种管理操作
force:需要在两种情况下强制创建软链接,一种是源文件不存在但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
 group:属组
mode:权限
owner:属主
 path:路径
recurse:递归的设置文件的属性,只对目录有效
src:要被链接的源文件的路径,只应用于state=link的情况
 dest:被链接到的路径,只应用于state=link的情况
 state:
   directory:如果目录不存在,创建目录
   file:即使文件不存在,也不会被创建
   link:创建软链接
   hard:创建硬链接
   touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
   absent:删除目录、文件或者取消链接文件
例子:在65主机上创建文件abc.sh

[root@63 ~]# ansible 10.220.5.65 -m file -a 'state=touch path=/tmp/abc.sh'
10.220.5.65 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/abc.sh", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "size": 0, 
    "state": "file", 
    "uid": 0

将65主机/tmp/abc.sh 链接为/tmp/abc

[root@63 ~]# ansible 10.220.5.65 -m file -a 'state=link src=/tmp/abc.sh path=/tmp/abc'
10.220.5.65 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/abc", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "size": 11, 
    "src": "/tmp/abc.sh", 
    "state": "link", 
    "uid": 0
}

9. service

作用:管理服务

子命名;
name:指定服务的名称
enabled:指定服务是否开机自启动
state:指定服务最后的状态 started stopped reloaded restarted
runlevel:指定运行级别

 例子:在65主机上重启网卡

[root@66  ~]# ansible 10.220.5.65 -m service -a "name=network state=restarted"
10.220.5.65 | SUCCESS => {
    "changed": true, 
    "name": "network", 
    "state": "started", 

10.ping

作用:做ping测试

例子:对64主机做ping测试

[root@66  ~]# ansible 10.220.5.64 -m ping
10.220.5.64 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

11.mount模块

配置挂载点
选项: 

dumpfstype:必选项,挂载文件的类型  

 name:必选项,挂载点 

opts:传递给mount命令的参数

src:必选项,要挂载的文件 

state:必选项 

present:只处理fstab中的配置

 absent:删除挂载点 

mounted:自动创建挂载点并挂载之 

umounted:卸载

 例子:在64主机上挂载光盘到/mnt

[root@66  ~]# ansible 10.220.5.64 -m mount -a "name=/mnt src=/dev/sr0 fstype=iso9660 state=mounted"
10.220.5.64 | SUCCESS => {
    "changed": true, 
    "dump": "0", 
    "fstab": "/etc/fstab", 
    "fstype": "iso9660", 
    "name": "/mnt", 

 

 

 

 

                

                     

 

posted on 2018-11-16 20:40  soul颠覆了执着  阅读(425)  评论(0编辑  收藏  举报