当前位置:   article > 正文

ansible模块功能及搭建lnmp架构

ansible模块功能及搭建lnmp架构


ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。以下内容为常用模块的详细整理。

批量管理模块功能

shell模块 —万能模块

作用:可以实现批量管理主机信息

PS:command模块有缺陷,无法识别特殊符号信息:"$HOME" "<"  ">"  "|"  ";" "&"
模块常用指令参数:
creates: 判断一个文件是否存在,如果存在就不执行相应命令
removers:判断一个文件是否存在,如果存在就执行相应命令
chdir:	 执行命令前,进行目录一个切换

PS:
(1)万能模块在使用时缺陷,不具有幂等性(第一次执行结果和多次执行结果相同)
	当不具有幂等性时,会对剧本编写产生问题???
(2)操作步骤可能会过于繁琐

批量执行脚本功能:
第一个历程:编写脚本
[root@ansible project]# vim test.sh 
[root@ansible project]# cat test.sh 
#!/bin/bash
echo "hello world"
[root@ansible project]# chmod +x test.sh
[root@ansible project]# ./test.sh 
hello world

第二个历程:需要将脚本文件分发给所有主机
[root@ansible project]# ansible webservers -m copy -a "src=/opt/project/test.sh dest=/opt/project"
192.168.137.135 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "checksum": "6aa73e56214888452816de465fa1c8329ac9e119",
    "dest": "/opt/project",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "path": "/opt/project",
    "size": 85,
    "state": "file",
    "uid": 0
}
192.168.137.136 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "checksum": "6aa73e56214888452816de465fa1c8329ac9e119",
    "dest": "/opt/project",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "path": "/opt/project",
    "size": 85,
    "state": "file",
    "uid": 0
}

第三个历程:设置脚本文件权限
[root@ansible project]# ansible webservers -m file -a "path=/opt/project mode=755"
192.168.137.136 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/opt/project",
    "size": 85,
    "state": "file",
    "uid": 0
}
192.168.137.135 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/opt/project",
    "size": 85,
    "state": "file",
    "uid": 0
}

第四个历程:执行脚本文件
[root@ansible project]# ansible webservers -m shell -a "/opt/project"
192.168.137.135 | CHANGED | rc=0 >>
hello world
192.168.137.136 | CHANGED | rc=0 >>
hello world

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96

script模块 —脚本模块

作用:专门批量执行脚本功能
第一个历程:编写脚本
第二个历程:批量执行脚本
[root@ansible project]# ansible webservers -m script -a "/opt/project/test.sh"
192.168.137.135 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.137.135 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.137.135 closed."
    ],
    "stdout": "hello world\r\n",
    "stdout_lines": [
        "hello world"
    ]
}
192.168.137.136 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.137.136 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.137.136 closed."
    ],
    "stdout": "hello world\r\n",
    "stdout_lines": [
        "hello world"
    ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

copy模块 —分发数据模块

作用:可以实现批量分发数据文件
常用参数指令:
src:				指定要分发文件数据信息
dest:				将分发文件保存到哪个目标路径
owner:				指定文件分发后的属主信息
group:				指定文件分发后的属组信息
mode:				指定文件分发后的权限信息
backup:				当文件名称重复时,会将原文件备份,再进行覆盖
content:			在被管理主机上创建文件并设置信息内容
directory_mode:		可以递归设置目录中数据权限(只是针对复制目录时)
force:(了解)		 可以避免相同名称文件覆盖
					force=yes 默认设置 表示会覆盖
					force=no  不会对相同文件进行覆盖
remote_src:			将被管理主机上文件进行本地备份保存
validate:			验证分发后的文件合法性
123456789101112131415
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

实践操作:

(1)批量分发文件信息,并修改文件属主 属组 和权限信息

[root@ansible project]# ansible webservers -m copy -a "src=/opt/project/abc.txt dest=/tmp/ mode=666 owner=tom group=tom"
192.168.137.135 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "1a82a29241cefb4e3e6599a809e234e580101003",
    "dest": "/tmp/abc.txt",
    "gid": 1001,
    "group": "tom",
    "mode": "0666",
    "owner": "tom",
    "path": "/tmp/abc.txt",
    "size": 14,
    "state": "file",
    "uid": 1001
}
192.168.137.136 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "1a82a29241cefb4e3e6599a809e234e580101003",
    "dest": "/tmp/abc.txt",
    "gid": 1001,
    "group": "tom",
    "mode": "0666",
    "owner": "tom",
    "path": "/tmp/abc.txt",
    "size": 14,
    "state": "file",
    "uid": 1001
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

2)在被管理主机上创建文件信息,并设置文件的内容

[root@ansible ~]# ansible webservers -m copy -a "content=hello_dog dest=/tmp/abc.txt"
192.168.137.136 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "7f3d38f956a244629bac0f6c5ef9b63d677c20aa",
    "dest": "/tmp/abc.txt",
    "gid": 1001,
    "group": "tom",
    "md5sum": "b168d7d7ec5af35034436a2f0aa34ec7",
    "mode": "0666",
    "owner": "tom",
    "size": 9,
    "src": "/root/.ansible/tmp/ansible-tmp-1666505224.513224-2159-172028776136002/source",
    "state": "file",
    "uid": 1001
}
·······
[root@ansible ~]# ansible webservers -m shell -a " cat /tmp/abc.txt "
192.168.137.136 | CHANGED | rc=0 >>
hello_dog
192.168.137.145 | CHANGED | rc=0 >>
hello_dog
192.168.137.135 | CHANGED | rc=0 >>
hello_dog
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

(3)将目录信息复制到被管理主机上,并且将目录以及下面所有内容权限统一改为777

1
  • 1

利用copy模块分发目录信息时:

目录后面有 / 表示将目录下面数据内容进行整体分发

目录后面没有 / 表示将目录下面数据内容以及目录本身都做分发

<
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/1011175
推荐阅读
相关标签
  

闽ICP备14008679号