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个进程去执行。


相关文章
|
2天前
|
运维 Linux Shell
Linux之自动化运维工具ansible、ansible模块(1)
Linux之自动化运维工具ansible、ansible模块(1)
|
1天前
|
安全 数据挖掘 Linux
Linux命令rpm深度解析
`rpm`是Linux下的软件包管理器,用于安装、升级、卸载和查询`.rpm`包,常见于Red Hat系Linux。它管理依赖、维护软件信息数据库,支持版本控制和安全验证。常用命令如`-i`安装,`-U`升级,`-e`卸载,`-q`查询。安装时用`-v`和`-h`可查看详细信息和进度。注意依赖关系、权限和签名验证,最佳实践包括使用仓库、定期更新和备份数据。
|
1天前
|
存储 缓存 网络协议
Linux系统之ARP命令的基本使用
【7月更文挑战第2天】Linux系统之ARP命令的基本使用
11 2
|
1天前
|
存储 数据挖掘 Linux
探索Linux命令rpm2cpio:解析RPM包内容的利器
`rpm2cpio`是Linux下用于从RPM包中提取内容的工具,它将`.rpm`转换为CPIO归档。无需安装,可直接访问包内文件,适合数据分析。命令简单,常与`cpio`结合使用,如`rpm2cpio package.rpm | cpio -idmv`解压文件。示例包括提取特定文件和列出包内所有文件。注意权限、路径和文件完整性,使用前备份数据,并查阅文档以优化使用。
|
1天前
|
监控 安全 Linux
Linux命令rpmkeys:守护RPM包的安全之门
`rpmkeys`是Linux中管理RPM包公钥的工具,用于验证软件包的签名,确保安全。它支持导入、导出、删除和查询公钥,通过数字签名保证包的完整性和来源可靠性。例如,使用`--import`导入公钥,`--query`查询公钥。最佳实践包括验证公钥来源、定期更新并备份。该命令间接保障了数据处理和分析的安全环境。
|
1天前
|
Shell Linux C语言
Linux初学者必备:全面掌握核心命令
Linux命令是操作系统的核心组成部分,用于管理和操作文件系统、网络、硬件资源以及执行各种任务。下面列出了一些常用的Linux命令及其基本用途,
Linux初学者必备:全面掌握核心命令
|
1天前
|
缓存 监控 Linux
Linux系统之smem命令的基本使用
【7月更文挑战第1天】Linux系统之smem命令的基本使用
18 2
|
2天前
|
Unix Linux
Linux中grep命令的高级用法与实例
Linux中grep命令的高级用法与实例

热门文章

最新文章