Ansible的常用模块

目录

  • ansible常用模块

    • 11.1 setup模块的使用

    • 10.1 get_url模块的选项

    • 10.2 get_url模块的使用

    • 9.1 fetch模块的选项

    • 9.2 fetch模块的使用

    • 8.1 group模块的选项

    • 8.2 group模块的使用

    • 7.1 user模块的选项

    • 7.2 user模块的使用

    • 6.1 systemd模块的选项

    • 6.2 systemd模块的使用

    • 5.1 service模块的选项

    • 5.2 service模块的使用

    • 4.1yum模块的选项

    • 4.2 yum模块的使用

    • 3.1 yum_repository的选项

    • 3.2 yum_repository的使用

    • 2.1 copy模块的选项

    • 2.2 copy模块的使用

    • 1.1 file模块的选项

    • 1.2 file模块的使用

    • 1.2.2 创建目录

    • 1.2.3 删除文件/目录

    • 1.2.1 使用file模块在远程主机创建文件

    • 1. file模块

    • 2. copy模块

    • 3. yum_repository模块

    • 4. yum 模块

    • 5. service模块

    • 6. systemd模块

    • 7. user模块

    • 8. group模块

    • 9. fetch模块

    • 10. get_url模块

    • 11. setup 模块

ansible常用模块

这些写的都是常用的模块,还有更多其他的模块可以自行查文档

模块的使用这里都使用Ad-Hoc的方式。playbook的方式后面会说

  • file 模块

  • copy 模块

  • yum_repository 模块

  • yum 模块

  • service 模块

  • systemd 模块

  • user 模块

  • group 模块

  • fetch 模块

  • get_url 模块

  • setup 模块

1. file模块

1.1 file模块的选项

file 模块主要用于远程主机上的文件操作,有一下选项

  • path:必选项,定义文件/目录的路径

  • state:

    • file:查看文件状态,默认选项,如果文件不存在会报错,并不会创建

    • touch:如果文件不存在,则创建,如果已经存在,会更新时间戳

    • absent:删除目录,文件,或取消软链接

    • directory:如果目录不存在,就创建目录

    • link:创建软链接

    • hard:创建硬链接

  • owner:定义文件/目录的拥有人

  • group:定义文件/目录的所属组

  • mode:定义文件的权限

  • src:要被链接的源文件的路径,只应用于state=link的时候

  • dest:被链接到的路径,只应用于state=link的时候

1.2 file模块的使用

1.2.1 使用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

可以看到,一切都是按照我们的想法来创建的

1.2.2 创建目录

[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

1.2.3 删除文件/目录

[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'
}

在删除的时候,是不分目录还是文件的,只需要给定文件的路径就可以删除

2. copy模块

见名知意,就是拷贝文件到远程主机的

2.1 copy模块的选项

  • src: 本地的哪个文件需要复制到远程主机

    • 注意:如果目标是一个目录,他会递归复制,在这种情况下,如果路径是以 / 结尾,那么他会复制目录下的所有文件,如果不以 **/ **结尾的话,就会将本地目录原样的复制过去,类似于rsync

  • dest:必选项,要将源文件复制到远程主机的哪个路径

  • force:如果目标主机存在此文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制,默认为yes

  • backup:在覆盖之前将原文件备份

  • content:用于替代src,可以直接指定文件内容

  • remote_src:如果需要复制的文件在远程主机上而不在ansible的控制节点上使用这个,注意:他只是将src改为远程主机了,并不是从远程主机将文件复制到本地,而是将某个远程主机的文件复制到其他的远程节点

2.2 copy模块的使用

[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

3. yum_repository模块

yum_repository是用来指定yum仓库的repo文件的

3.1 yum_repository的选项

  • file:配置文件的名字,不用包含repo

  • name:yum仓库的名字

  • description:仓库的描述信息

  • baseurl:yum仓库的地址

  • enabled:是否开启这个yum仓库

  • gpgcheck:是否开启gpg检查

3.2 yum_repository的使用

[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

这个模块相对比较简单,功能也比较单一

4. yum 模块

用来使用yum去安装软件包

4.1yum模块的选项

  • name:指定要安装的软件包的名字

  • state:指定动作

    • present:安装

    • latest:最新版本

    • absent:删除

4.2 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'
]
}

5. service模块

这个模块完全可以使用systemd来替代,有这个模块的原因是因为centos的早期版本是使用service xxx start 来管理服务的

5.1 service模块的选项

  • name:必选项,服务的名称

  • state:对当前服务执行的操作

  • enabled:是否开机自启

5.2 service模块的使用

[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': {


输出信息过多,不在这里完全展示

6. systemd模块

6.1 systemd模块的选项

  • name:指定服务的名称

  • state:管理服务状态

    • started

    • restarted

    • stopped

    • reloaded

  • daemon_reload:当服务配置文件发生改变重载服务

  • enabled:是否开机自启

6.2 systemd模块的使用

[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',

7. user模块

7.1 user模块的选项

  • name:指定用户名

  • state:创建还是删除

  • uid:指定用户uid

  • group:指定用户组

  • groups:指定用户的附加组

  • comment:用户的描述信息

  • create_home:是否给他创建home目录

  • home:home目录的路径,需要配合create_home一起使用

  • shell:指定用户的shell环境

  • password:指定用户的密码,这里必须给定加密过后的密码,如果直接将密码明文写在这是没有用的,登录不了

  • remove:删除用户时是否删除home目录,相当于userdel -r

7.2 user模块的使用

[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'
}

8. group模块

group模块是用来创建用户组的

8.1 group模块的选项

  • gid:指定组的gid

  • name:指定组名

  • state:创建还是删除组,选项

    • present

    • absent

8.2 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'
}

9. fetch模块

这个模块正好与copy模块相反,copy是将文件复制到远程,这个则是将远程文件收集到本地

9.1 fetch模块的选项

  • src:远程主机文件路径,只能是文件,不能是目录

  • dest:文件收集到本地的哪个路径

  • flat:默认为no,标识在主控端目录下以远程主机名的方式来显示文件目录结构,yes标识不用主机名的方式来显示,dest的结尾必须是 /

9.2 fetch模块的使用

# 将之前创建的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的话就会直接显示文件名

10. get_url模块

功能从网上下载文件,类似于wget命令

10.1 get_url模块的选项

  • url:下载的url

  • url_password / url_username:主要用于需要用户名和密码验证的情况

  • dest:保存到本地的哪个地方

  • mode:给定权限

  • owner:指定拥有人

  • group:指定所属组

10.2 get_url模块的使用

[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目录下

11. setup 模块

这个模块用来收集远程主机的信息,一般不需要指定参数

11.1 setup模块的使用

这个模块的使用方式有些特别,是将远程主机的信息收集过来之后供我们来看的,方便后面编写不同场景下的通用型playbook

[ansible@master ansible]$ ansible all -m setup  > host_info.yaml
[ansible@master ansible]$ vim host_info.yaml
92.168.200.210 | SUCCESS => {
'ansible_facts': {
'ansible_all_ipv4_addresses': [
'192.168.200.210',
'172.17.0.1',
'10.245.149.0'
],
'ansible_all_ipv6_addresses': [
'fe80::20c:29ff:fe2c:d98',
'fe80::ecee:eeff:feee:eeee'
],
'ansible_apparmor': {
'status': 'disabled'
},
'ansible_architecture': 'x86_64',
'ansible_bios_date': '11/12/2020',
'ansible_bios_version': '6.00',

链接:https://www.cnblogs.com/fsdstudy/p/18258735

                                                              (版权归原作者所有,侵删)

8346b0a6c12313c102e0597356ebf9f2.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值