Linux之自动化运维工具ansible、ansible模块(2)

简介: Linux之自动化运维工具ansible、ansible模块(2)

Linux之自动化运维工具ansible、ansible模块(1)+https://developer.aliyun.com/article/1557938

带/:/lianxi/copy_dir/ :是将lianxi下的copy_dir下的所有子目录以及子文件都拷贝到/lianxi/ansible下面。

[root@b copy_dir]# ansible all -m copy -a "src=/lianxi/copy_dir/ dest=/lianxi/ansible"
192.168.2.132 | CHANGED => {
    "changed": true, 
    "dest": "/lianxi/ansible/", 
    "src": "/lianxi/copy_dir/"
}

2、fetch模块

从远程主机拉取文件到本地。

fetch会自动的在dest指定目录后加上远程主机命名的目录结构后面接src目录结构

fetch存储到本地的目录结构: dest + 远程主机名 + src

例如:将远程主机上的hostname整个目录拉取到ansible机器上的/tmp文件夹下面

[root@sc-master ~]# ansible webser -m fetch -a "src=/etc/hostname dest=/tmp/"
192.168.2.132 | CHANGED => {
    "changed": true, 
    "checksum": "6a407093b4b39fea35a63344f893529cf4ff26d2", 
    "dest": "/tmp/192.168.2.132/etc/hostname", 
    "md5sum": "c810e45b405a225acc2b9cced74d4e1f", 
    "remote_checksum": "6a407093b4b39fea35a63344f893529cf4ff26d2", 
    "remote_md5sum": null
}
192.168.2.137 | CHANGED => {
    "changed": true, 
    "checksum": "de9bcf635db0ce32c9975f2938b930d923576653", 
    "dest": "/tmp/192.168.2.137/etc/hostname", 
    "md5sum": "9fab83764b3e4608e27bd0c32ea881a9", 
    "remote_checksum": "de9bcf635db0ce32c9975f2938b930d923576653", 
    "remote_md5sum": null
}
[root@sc-master ~]# cd /tmp
[root@sc-master tmp]# ls
192.168.2.132
192.168.2.137
)

3、command模块

在远程主机上执行命令,属于裸执行,非键值对显示;不进行shell解析。

[root@b lianxi]# ansible all -m shell -a "ifconfig"
[root@b lianxi]# ansible all -m command -a "ifconfig|grep inet"
192.168.2.132 | FAILED | rc=2 >>
[Errno 2] 没有那个文件或目录
192.168.2.137 | FAILED | rc=2 >>
[Errno 2] 没有那个文件或目录

属于裸执行,不会解析它的管道符号 会认为ifconfig|grep 是一个命令。

4、shell模块

使用ansible中的shell命令的时候,需要两台机器上也有能执行的命令。比如执行ifconfig密码

跟command一样,只不过shell模块可以解析管道之类的功能。

[root@b lianxi]# ansible all -m shell -a "ifconfig|grep inet"
[root@sc-master 192.168.2.132]# ansible webser -m shell -a "ifconfig|grep inet"
192.168.2.132 | CHANGED | rc=0 >>
        inet 192.168.2.132  netmask 255.255.255.0  broadcast 192.168.2.255
        inet6 fe80::20c:29ff:fefd:d5db  prefixlen 64  scopeid 0x20<link>
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
192.168.2.137 | CHANGED | rc=0 >>
        inet 192.168.2.137  netmask 255.255.255.0  broadcast 192.168.2.255
        inet6 fe80::20c:29ff:fe3f:78b  prefixlen 64  scopeid 0x20<link>
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>

5、file模块

设置文件属性(创建文件)

常用参数:

path 目标路径

state directory为目录, link为软件链接

group 目录属组

owner 属主

mode 指定权限

等,其他参数通过ansible-doc -s file 获取

其中的state –

absent 删除文件和目录的

directory 目录

touch 新建空文件

link 软链接

hard 硬链接

例题:创建文件目录,并且设置权限为777。

