如何使用自动化运维ansible工具——理论+实验

前言

因为时代在发展,公司也在快速的发展,随着业务的增加事情也越来越多,这个时候也不可能还在和以前一样人为手动对单台机器进行部署,那样非常消耗人力资源还费时,从而出现了自动化工具三剑客:ansible saltstack puppet,本章就是来讲自动化工具中的ansible来实现批量部署程序、命令等

一、什么是ansible?

  • ansible是新出现的自动化运维工具,基于Python开发,集合众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量命令等功能。
  • ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架

二、ansible的特点

  • 1.部署简单,只需主控端部署ansible环境,被控端无序做任何操作
  • 2.默认使用SSH协议对设备进行连接管理
  • 3.主从集中化管理
  • 4.配置简单、功能强大、扩展性强
  • 5.支持API及自定义模块,可通过Python轻松扩展
  • 6.通过Playbooks来定制强大的配置、状态管理
  • 7.支持云平台、大数据
  • 8.提供一个功能强大、操作性强的Web管理界面和REST API接口——AWX平台

三、三剑客中ansible 与 saltStack的区别

  • 最大的区别是 Ansible 无需在被监控主机部署任何客户端代理,默认通过 SSH 通道进行远程命令执行或下发配置。
  • 相同点是都具备功能强大、灵活的系统管理、状态配置,都使用 YAML 格式来描述配置,两者都提供丰富的模板及 API,对云计算平台、大数据都有很好的支持。

四、ansible解析图

在这里插入图片描述
主要模块如下:

  • Ansible:Ansible核心程序
  • Host Inventory:记录有Ansible管理的主机信息,包括端口、密码、IP等
  • Playbooks: “剧本” YAML格式文件,多个任务定义在一个文件中,定义主机需要调用哪些模块来完成的功能
  • Core Modules:核心模块,主要操作是通过调用核心模块来完成管理任务
  • Custom Modules:自定义模块,完成核心模块无法完成的功能,支持多种语言
  • Connection Plugins:连接插件,Ansible和Host通信使用

五、部署操作

5.1 环境

master:192.168.1.10
node1:192.168.1.11
node2:192.168.1.12

5.2 实操

5.2.1 安装ansible

1.先保证主控服务器可以上网
在这里插入图片描述
2.下载阿里的镜像文件

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

3.安装epel-release、ansible

yum  -y  install  epel-release
yum  -y  install  ansible 

4.查看ansible版本

[root@master ~]#ansible  --version  
ansible 2.9.16
  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, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

5.可以安装tree树状结构显示软件展示ansible文件夹

[root@localhost ~]#yum  -y  install  tree
[root@localhost ~]# tree /etc/ansible/    #树桩结构展示文件夹
/etc/ansible/
├── ansible.cfg         #ansible的配置文件
├── hosts     #ansible的主仓库,用于存储需要管理的远程主机的相关
└── roles     #角色

1 directory, 2 files

6.配置主机名单

[root@master ~]# cd /etc/ansible/
[root@master ansible]# vi  hosts    
添加:
[webserver]
192.168.1.11
[mysql]
192.168.1.12

在这里插入图片描述
7.生成秘钥对

[root@master ~]# ssh-keygen -t rsa

在这里插入图片描述
8.配置秘钥对验证,发送公钥给节点

[root@master ~]# ssh-copy-id root@192.168.1.11
[root@master ~]# ssh-copy-id root@192.168.1.12

在这里插入图片描述
9.这边可以看到有一个.ssh目录,进入.ssh查看目录中的内容可以看到是有Master主控服务端发给它被控端的秘钥对
在这里插入图片描述
10.设置免交互代理

[root@master ~]# ssh-agent bash
[root@master ~]# ssh-add
Enter passphrase for /root/.ssh/id_rsa:     #输入秘钥对密码
Identity added: /root/.ssh/id_rsa	 (/root/.ssh/id_rsa)

5.2.2 ansible模块介绍

anisble命令格式:ansible  [主机] [-m 模块] [-a args]

ansible-doc -l #列出所有已安装的模块,注:按q退出

ansible-doc -s yum #-s列出yum模块描述信息和操作动作

5.2.2.1 ping模块(测试网络模块)

用来测试控端与被控端的连通性,网络连接是否正常

