这些写的都是常用的模块,还有更多其他的模块可以自行查文档
模块的使用这里都使用Ad-Hoc的方式。playbook的方式后面会说
file 模块主要用于远程主机上的文件操作,有一下选项
[ansible@master ansible]$ ansible all -m file -a "path=/tmp/file1 state=touch owner=ansible group=root mode=666"
192.168.200.210 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"dest": "/tmp/file1",
"gid": 0,
"group": "root",
"mode": "0666",
"owner": "ansible",
"size": 0,
"state": "file",
"uid": 1000
}
使用path定义了文件的路径,state为touch,则为创建/更新时间戳,文件的拥有人是ansible,所属组是root,并且权限是666,那么我们来到远程主机来看看是不是这样
[ansible@master ansible]$ ansible all -m shell -a "ls -l /tmp/file1"
192.168.200.210 | CHANGED | rc=0 >>
-rw-rw-rw- 1 ansible root 0 Jun 20 13:38 /tmp/file1
可以看到,一切都是按照我们的想法来创建的
[ansible@master ansible]$ ansible all -m file -a "path=/tmp/dire1 state=directory"
192.168.200.210 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/dire1",
"size": 40,
"state": "directory",
"uid": 0
}
这次我们没有指定权限,拥有人,所属组那些,他就是按照默认的来创建的,通过回显可以看到group是root,owner是root,权限是755
[ansible@master ansible]$ ansible all -m file -a "path=/tmp/dire1 state=absent"
192.168.200.210 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"path": "/tmp/dire1",
"state": "absent"
}
[ansible@master ansible]$ ansible all -m file -a "path=/tmp/file1 state=absent"
192.168.200.210 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"path": "/tmp/file1",
"state": "absent"
}
在删除的时候,是不分目录还是文件的,只需要给定文件的路径就可以删除
见名知意,就是拷贝文件到远程主机的
[ansible@master ansible]$ ansible all -m copy -a "src=./ansible.cfg dest=/tmp/ansible.cfg"
192.168.200.210 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"checksum": "b4eeb9b83b919c3f57d7e92dbde263a35713dca4",
"dest": "/tmp/ansible.cfg",
"gid": 0,
"group": "root",
"md5sum": "156467e44d3da8b3a40b2ca409f86ae8",
"mode": "0644",
"owner": "root",
"size": 19974,
"src": "/root/.ansible/tmp/ansible-tmp-1718863118.8950412-44119-223733622484954/source",
"state": "file",
"uid": 0
}
指定文件内容到远程主机
[ansible@master ansible]$ ansible all -m copy -a "content='hello,world' dest=/tmp/hello"
[ansible@master ansible]$ ansible all -m shell -a "cat /tmp/hello"
192.168.200.210 | CHANGED | rc=0 >>
hello,world
yum_repository是用来指定yum仓库的repo文件的
[ansible@master ansible]$ ansible all -m yum_repository -a "file=ansible name=AppStream baseurl=http://test.com enabled=1 gpgcheck=0 description='this is test repo file'"
192.168.200.210 | CHANGED => {
"changed": true,
"repo": "AppStream",
"state": "present"
}
我们来到远程主机查看这个文件
[root@node1 tmp]# cd /etc/yum.repos.d/
[root@node1 yum.repos.d]# ls
ansible.repo kubernetes.repo openEuler.repo
[root@node1 yum.repos.d]# cat ansible.repo
[AppStream]
baseurl = http://test.com
enabled = 1
gpgcheck = 0
name = this is test repo file
这个模块相对比较简单,功能也比较单一
用来使用yum去安装软件包
删除nginx
[ansible@master ansible]$ ansible all -m yum -a "name=nginx state=absent"
192.168.200.210 | CHANGED => {
"ansible_facts": {
"pkg_mgr": "dnf"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Removed: nginx-1:1.21.5-6.oe2203sp3.x86_64",
"Removed: nginx-all-modules-1:1.21.5-6.oe2203sp3.noarch",
"Removed: nginx-mod-http-image-filter-1:1.21.5-6.oe2203sp3.x86_64",
"Removed: nginx-mod-http-perl-1:1.21.5-6.oe2203sp3.x86_64",
"Removed: nginx-mod-http-xslt-filter-1:1.21.5-6.oe2203sp3.x86_64",
"Removed: nginx-mod-mail-1:1.21.5-6.oe2203sp3.x86_64",
"Removed: nginx-mod-stream-1:1.21.5-6.oe2203sp3.x86_64"
]
}
安装httpd
[ansible@master ansible]$ ansible all -m yum -a "name=nginx state=present"
192.168.200.210 | CHANGED => {
"ansible_facts": {
"pkg_mgr": "dnf"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: nginx-mod-http-xslt-filter-1:1.21.5-6.oe2203sp3.x86_64",
"Installed: nginx-mod-mail-1:1.21.5-6.oe2203sp3.x86_64",
"Installed: nginx-mod-stream-1:1.21.5-6.oe2203sp3.x86_64",
"Installed: nginx-1:1.21.5-6.oe2203sp3.x86_64",
"Installed: nginx-all-modules-1:1.21.5-6.oe2203sp3.noarch",
"Installed: nginx-mod-http-image-filter-1:1.21.5-6.oe2203sp3.x86_64",
"Installed: nginx-mod-http-perl-1:1.21.5-6.oe2203sp3.x86_64"
]
}
这个模块完全可以使用systemd来替代,有这个模块的原因是因为centos的早期版本是使用service xxx start 来管理服务的
[ansible@master ansible]$ ansible all -m service -a "name=nginx state=started enabled=yes"
192.168.200.210 | CHANGED => {
"changed": true,
"enabled": true,
"name": "nginx",
"state": "started",
"status": {
输出信息过多,不在这里完全展示
[ansible@master ansible]$ ansible all -m systemd -a "name=nginx state=stopped enabled=no"
192.168.200.210 | CHANGED => {
"changed": true,
"enabled": false,
"name": "nginx",
"state": "stopped",
[ansible@master ansible]$ ansible all -m user -a "name=natasha uid=1234 groups=root shell=/sbin/nologin password=123"
[WARNING]: The input password appears not to have been hashed. The 'password'
argument must be encrypted for this module to work properly.
192.168.200.210 | CHANGED => {
"changed": true,
"comment": "",
"create_home": true,
"group": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"groups": "root",
"home": "/home/natasha",
"name": "natasha",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/sbin/nologin",
"state": "present",
"system": false,
"uid": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER"
}
注意,我在这里指定了password,并且就是明文指定的,他会有一个警告,说我们给定的密码不是一个hash过的值,说白了就是没有经过加密的密码,这个密码是无法使用的,你使用123 是无法登录这个用户的,当然我们指定的shell是/sbin/nologin,你正常指定shell也是不能
我们可以使用openssl来生成一个加密的密码
[ansible@master ansible]$ openssl passwd -6
Password:
Verifying - Password:
$6$b4Ug/ub0EPkyRE5x$oN.c5c2ah.Ej.Eo8s3F0q1E5t1/MHCFanZZivkJ8S2ZzE8fR2I2e7uYL5HgZ5CLwo1MGMhnHd2mmFxkN49Kq20
将输出的这一段放在password字段就可以了
用户的删除
[ansible@master ansible]$ ansible all -m user -a "name=natasha state=absent remove=yes"
192.168.200.210 | CHANGED => {
"changed": true,
"force": false,
"name": "natasha",
"remove": true,
"state": "absent"
}
group模块是用来创建用户组的
[ansible@master ansible]$ ansible all -m group -a "name=test gid=2024 state=present"
192.168.200.210 | CHANGED => {
"changed": true,
"gid": 2024,
"name": "test",
"state": "present",
"system": false
}
# 删除组
[ansible@master ansible]$ ansible all -m group -a "name=test gid=2024 state=absent"
192.168.200.210 | CHANGED => {
"changed": true,
"name": "test",
"state": "absent"
}
这个模块正好与copy模块相反,copy是将文件复制到远程,这个则是将远程文件收集到本地
# 将之前创建的hello文件收集到本地
[ansible@master ansible]$ ansible all -m fetch -a "src=/tmp/hello dest=./ "
192.168.200.210 | CHANGED => {
"changed": true,
"checksum": "74f4f4eb1947b9ca08e5e68d04d081808777f9a0",
"dest": "/home/ansible/ansible/192.168.200.210/tmp/hello",
"md5sum": "3cb95cfbe1035bce8c448fcaf80fe7d9",
"remote_checksum": "74f4f4eb1947b9ca08e5e68d04d081808777f9a0",
"remote_md5sum": null
}
[ansible@master ansible]$ ls
ansible.cfg roles user.yaml 192.168.200.210 inventory set
这里就会有一个目录名是远程主机名,目录里面的内容就是我们收集过来的文件了
[ansible@master ansible]$ cat 192.168.200.210/tmp/hello
hello,world
[ansible@master ansible]$ rm -rf 192.168.200.210/
[ansible@master ansible]$ ansible all -m fetch -a "src=/tmp/hello dest=./ flat=yes"
192.168.200.210 | CHANGED => {
"changed": true,
"checksum": "74f4f4eb1947b9ca08e5e68d04d081808777f9a0",
"dest": "/home/ansible/ansible/hello",
"md5sum": "3cb95cfbe1035bce8c448fcaf80fe7d9",
"remote_checksum": "74f4f4eb1947b9ca08e5e68d04d081808777f9a0",
"remote_md5sum": null
}
[ansible@master ansible]$ ls
ansible.cfg hello inventory roles set user.yaml
如果为yes的话就会直接显示文件名
功能从网上下载文件,类似于wget命令
[ansible@master ansible]$ ansible all -m get_url -a "url=https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo dest=/tmp"
192.168.200.210 | CHANGED => {
"changed": true,
"checksum_dest": null,
"checksum_src": "42cd41801c59a7d62b8d936249817bb29c66c9aa",
"dest": "/tmp/Centos-vault-8.5.2111.repo",
"elapsed": 0,
"gid": 0,
"group": "root",
"md5sum": "3861ff439b02834d39b225045a5b0f97",
"mode": "0644",
"msg": "OK (2495 bytes)",
"owner": "root",
"size": 2495,
"src": "/root/.ansible/tmp/ansible-tmp-1718867579.1066597-109801-88317862966284/tmp65039pbq",
"state": "file",
"status_code": 200,
"uid": 0,
"url": "https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo"
}
将centos8的yum源下载到了/tmp目录下