[root@sc-master tmp]# ansible webser -m file -a "path=/tmp/sanchuang state=directory mode=777"
192.168.2.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "path": "/tmp/sanchuang", 
    "size": 6, 
    "state": "directory", 
    "uid": 0
}
192.168.2.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "path": "/tmp/sanchuang", 
    "size": 6, 
    "state": "directory", 
    "uid": 0
}

查看B机器:目录存在

[root@sc-slave tmp]# ls
ansible_stat_payload_fsj79S
sanchuang

C机器的也存在。

例如:删除那个sanchaung目录

[root@sc-master tmp]# ansible webser -m file -a "path=/tmp/sanchuang state=absent mode=777"

1、查看file模块帮助信息:ansible-doc

[root@b lianxi]# ansible-doc -s file

例题:设置修改文件属性

root@b lianxi]# ansible all -m file -a "path=/tmp/passwd owner=sanchuang"
192.168.2.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "sanchuang", 
    "path": "/tmp/passwd", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 1045, 
    "state": "file", 
    "uid": 1009
}

2、软连接 ,硬链接

创建一个硬链接 文件的链接数会+1。删除硬链接文件或者是源文件 只是把文件的链接数-1 文件不会被真正的删掉。

① 软连接:

[root@b lianxi]# vim ansible-copy
[root@b lianxi]# ln -s ansible-copy ansible-copy-link-s  #软链接
[root@b lianxi]# ls -al
总用量 4
drwxr-xr-x   4 root root  89 11月 25 11:37 .
dr-xr-xr-x. 18 root root 258 11月 25 10:10 ..
drwxr-xr-x   3 root root  17 11月 25 11:03 192.168.0.48
-rw-r--r--   1 root root   8 11月 25 11:36 ansible-copy
lrwxrwxrwx   1 root root  12 11月 25 11:37 ansible-copy-link-s -> ansible-copy
drwxr-xr-x   2 root root  36 11月 25 10:29 copy_dir

② 硬链接

[root@b lianxi]# ln ansible-copy ansible-copy-link   #硬链接
[root@b lianxi]# ls -al
总用量 8
drwxr-xr-x   4 root root 114 11月 25 11:38 .
dr-xr-xr-x. 18 root root 258 11月 25 10:10 ..
drwxr-xr-x   3 root root  17 11月 25 11:03 192.168.0.48
-rw-r--r--   2 root root   8 11月 25 11:36 ansible-copy
-rw-r--r--   2 root root   8 11月 25 11:36 ansible-copy-link
lrwxrwxrwx   1 root root  12 11月 25 11:37 ansible-copy-link-s -> ansible-copy
drwxr-xr-x   2 root root  36 11月 25 10:29 copy_dir

6、cron模块

通过cron模块对目标主机生成计划任务。

常用参数:

除了分(minute)时(hour)日(day)月(month)周(week)外

name: 本次计划任务的名称

state: present 生成(默认) | absent 删除 (基于name)

ntp服务,是一个时间管理服务器
在 centos 8 中, ntp 已经被 chrony 代替。
之前的版本:yum install -y ntp
centos8:yum install chrony

例题:每三分钟输出当前时间,到/tmp/time.txt文件。这个计划任务是设置为每三分钟生成一次,所以要记得及时删除。

[root@b ~]# ansible all -m cron -a "minute=*/3 job='date >>/tmp/time.txt' name=date_test  state=present"
192.168.2.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "date_test"
    ]
}

如果做测试的话,要及时删除计划任务。如果不记得设置的计划任务的名字,可以通过查看计划任务,上方会显示计划任务的名称,然后指定那个名字删除就可以了。

crantab -l:查看计划任务。

删除刚刚创建的计划任务。name是指刚刚创建的计划任务的名字。state=absent是表示删除。

[root@b ~]# ansible 192.168.0.48 -m cron -a "name=date_test  state=absent"
192.168.2.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": []
}

例题:每天凌晨1点 ,检查/etc/passwd 文件是否被修改,并且生成检查报告。