[root@master ~]# ansible all -m ping
192.168.1.12 | SUCCESS => {
    
    
    "ansible_facts": {
    
           #网络连接正常
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.1.11 | SUCCESS => {
    
    
    "ansible_facts": {
    
         #网络连接正常
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
5.2.2.1.1 可能会出现问题

用ansible对某一个节点执行一个操作(创建文件夹、发送文件等),却发现这个节点没有执行那个操作,该如何解决这个问题:
1.ping一下测试通不通,看看是不是网络的问题
2.查看hosts名单中有没有节点
3.看看有没有添加秘钥对,是否需要删除重新传
4.查看节点ssh的22端口是否开启

5.2.2.2 yum模块(安装模块)

通常用于服务的查看、安装、卸载的作用

使用yum模块安装httpd服务

[root@master ~]# ansible mysql -m yum -a 'name=httpd'
192.168.1.12 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
    
    
        "installed": [
            "httpd"
        ]
    }, 
    "msg": "", 
    "rc": 0, 
    "results": [
........

测试可以通过ansible中yum模块对mysql清单中的主机,实现了httpd的安装

[root@node2 ~]# rpm -q httpd
httpd-2.4.6-67.el7.centos.x86_64

使用yum模块查看httpd版本信息

[root@master ~]# ansible mysql -m yum -a 'name=httpd state=present'
192.168.1.12 | SUCCESS => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "httpd-2.4.6-67.el7.centos.x86_64 providing httpd is already installed"
    ]
}

使用yum模块卸载httpd软件

[root@master ~]# ansible mysql -m yum -a 'name=httpd state=absent'
192.168.1.12 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
    
    
        "removed": [
            "httpd"
        ]
    }, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "已加载插件:fastestmirror, langpacks\n正在解决依赖关系\n--> 正在检查事务\n---> 软件包 httpd.x86_64.0.2.4.6-67.el7.centos 将被 删除\n--> 解决依赖关系完成\n\n依赖关系解决\n\n================================================================================\n Package       架构           版本                         源              大小\n================================================================================\n正在删除:\n httpd         x86_64         2.4.6-67.el7.centos          @local         9.4 M\n\n事务概要\n================================================================================\n移除  1 软件包\n\n安装大小:9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  正在删除    : httpd-2.4.6-67.el7.centos.x86_64                            1/1 \n  验证中      : httpd-2.4.6-67.el7.centos.x86_64                            1/1 \n\n删除:\n  httpd.x86_64 0:2.4.6-67.el7.centos                                            \n\n完毕!\n"
    ]
}

5.2.2.3 Command模块(命令模块)

command是默认模块,如不加,默认选择的是command模块

使用command模块查看当前时间

1.基于hosts清单中定义的名称

[root@master ~]# ansible mysql -m command -a 'date'
192.168.1.12 | CHANGED | rc=0 >>
20210113日 星期三 19:02:38 CST

2.基于节点服务的IP地址

[root@master ~]# ansible 192.168.1.12 -m command -a 'date'
192.168.1.12 | CHANGED | rc=0 >>
20210113日 星期三 19:02:57 CST

3.基于所有节点服务查看时间

[root@master ~]# ansible all -m command -a 'date'
192.168.1.12 | CHANGED | rc=0 >>
20210113日 星期三 19:05:49 CST
192.168.1.11 | CHANGED | rc=0 >>
20210113日 星期三 19:05:49 CST

查看所有节点服务器下的根目录内容

[root@master ~]# ansible all -a 'ls -lh /'
192.168.1.12 | CHANGED | rc=0 >>
总用量 30K
lrwxrwxrwx.   1 root root    7 111 18:53 bin -> usr/bin
dr-xr-xr-x.   5 root root 4.0K 111 19:17 boot
drwxr-xr-x.  20 root root 3.3K 111 19:17 dev
............
192.168.1.11 | CHANGED | rc=0 >>
总用量 30K
lrwxrwxrwx.   1 root root    7 112 18:49 bin -> usr/bin
dr-xr-xr-x.   5 root root 4.0K 112 19:17 boot
drwxr-xr-x.  20 root root 3.3K 112 19:17 dev
drwxr-xr-x. 143 root root 8.0K 113 17:14 etc
drwxr-xr-x.   4 root root   29 113 17:14 home
lrwxrwxrwx.   1 root root    7 112 18:49 lib -> usr/lib
.......

使用默认的command模块查看软件是否安装与版本信息

[root@master ~]# ansible mysql -a 'rpm -q httpd'
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'.  If you need to
use command because yum, dnf or zypper is insufficient you can add 'warn: false' to this command
task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
192.168.1.12 | CHANGED | rc=0 >>
httpd-2.4.6-67.el7.centos.x86_64

