zoukankan      html  css  js  c++  java
  • ansible自动化工具使用

     

    1、服务端配置

    安装即可,无需启动,在安装ansible之前需要配置epel源

    [root@m01 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

    [root@m01 ~]# yum makecache

    [root@m01 ~]# yum install ansible -y    #安装ansible

    修改配置文件,指定主机组已经主机信息

    [root@m01 ~]# vim /etc/ansible/hosts        #修改配置文件/etc/ansible/hosts

    [test]    #主机组名,通过组名进行分类便于管理

    10.0.0.7 ansible_ssh_user=root ansible_ssh_pass=123456

    10.0.0.51 ansible_ssh_user=root ansible_ssh_pass=123456

    [root@Web01 ~]# vim /etc/ssh/ssh_config    

    StrictHostKeyChecking no    #配置服务端ssh配置文件,不询问是否连接

    2、ansible模块使用

    查看帮助ansable-doc -s user

    2.1、测试主机组下的主机是否存活模块ping

    [root@m01 ~]# ansible test -m ping

    10.0.0.51 | SUCCESS => {

    "changed": false,

    "ping": "pong"

    }

    10.0.0.7 | SUCCESS => {

    "changed": false,

    "ping": "pong"

    }

    2.2、命令模块command(不支持管道和分号)

    [root@m01 ~]# ansible test -m command -a "date"

    10.0.0.7 | SUCCESS | rc=0 >>

    Thu May 17 17:54:36 CST 2018

     

    10.0.0.51 | SUCCESS | rc=0 >>

    Thu May 17 17:56:17 CST 2018

    2.3、模块shell(支持管道和分号)

    [root@m01 ~]# ansible test -m shell -a 'ifconfig eth0|grep "inet addr:"'

    10.0.0.7 | SUCCESS | rc=0 >>

    inet addr:10.0.0.7 Bcast:10.0.0.255 Mask:255.255.255.0

     

    10.0.0.51 | SUCCESS | rc=0 >>

    inet addr:10.0.0.51 Bcast:10.0.0.255 Mask:255.255.255.0

    2.4、用户管理模块user

    模块参数说明:

    name:指定用户名

    password:设定用户密码,password参数需要接受md5加密后的值

    state:用户状态,默认为present,present:表示添加用户,absent:表示删除用户

    update_password:修改用户密码,always:新密码和旧密码不同时进行修改,on_create:为新创建的用户指定密码

    createhome:创建家目录,yes:默认项,即创建用户默认是有家目录的,no:创建用户时不创建家目录

    remove:yes:删除用户家目录,需要指定此参数

    no:默认项,删除用户时默认不删除用户的家目录

    system:yes:默认创建为普通用户,而非系统用户,如果不指定默认生成的选项有:

    home:创建家目录

    shell:创建默认的shell为/bin/bash

    system:默认创建为普通用户,而非系统用户,指定是用yes

    user模块中的password是需要经过md5加密的

    [root@m01 ~]# echo 123456|openssl passwd -1 -stdin

    $1$66TtUpmH$Fkcg13VjPwXXBPZoEpHgB/

    添加用户

    [root@m01 ~]# ansible test -m user -a 'name=abc password=$1$66TtUpmH$Fkcg13VjPwXXBPZoEpHgB/ createhome=no shell=/bin/sh system=yes uid=888'

    10.0.0.7 | SUCCESS => {

    "changed": true,

    "comment": "",

    "create_home": false,

    "group": 499,

    "home": "/home/abc",

    "name": "abc",

    "password": "NOT_LOGGING_PASSWORD",

    "shell": "/bin/sh",

    "state": "present",

    "system": true,

    "uid": 888

    }

    10.0.0.51 | SUCCESS => {

    "changed": true,

    "comment": "",

    "create_home": false,

    "group": 495,

    "home": "/home/abc",

    "name": "abc",

    "password": "NOT_LOGGING_PASSWORD",

    "shell": "/bin/sh",

    "state": "present",

    "system": true,

    "uid": 888

    }

    删除用户

    [root@m01 ~]# ansible test -m user -a 'name=abc state=absent'

    10.0.0.7 | SUCCESS => {

    "changed": true,

    "force": false,

    "name": "abc",

    "remove": false,

    "state": "absent"

    }

    10.0.0.51 | SUCCESS => {

    "changed": true,

    "force": false,

    "name": "abc",

    "remove": false,

    "state": "absent"

    }

    2.5、任务计划模块cron

    模块参数说明:

    模块参数详解:

    state:present:创建任务,absent:删除任务

    backup:对远程主机上的原任务计划内容修改之前做备份

    job:要执行的任务

    name:该任务的描述(必须项)

    user:以哪个用户的身份运行

    minute:分钟(0-59,*,*/2,……),不写默认为*

    hour:小时(0-23,*,*/2,……),不写默认为*

    day:日(1-31,*,*/2,……),不写默认为*

    month:月(1-12,*,*/2,……),不写默认为*

    weekday:周(0-7,*,……),不写默认为*

    创建一个定时任务,每5分钟同步一次时间

    [root@m01 ~]# ansible test -m cron -a 'name="every 5 minute sync time" minute=*/5 job="/usr/sbin/ntpdate ntp2.aliyun.com"'

    10.0.0.7 | SUCCESS => {

    "changed": true,

    "envs": [],

    "jobs": [

    "every 5 minute sync time"

    ]

    }

    10.0.0.51 | SUCCESS => {

    "changed": true,

    "envs": [],

    "jobs": [

    "every 5 minute sync time"

    ]

    }

    删除该定时任务

    [root@m01 ~]# ansible test -m cron -a 'name="every 5 minute sync time" minute=*/5 job="/usr/sbin/ntpdate ntp2.aliyun.com" state=absent'

    10.0.0.51 | SUCCESS => {

    "changed": true,

    "envs": [],

    "jobs": []

    }

    10.0.0.7 | SUCCESS => {

    "changed": true,

    "envs": [],

    "jobs": []

    }

    2.6、远程复制备份模块copy

    模块参数说明:

    src:指定源文件路径,可以是相对路径,也可以是绝对路径,可以是目录(并非是必须的,可以使用content,直接生成文件内容)

    dest=:指定目标文件路径,只能是绝对路径,如果src是目录,此项必须是目录

    owner:指定属主

    group:指定属组

    mode:指定权限,可以以数字指定比如0644

    content:代替src,直接往dest文件中写内容,可以引用变量,也可以直接使用inventory中的主机变量

    backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no

    force:yes:默认项,如果目标主机包含该文件,但内容不同,则强制覆盖

    no:则只有当目标主机的目标位置不存在该文件时,才复制

    directory_mode:递归的设定目录的权限,默认为系统默认权限

    复制本地文件到远程主机并对原文件进行备份

    [root@localhost tmp]# ansible erp -m copy -a 'src=/tmp/abc.txt dest=/tmp/ backup=yes'

    192.168.10.10 | SUCCESS => {

    "backup_file": "/tmp/abc.txt.2017-02-07@10:55:31~",

    "changed": true,

    "checksum": "13520f9e1a6f0b2ca9557d85825616c3680b4edc",

    "dest": "/tmp/abc.txt",

    "gid": 0,

    "group": "root",

    "md5sum": "baae19d280afe4e2df1799daa37bebba",

    "mode": "0644",

    "owner": "root",

    "size": 18,

    "src": "/root/.ansible/tmp/ansible-tmp-1486436129.98-98537282809491/source",

    "state": "file",

    "uid": 0

    }

    192.168.10.6 | SUCCESS => {

    "backup_file": "/tmp/abc.txt.2017-02-07@10:55:31~",

    "changed": true,

    "checksum": "13520f9e1a6f0b2ca9557d85825616c3680b4edc",

    "dest": "/tmp/abc.txt",

    "gid": 0,

    "group": "root",

    "md5sum": "baae19d280afe4e2df1799daa37bebba",

    "mode": "0644",

    "owner": "root",

    "size": 18,

    "src": "/root/.ansible/tmp/ansible-tmp-1486436130.19-187127825454744/source",

    "state": "file",

    "uid": 0

    }

    向远程主机的文件中写内容,会把原内容覆盖掉

    [root@localhost tmp]# ansible erp -m copy -a 'content=" My age is 26" dest=/tmp/abc.txt'

    192.168.10.6 | SUCCESS => {

    "changed": true,

    "checksum": "e1cbbec8927a295a767fa44e91dea6eeafa5a4f4",

    "dest": "/tmp/abc.txt",

    "gid": 0,

    "group": "root",

    "md5sum": "55ec30ce5102aa8716b75ab5e98163a7",

    "mode": "0644",

    "owner": "root",

    "size": 13,

    "src": "/root/.ansible/tmp/ansible-tmp-1486436336.05-254449966786008/source",

    "state": "file",

    "uid": 0

    }

    192.168.10.10 | SUCCESS => {

    "changed": true,

    "checksum": "e1cbbec8927a295a767fa44e91dea6eeafa5a4f4",

    "dest": "/tmp/abc.txt",

    "gid": 0,

    "group": "root",

    "md5sum": "55ec30ce5102aa8716b75ab5e98163a7",

    "mode": "0644",

    "owner": "root",

    "size": 13,

    "src": "/root/.ansible/tmp/ansible-tmp-1486436348.0-188270058505341/source",

    "state": "file",

    "uid": 0

    }

    2.7、远程文件管理模块file

    模块参数说明:

    owner:修改属主

    group:修改属组

    mode:修改权限

    path=:要修改文件的路径

    recurse:递归的设置文件的属性,只对目录有效

    yes:表示使用递归设置

    state:

    touch:创建一个新的空文件

    directory:创建一个新的目录,当目录存在时不会进行修改

    link:创建软连接,结果src一起使用此选项才生效

    hard:创建硬连接

    absent:删除文件,目录,软连接

    src:当state=link时,要被连接文件的源路径

    新建一个文件

    [root@localhost tmp]# ansible erp -m file -a 'path=/tmp/liuwei.txt state=touch'

    192.168.10.10 | SUCCESS => {

    "changed": true,

    "dest": "/tmp/liuwei.txt",

    "gid": 0,

    "group": "root",

    "mode": "0644",

    "owner": "root",

    "size": 0,

    "state": "file",

    "uid": 0

    }

    192.168.10.6 | SUCCESS => {

    "changed": true,

    "dest": "/tmp/liuwei.txt",

    "gid": 0,

    "group": "root",

    "mode": "0644",

    "owner": "root",

    "size": 0,

    "state": "file",

    "uid": 0

    }

    新建一个目录

    [root@localhost tmp]# ansible erp -m file -a 'path=/tmp/liuwei state=directory'

    192.168.10.10 | SUCCESS => {

    "changed": true,

    "gid": 0,

    "group": "root",

    "mode": "0755",

    "owner": "root",

    "path": "/tmp/liuwei",

    "size": 4096,

    "state": "directory",

    "uid": 0

    }

    192.168.10.6 | SUCCESS => {

    "changed": true,

    "gid": 0,

    "group": "root",

    "mode": "0755",

    "owner": "root",

    "path": "/tmp/liuwei",

    "size": 4096,

    "state": "directory",

    "uid": 0

    }

    删除文件或者目录

    [root@localhost tmp]# ansible erp -m file -a 'path=/tmp/liuwei.txt state=absent'

    192.168.10.10 | SUCCESS => {

    "changed": true,

    "path": "/tmp/liuwei.txt",

    "state": "absent"

    }

    192.168.10.6 | SUCCESS => {

    "changed": true,

    "path": "/tmp/liuwei.txt",

    "state": "absent"

    }

    递归设置文件的属主或者属组

    [root@localhost tmp]# ansible erp -m file -a 'path=/tmp/liuwei owner=root group=root recurse=yes'

    192.168.10.10 | SUCCESS => {

    "changed": true,

    "gid": 0,

    "group": "root",

    "mode": "0755",

    "owner": "root",

    "path": "/tmp/liuwei",

    "size": 4096,

    "state": "directory",

    "uid": 0

    }

    192.168.10.6 | SUCCESS => {

    "changed": true,

    "gid": 0,

    "group": "root",

    "mode": "0755",

    "owner": "root",

    "path": "/tmp/liuwei",

    "size": 4096,

    "state": "directory",

    "uid": 0

    }

    为文件设置软连接

    [root@localhost tmp]# ansible erp -m file -a 'src=/tmp/liuwei state=link path=/tmp/liuzhengwei'

    192.168.10.6 | SUCCESS => {

    "changed": true,

    "dest": "/tmp/liuzhengwei",

    "gid": 0,

    "group": "root",

    "mode": "0777",

    "owner": "root",

    "size": 11,

    "src": "/tmp/liuwei",

    "state": "link",

    "uid": 0

    }

    192.168.10.10 | SUCCESS => {

    "changed": true,

    "dest": "/tmp/liuzhengwei",

    "gid": 0,

    "group": "root",

    "mode": "0777",

    "owner": "root",

    "size": 11,

    "src": "/tmp/liuwei",

    "state": "link",

    "uid": 0

    }

    2.8、远程主机执行本地脚本模块script

    [root@localhost tmp]# ansible erp -m script -a '/tmp/a.sh'

    192.168.10.10 | SUCCESS => {

    "changed": true,

    "rc": 0,

    "stderr": "",

    "stdout": "Tue Feb 7 11:26:41 CST 2017 ",

    "stdout_lines": [

    "Tue Feb 7 11:26:41 CST 2017"

    ]

    }

    192.168.10.6 | SUCCESS => {

    "changed": true,

    "rc": 0,

    "stderr": "",

    "stdout": "Tue Feb 7 11:26:52 CST 2017 ",

    "stdout_lines": [

    "Tue Feb 7 11:26:52 CST 2017"

    ]

    }

    2.9、收集远程主机信息模块setup

    收集可用的facts,收集每个节点的相关信息:架构信息,IP,时间,域名,网卡,MAC,主机名,CPU等信息。

    这些收集的信息,可以作为变量。

    [root@localhost tmp]# ansible erp -m setup

    2.10、安装模块yum

    模块参数说明:

    name:表示要安装软件包的名字,默认最新的程序包,指明要安装的程序包,可以带上版本号

    state:表示是安装还卸载

    present:默认的,表示为安装

    lastest:安装为最新的版本

    absent:表示删除

    2.11服务模块service

    模块参数说明:

    enabled:表示设置服务开机是否启动,取值为true或者false;enabled=yes

    name=:表示要控制哪一个服务

    state:

    started:表示现在就启动此服务

    stopped:表示现在关闭此服务

    restarted:表示重启此服务

    sleep:如果执行了restarted,在stop和start之间沉睡几秒

    runlevel:定义在哪些级别可以自启动

    arguments:表示向命令行传递的参数

    [root@localhost tmp]#ansible erp -m service -a 'enabled=on name=httpd state=started'

    2.12、文件编辑模块lineinfile

    模块参数说明:

    path:指定要修改的配置文件

    regexp:匹配要修改的内容

    line:要增加或者修改的内容

    state:

    absent:表示删除,当匹配到时进行删除

    present:表示增加,当匹配到时进行修改,当没有匹配到时在最后增加一行,默认为此项

    backrefs:

    no:表示如果没有匹配到,则增加line;如果匹配成功,则替换line;

    yes:表示如果没有匹配到,则不变line;如果匹配成功,则替换line;

    backup:

    no:表示如果没有匹配到,则增加line;如果匹配成功,则替换line;不备份原文件

    yes:表示如果没有匹配到,则增加line;如果匹配成功,则替换line;备份原文件

    insertafter(匹配的是此行):

    在匹配到的行之后添加一行

    insertbefore(匹配的是此行):

    在匹配到的行之前添加一行

    3、ansible剧本YAML

    playbook是由一个或多个"play"组成的列表。play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task定义好的角色。从根本上来讲,所有task无非是调用ansible的一个module。将多个play组织在一个playbook中,即可以让他们连同起来按事先编排的机制同唱一台大戏。

    剧本1:

    [root@m01 scripts]# vim first.yml

    - hosts: test    #指定主机组名

    tasks:    #任务

    - name: test host ping    #操作名称

    ping:    #ansible模块名称

    [root@m01 scripts]# ansible-playbook first.yml     #执行

    PLAY [test] *************************************************************************************************

    TASK [Gathering Facts] **************************************************************************************

    ok: [10.0.0.7]

    ok: [10.0.0.51]

    TASK [test host ping] ***************************************************************************************

    ok: [10.0.0.7]

    ok: [10.0.0.51]

    PLAY RECAP **************************************************************************************************

    10.0.0.51 : ok=2 changed=0 unreachable=0 failed=0

    10.0.0.7 : ok=2 changed=0 unreachable=0 failed=0

    剧本2:

    [root@m01 scripts]# vim second.yml

    - hosts: test

    tasks:

    - name: test host ping

    ping:

    - name: check hostname

    command: hostname

    [root@m01 scripts]# ansible-playbook second.yml

    PLAY [test] *************************************************************************************************

    TASK [Gathering Facts] **************************************************************************************

    ok: [10.0.0.7]

    ok: [10.0.0.51]

    TASK [test host ping] ***************************************************************************************

    ok: [10.0.0.7]

    ok: [10.0.0.51]

    TASK [check hostname] ***************************************************************************************

    changed: [10.0.0.7]

    changed: [10.0.0.51]

    PLAY RECAP **************************************************************************************************

    10.0.0.51 : ok=3 changed=1 unreachable=0 failed=0

    10.0.0.7 : ok=3 changed=1 unreachable=0 failed=0

    剧本3:

    [root@m01 scripts]# vim third.yml

    - hosts: test

    tasks:

    - name: test host ping

    ping:

    - name: check hostname

    command: hostname

    - name: sleep 10

    shell: sleep 10

    [root@m01 scripts]# ansible-playbook third.yml

    PLAY [test] *************************************************************************************************

    TASK [Gathering Facts] **************************************************************************************

    ok: [10.0.0.51]

    ok: [10.0.0.7]

    TASK [test host ping] ***************************************************************************************

    ok: [10.0.0.51]

    ok: [10.0.0.7]

    TASK [check hostname] ***************************************************************************************

    changed: [10.0.0.51]

    changed: [10.0.0.7]

    TASK [sleep 10] *********************************************************************************************

    changed: [10.0.0.7]

    changed: [10.0.0.51]

    PLAY RECAP **************************************************************************************************

    10.0.0.51 : ok=4 changed=2 unreachable=0 failed=0

    10.0.0.7 : ok=4 changed=2 unreachable=0 failed=0

    剧本4:

    [root@m01 scripts]# cat forth.yml

    - hosts: test

    tasks:

    - name: ping baidu.com

    shell: ping www.baidu.com -c4

    - name: install httpd

    yum: name=httpd state=present

    - name: start httpd

    service: name=httpd enabled=no state=restarted sleep=3

    [root@m01 scripts]# ansible-playbook forth.yml

    PLAY [test] *************************************************************************************************

    TASK [Gathering Facts] **************************************************************************************

    ok: [10.0.0.7]

    ok: [10.0.0.51]

    TASK [ping baidu.com] ***************************************************************************************

    changed: [10.0.0.51]

    changed: [10.0.0.7]

    TASK [install httpd] ****************************************************************************************

    ok: [10.0.0.51]

    ok: [10.0.0.7]

    TASK [start httpd] ******************************************************************************************

    changed: [10.0.0.7]

    changed: [10.0.0.51]

    PLAY RECAP **************************************************************************************************

    10.0.0.51 : ok=4 changed=2 unreachable=0 failed=0

    10.0.0.7 : ok=4 changed=2 unreachable=0 failed=0

    剧本5:

    利用ansible配置rsync服务端backup和客户端web01,在本地ansible服务器的配置文件中加入相应主机组,并在本地创建/srv/rsyncd.conf

    [root@m01 srv]# vim /etc/ansible/hosts

    [rsync_client]

    10.0.0.7 ansible_ssh_user=root ansible_ssh_pass=123456

    [backup]

    10.0.0.41 ansible_ssh_user=root ansible_ssh_pass=123456

    [root@m01 srv]# pwd

    /srv

    [root@m01 srv]# vim rsyncd.conf

    #rsync server

    ##rsyncd.conf start##

    uid = rsync

    gid = rsync

    use chroot = no

    max connections = 2000

    timeout = 600

    pid file = /var/run/rsyncd.pid

    lock file = /var/run/rsync.lock

    log file = /var/log/rsyncd.log

    ignore errors

    read only = false

    list = false

    hosts allow = 192.168.1.0/24

    auth users = rsync_backup

    secrets file = /etc/rsync.password

    #####################################

    [backup]

    comment = back server by har

    path = /backup

    [root@m01 scripts]# vim backup.yml

    - hosts: backup

    tasks:

    - name: server

    shell: useradd -M -s /sbin/nologin rsync;mkdir /backup;chown -R rsync:rsync /backup

    - name: copy rsyncd.conf

    copy: src=/srv/rsyncd.conf dest=/etc/rsyncd.conf force=yes

    - name: password file and start rsync daemon

    shell: echo "rsync_backup:rsync" >/etc/rsync.password;chmod 600 /etc/rsync.password;rsync --daemon; netstat -lntup|grep 873

    - hosts: rsync_client

    tasks:

    - name: client

    shell: echo "rsync">/etc/rsync.password;chmod 600 /etc/rsync.password

    - name: rsync dir

    shell: rsync -avz /etc/sysconfig rsync_backup@192.168.1.41::backup --password-file=/etc/rsync.password

    [root@m01 scripts]# ansible-playbook -v backup.yml        #执行

    部分参考来源:http://blog.51cto.com/liuzhengwei521/1895480

  • 相关阅读:
    堆的创建、优先队列、topk、堆排序C语言实现
    HTTPS加密原理
    go shard map实现
    Python进程间通信
    TCP 半连接队列和全连接队列
    WireShark过滤语法
    TCP拥塞机制
    【企业管理实务系列】低值易耗品管理办法
    CV之Face Change:基于人工智能实现国内众多一线美女明星换脸(基于Face++输出4*106个特征点定位+融合代码、deepfake技术)
    【转发】农行银企直联XML对接socket SAP EPIC
  • 原文地址:https://www.cnblogs.com/ssgeek/p/9223440.html
Copyright © 2011-2022 走看看