实现ansible node节点服务器备份,备份/var/log/messages 备份到/backup目录下,并且取名2020-11-25-01-log.tar.gz,每一个小时执行一次。

ansible webserver -m cron -a "minute=*/1 job='tar -czf /tmp/sc/$(date +%Y-%m-%d-%H)-log.tar.gz /var/log/messages' name=date_test state=present"

7、yum模块

故名思义就是yum安装软件包的模块;

常用参数说明:

enablerepo,disablerepo表示启用与禁用某repo库

name 安装包名

state (present’ or installed’, latest’)表示安装, (absent’ or `removed’) 表示删除

示例:通过安装epel扩展源并安装nginx。

1、安装wget:

[root@b ~]# ansible all -m yum -a "name=wget state=installed"

2、卸载wget

[root@b ~]# ansible all -m yum -a "name=wget state=absent"

8、service模块

服务管理模块。

常用参数:

name:服务名

state:服务状态 started(启动) stopped(关闭) restarted(重启) reloaded(重新加载)

enabled: 是否开机启动 true|false

runlevel: 启动级别 (systemed方式忽略)

安装文件传输服务vsftpd。

[root@b ~]# ansible all -m yum -a "name=vsftpd state=installed"
192.168.0.48 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    }, 
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Installed: vsftpd-3.0.3-31.el8.x86_64"
    ]
}

关闭vsftpd服务:ansible all -m service -a “name=vsftpd state=stopped”

[root@b ~]# ansible all -m service -a "name=vsftpd state=stopped"

开启vsftpd服务:ansible all -m service -a “name=vsftpd state=started”

[root@b ~]# ansible all -m service -a "name=vsftpd state=started"

例题:使用ansible 部署web服务

安装nginx, 修改nginx的配置文件 /etc/nginx/conf.d/sc.conf

传递index.html文件 到 /opt/dist目录下

index里面的内容: this is index

启动服务

测试能不能访问

server {
        listen       80 ;
        server_name  www.sc.com;
        root         /opt/dist;
  access_log  /var/log/nginx/sc_access.log  main;
        location / {
  }
  location =/api {
  }
}
[root@scmysql opt]# curl -H "Host: www.sc.com" http://192.168.77.13
4
this is index

9、script模块

把本地的脚本传到远端执行;前提是到远端可以执行,不要把Linux下的脚本同步到windows下执行;只在远程服务器执行脚本,不上传脚本到远程服务器。

例如:测试将ansible本地机上的文件放在B、C机器上执行。

1、本地机器上的文件test.sh
[root@b ~]# cat test.sh
#!/bin/bash
echo "test ansible" >> /tmp/ansible.txt
2、使用script命令将本地机上的test.sh文件放在B、C两台远程机上执行。
[root@sc-master ~]# ansible all -m script -a "/root/test.sh"
192.168.2.132 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.2.132 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.2.132 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}
192.168.2.137 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.2.137 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.2.137 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}

执行慢,机器多 。可以使用多进程去执行

-f 6 执行6个进程去执行。


相关文章
|
13天前
|
Linux 开发工具
7种比较Linux中文本文件的最佳工具
7种比较Linux中文本文件的最佳工具
7种比较Linux中文本文件的最佳工具
|
2月前
|
自然语言处理 数据库 iOS开发
DBeaver Ultimate Edtion 25.0 Multilingual (macOS, Linux, Windows) - 通用数据库工具
DBeaver Ultimate Edtion 25.0 Multilingual (macOS, Linux, Windows) - 通用数据库工具
161 12
DBeaver Ultimate Edtion 25.0 Multilingual (macOS, Linux, Windows) - 通用数据库工具
|
1月前
|
运维 安全 Linux
试试Linux设备命令行运维工具——Wowkey
WowKey 是一款专为 Linux 设备设计的命令行运维工具,提供自动化、批量化、标准化的运维解决方案。它简单易用、高效集成且无依赖,仅需 WIS 指令剧本文件、APT 账号密码文件和 wowkey 命令即可操作。通过分离鉴权与执行过程,WowKey 让运维人员专注于决策,摆脱繁琐的交互与执行工作,大幅提升运维效率与质量。无论是健康检查、数据采集还是配置更新,WowKey 都能助您轻松应对大规模设备运维挑战。立即从官方资源了解更多信息:https://atsight.top/training。
|
1月前
|
数据采集 运维 安全
Linux设备命令行运维工具WowKey问答
WowKey 是一款用于 Linux 设备运维的工具,可通过命令行手动或自动执行指令剧本,实现批量、标准化操作,如健康检查、数据采集、配置更新等。它简单易用,只需编写 WIS 指令剧本和 APT 帐号密码表文件,学习成本极低。支持不同流派的 Linux 系统,如 RHEL、Debian、SUSE 等,只要使用通用 Shell 命令即可通吃Linux设备。
|
3月前
|
监控 jenkins 测试技术
Ansible与Jenkins:自动化工具的对比
Ansible和Jenkins是自动化领域的两大巨头。Ansible专注于配置管理和任务自动化,采用无代理架构,使用YAML定义配置,具有幂等性和可扩展性。Jenkins则擅长持续集成和持续交付(CI/CD),支持丰富的插件生态系统,适用于自动化构建、测试和部署。两者各有优势,Ansible适合配置管理与大规模部署,Jenkins则在CI/CD方面表现出色。结合使用可创建更强大的自动化工作流,提升团队生产力和软件质量。选择工具时应根据具体需求决定。
|
2月前
|
人工智能 数据可视化 安全
2025年销售自动化工具选型指南
本文探讨了企业在数字经济时代选择合适CRM系统的重要性,分析了选型的5大核心维度:AI能力、全流程闭环管理、生态集成能力、数据安全合规及供应商综合实力。同时对比了5家国内外CRM厂商,如Salesforce、纷享销客、Hubspot等,并指出企业常遇的3大选型误区,包括功能冗余、忽视数据迁移成本和迷信行业模板。文章强调,适合企业的CRM需与战略、管理和业务流程深度适配,而非单纯追求技术堆砌。最终提醒管理者,选择CRM时应以实际需求为导向,而非盲目迷信国际品牌。
|
2天前
|
Ubuntu Linux
"unzip"命令解析:Linux下如何处理压缩文件。
总的来说,`unzip`命令是Linux系统下一款实用而方便的ZIP格式文件处理工具。本文通过简明扼要的方式,详细介绍了在各类Linux发行版上安装 `unzip`的方法,以及如何使用 `unzip`命令进行解压、查看和测试ZIP文件。希望本文章能为用户带来实际帮助,提高日常操作的效率。
25 12
|
11天前
|
缓存 Linux 数据安全/隐私保护
Linux环境下如何通过手动调用drop_caches命令释放内存
总的来说,记录住“drop_caches” 命令并理解其含义,可以让你在日常使用Linux的过程中更加娴熟和自如。
63 23
|
10天前
|
消息中间件 NoSQL Linux
Redis的基本介绍和安装方式(包括Linux和Windows版本),以及常用命令的演示
Redis(Remote Dictionary Server)是一个高性能的开源键值存储数据库。它支持字符串、列表、散列、集合等多种数据类型,具有持久化、发布/订阅等高级功能。由于其出色的性能和广泛的使用场景,Redis在应用程序中常作为高速缓存、消息队列等用途。
104 16
|
2月前
|
Linux
linux命令详细说明以及案例
本文介绍了常用的 Linux 命令及其详细说明和示例,包括:`ls`(列出目录内容)、`cd`(更改目录)、`rm` 和 `mv`(删除与移动文件)、`grep`(搜索文本)、`cat`(显示文件内容)以及 `chmod`(更改文件权限)。每个命令均配有功能描述、选项说明及实际案例,帮助用户更好地掌握 Linux 命令的使用方法。
180 56