查看httpd服务端口状态

[root@master ~]# ansible mysql -a 'systemctl status httpd'
192.168.1.12 | FAILED | rc=4 >>
Unit httpd.service could not be found.non-zero return code

5.2.2.4 cron模块(计划任务模块)

两种状态(stat): present 表示添加(可以忽略),absent 表示移除

查看cron模块信息

[root@master ~]# ansible-doc -s cron 

制作ansible管理制定计划任务
每一分钟执行输出hello到hello.txt文档中,动作名称为hello

[root@master ~]# ansible mysql -m cron -a 'minute="*/1" job="/usr/bin/echo hello >> /opt/hello.txt" name="hello"'
Enter passphrase for key '/root/.ssh/id_rsa': 
192.168.1.12 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true,   #表示成功
    "envs": [], 
    "jobs": [
        "hello"
    ]
}

默认使用command模块执行查看mysql节点的计划任务命令

[root@master ~]# ansible mysql -a 'crontab -l'
192.168.1.12 | CHANGED | rc=0 >>
#Ansible: hello world
*/1 * * * * /usr/bin/echo hello >> /opt/hello.txt

mysql节点查看
[root@node2 opt]# cat hello.txt     #过了二分钟,文本会生成2个hello

hello
hello

停止删除计划任务,加入该计划任务没有取名字,name=None即可

[root@master ~]# ansible mysql -m cron -a 'name="hello" state=absent'
192.168.1.12 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": []
}

停止删除计划后

[root@master ~]# ansible mysql -a 'crontab -l'
192.168.1.12 | CHANGED | rc=0 >>

5.2.2.5 user模块(用户模块)

user模块请求的是useradd,userdel,usermod三个命令

使用user创建lisi用户

[root@master ~]# ansible mysql -m user -a 'name=lisi'
192.168.1.12 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 1001,      #属组
    "home": "/home/lisi",  #家目录路径
    "name": "lisi",     #用户名称
    "shell": "/bin/bash",   
    "state": "present", 
    "system": false,   #是否为系统用户
    "uid": 1001    #用户uid
}

使用command模块查看倒数6行的用户信息

[root@master ~]# ansible mysql -m command -a 'tail -6 /etc/passwd'
192.168.1.12 | CHANGED | rc=0 >>
.......省略
lisi:x:1001:1001::/home/lisi:/bin/bash

删除用户lisi

[root@master ~]# ansible mysql -m user -a 'name="lisi" state=absent'
192.168.1.12 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "force": false, 
    "name": "lisi", 
    "remove": false, 
    "state": "absent"
}

5.2.2.6 group模块(组模块)

group模块请求的是groupadd,groupdel,groupmod三个指令

创建一个mysql组,gid为306,为系统用户

[root@master ~]# ansible mysql -m group -a 'name=mysql gid=306 system=yes'
192.168.1.12 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 306, 
    "name": "mysql", 
    "state": "present", 
    "system": true
}
[root@master ~]# ansible mysql -a 'tail -6 /etc/group'
192.168.1.12 | CHANGED | rc=0 >>
.......省略
mysql:x:306:

创建一个系统用户zhangsan,uid为306,属组为group

[root@master ~]# ansible mysql -m user -a 'name=zhangsan uid=306 system=yes group=mysql'
192.168.1.12 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 306, 
    "home": "/home/zhangsan", 
    "name": "zhangsan", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": true, 
    "uid": 306
}
[root@master ~]# ansible mysql -a 'tail -6 /etc/passwd'
192.168.1.12 | CHANGED | rc=0 >>
.......省略
zhangsan:x:306:306::/home/zhangsan:/bin/bash
[root@master ~]# ansible mysql -a 'id zhangsan'
192.168.1.12 | CHANGED | rc=0 >>
uid=306(zhangsan) gid=306(mysql)=306(mysql)

5.2.2.7 copy模块(复制模块)

复制mysql节点上/etc/fstab文件到/opt/fstab.bak(备份),新文件属主为root,权限640

[root@master ~]# ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root mode=640' 
192.168.1.12 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "19e2929290dd2fc310d6e08c3703fdbab3cf7cc0", 
    "dest": "/opt/fstab.bak", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "dff6006a952e63391b7ae04789a18e24", 
    "mode": "0640", 
    "owner": "root", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 620, 
    "src": "/root/.ansible/tmp/ansible-tmp-1610548270.52-43118-165262685948891/source", 
    "state": "file", 
    "uid": 0
}

查看备份文件

[root@master ~]# ansible mysql -a 'ls -l /opt/fstab.bak' 
192.168.1.12 | CHANGED | rc=0 >>
-rw-r-----. 1 root root 620 112 21:33 /opt/fstab.bak

[root@master ~]# ansible mysql -a 'cat /opt/fstab.bak'
192.168.1.12 | CHANGED | rc=0 >>

#
# /etc/fstab
# Created by anaconda on Wed Sep 30 18:19:59 2020
......省略

5.2.2.8 file模块(文件管理模块)

使用user模块创建一个系统账户mysql

[root@master ~]# ansible mysql -m user -a 'name=mysql system=yes'
192.168.1.12 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 100, 
    "home": "/home/mysql", 
    "name": "mysql", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": true, 
    "uid": 305
}

使用group模块创建一个系统组mysql

[root@master ~]# ansible mysql -m group -a 'name=mysql system=yes'
192.168.1.12 | SUCCESS => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "gid": 306, 
    "name": "mysql", 
    "state": "present", 
    "system": true
}

使用file模块修改/etc/fstab.bak的属主属组,权限

[root@master ~]# ansible mysql -m file -a 'owner=mysql group=mysql mode=644 path=/opt/fstab.bak' 
192.168.1.12 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 306, 
    "group": "mysql", 
    "mode": "0644", 
    "owner": "mysql", 
    "path": "/opt/fstab.bak", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 620, 
    "state": "file", 
    "uid": 305
}

创建/opt/fstab.bak软连接

[root@master ~]# ansible mysql -m file -a 'path=/opt/fstab.link src=/opt/fstab.bak state=link'
192.168.1.12 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/opt/fstab.link", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:usr_t:s0", 
    "size": 14, 
    "src": "/opt/fstab.bak", 
    "state": "link", 
    "uid": 0
}

删除/opt/fstab.bak

[root@master ~]# ansible mysql -m file -a 'path=/opt/fstab.bak state=absent' 
192.168.1.12 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "path": "/opt/fstab.bak", 
    "state": "absent"
}

创建一个/opt/text文件

[root@master ~]# ansible mysql -m file -a 'path=/opt/text state=touch' 
192.168.1.12 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/opt/text", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:usr_t:s0", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}

创建/opt/ttt目录

[root@master ~]# ansible mysql -m file -a 'path=/opt/ttt state=directory mode=755' 
192.168.1.12 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/opt/ttt", 
    "secontext": "unconfined_u:object_r:usr_t:s0", 
    "size": 6, 
    "state": "directory", 
    "uid": 0
}

5.2.2.9 service模块(服务模块)

开启httpd服务

ansible mysql -m service -a 'name=httpd state=started'
ansible mysql -a 'systemctl start httpd'
上下两条命令作用都能达到开启服务的功能

关闭httpd服务

ansible mysql -m service -a 'name=httpd state=stopped'
ansible mysql -a 'systemctl stop httpd'

重新加载httpd服务

ansible mysql -m service -a 'name=httpd state=reloaded'
ansible mysql -a 'systemctl reload httpd'

重启httpd服务

nsible mysql -m service -a 'name=httpd state=restarted'

开启ftp服务,同时设置开机自动启动

ansible mysql -m service -a 'name=httpd state=started enabled=yes'

使用service模块启动httpd服务

[root@master ~]# ansible mysql -m service -a 'name=httpd enabled=true state=started'
192.168.1.12 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "enabled": true, 
    "name": "httpd", 
    "state": "started", 
    "status": {
    
    
...........省略

使用service模块,关闭防火墙

[root@master ~]# ansible mysql -m service -a 'name=firewalld state=stopped'
Enter passphrase for key '/root/.ssh/id_rsa': 
192.168.1.12 | SUCCESS => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "name": "firewalld", 
    "state": "stopped", 
    "status": {
    
    
        "ActiveEnterTimestampMonotonic": "0", 
        "ActiveExitTimestampMonotonic": "0", 
        "ActiveState": "inactive", 
        "After": "polkit.service basic.target dbus.service system.slice", 
        "AllowIsolate": "no", 
        "AmbientCapabilities": "0", 
.........省略

在被控端节点上
查看httpd服务状态

[root@node2 ~]# systemctl status httpd.service 
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2021-01-13 23:12:02 CST; 3min 26s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 62145 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─62145 /usr/sbin/httpd -DFOREGROUND
           ├─62161 /usr/sbin/httpd -DFOREGROUND
           ├─62162 /usr/sbin/httpd -DFOREGROUND
           ├─62163 /usr/sbin/httpd -DFOREGROUND
           ├─62164 /usr/sbin/httpd -DFOREGROUND
           └─62165 /usr/sbin/httpd -DFOREGROUND

113 23:12:02 node2 systemd[1]: Starting The Apache HTTP Server...
113 23:12:02 node2 httpd[62145]: AH00558: httpd: Could not reliably determine the server...sage
113 23:12:02 node2 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

查看防火墙状态

[root@node2 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

111 19:17:07 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
111 19:17:07 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
111 19:17:07 localhost.localdomain firewalld[776]: WARNING: ICMP type 'beyond-scope' is no...6.
111 19:17:07 localhost.localdomain firewalld[776]: WARNING: beyond-scope: INVALID_ICMPTYPE...e.
111 19:17:07 localhost.localdomain firewalld[776]: WARNING: ICMP type 'failed-policy' is n...6.
111 19:17:07 localhost.localdomain firewalld[776]: WARNING: failed-policy: INVALID_ICMPTYP...e.
111 19:17:07 localhost.localdomain firewalld[776]: WARNING: ICMP type 'reject-route' is no...6.
111 19:17:07 localhost.localdomain firewalld[776]: WARNING: reject-route: INVALID_ICMPTYPE...e.
112 11:50:39 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon...
112 11:50:40 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.

5.2.2.10 Shell模块(脚本模块)

shell脚本中可以执行多个语句,而且shell可以识别重定向,追加,默认command命令不识别

  • chdir(cd):制定工作目录,在执行对应的命令之前,会先进入到chdir参数指定的目录中。
  • create:指定一个文件,当指定的文件存在时,就不执行对应命令。
  • removes:使用此参数指定一个文件,当指定的文件不存在时,就不执行对应命令。

默认command模块不识别重定向(>)、追加符号

[root@master ~]# ansible mysql -m shell -a 'chdir=/opt/ echo hello > /opt/hello.txt'
192.168.1.12 | CHANGED | rc=0 >>

[root@master ~]# ansible mysql -m shell -a 'chdir=/opt/ ls'
192.168.1.12 | CHANGED | rc=0 >>
fstab.back
fstab.link
hello.txt
rh
text
ttt
yum.bak

5.2.2.11 script模块(脚本模块)

在控制端创建脚本

[root@master ~]# cd /opt
[root@master opt]# vi test.sh
#!/bin/bash
echo "test" > /opt/script.txt

[root@master opt]# chmod +x test.sh #添加执行权限

调用管理端本地脚本在mysql节点运行

[root@master opt]# ansible mysql -m script -a 'test.sh'
192.168.1.12 | CHANGED => {
    
    
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.1.12 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.1.12 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}

在被控端上查看

[root@node2 opt]# ll
总用量 12
drwxr-xr-x. 3 root root  25 113 22:33 a
-rw-r--r--. 1 root root  66 113 23:33 hello.txt
-rw-r--r--. 1 root root 511 113 17:14 inittab.back
drwxr-xr-x. 2 root root   6 326 2015 rh
-rw-r--r--. 1 root root   5 113 23:32 script.txt
[root@node2 opt]# cat script.txt 
test

5.2.2.12 setup模块(获取节点信息)

获取mysql组主机的facts信息;facts(事实):主机名称、内核版本、网络接口、IP地址等

[root@master ~]# ansible mysql -m setup
Enter passphrase for key '/root/.ssh/id_rsa': 
192.168.1.12 | SUCCESS => {
    
    
    "ansible_facts": {
    
    
        "ansible_all_ipv4_addresses": [
            "192.168.122.1", 
            "192.168.1.12"
        ], 
        "ansible_all_ipv6_addresses": [
.......省略
// An highlighted block
var foo = 'bar';
// An highlighted block
var foo = 'bar';
// An highlighted block
var foo = 'bar';
// An highlighted block
var foo = 'bar';
// An highlighted block
var foo = 'bar';
// An highlighted block
var foo = 'bar';
// An highlighted block
var foo = 'bar';
// An highlighted block
var foo = 'bar';

猜你喜欢

转载自blog.csdn.net/F2001523/article/details/112641778
今日推荐