ansible常用模块(command、copy、file、yum、service、firewalld)

前言:

本篇博客的内容在上一篇博客ansible搭建的基础上完成:
自动化运维工具Ansible的搭建

一、Ansible的command默认模块的简单使用

1、在server1上执行

[devops@server1 ansible]$ ansible test -m command -a 'df -h'

test指ansible中test组,-m后指定模块 -a后加这个模块的参数
ansible常用模块(command、copy、file、yum、service、firewalld)_第1张图片
注意:command模块为默认的模块,如果不-m指定的话,默认使用的command模块

二、copy模块

1、copy

[devops@server1 ansible]$ ansible test -m copy -a 'src=/etc/passwd dest=/tmp/passwd'
src是原文件 dest后是复制到哪里

ansible常用模块(command、copy、file、yum、service、firewalld)_第2张图片

  • 查看
    在这里插入图片描述

三、file模块

查看server2某文件的权限

[devops@server1 ansible]$ ansible test -a 'ls -l /tmp/passwd'
server2 | CHANGED | rc=0 >>
-rw-rw-r-- 1 devops devops 1051 Jun 12 14:29 /tmp/passwd

更改权限:

[devops@server1 ansible]$ ansible test -m file -a 'dest=/tmp/passwd mode=600'

ansible常用模块(command、copy、file、yum、service、firewalld)_第3张图片

四、yum模块

查看yum模块的使用方法:

[devops@server1 ansible]$ ansible-doc yum

ansible常用模块(command、copy、file、yum、service、firewalld)_第4张图片
注意:我们现在使用的是普通用户,没有执行yum命令的权力:
先配置devops用户的sudo权限:

[root@server2 ~]# vim /etc/sudoers
 92 devops  ALL=(ALL)       NOPASSWD: ALL

[root@server3 ~]# vim /etc/sudoers
 92 devops  ALL=(ALL)       NOPASSWD: ALL

在server2-3上,下载httpd服务

[devops@server1 ansible]$ ansible test -m yum -a 'name=httpd state=present' -b
一定要加入-b选项,否则会报错

ansible常用模块(command、copy、file、yum、service、firewalld)_第5张图片

  • 查看是否下载成功:
    在这里插入图片描述

不加-b可以在ansible的配置文件里面配置:

[devops@server1 ansible]$ vim ansible.cfg
[defaults]


inventory      = inventory

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False

ansible常用模块(command、copy、file、yum、service、firewalld)_第6张图片
ansible常用模块(command、copy、file、yum、service、firewalld)_第7张图片
卸载:
ansible常用模块(command、copy、file、yum、service、firewalld)_第8张图片

五、service模块

启动db组的httpd服务:

[devops@server1 ansible]$ ansible db -m service -a 'name=httpd state=started'

ansible常用模块(command、copy、file、yum、service、firewalld)_第9张图片
测试:
ansible常用模块(command、copy、file、yum、service、firewalld)_第10张图片
这是server3的httpd服务的默认发布页,不太直观,我们写一个发布页:

[devops@server1 ansible]$ ansible db -m copy -a 'content="www.redhat.com\n" dest=/var/www/html/index.html'

ansible常用模块(command、copy、file、yum、service、firewalld)_第11张图片

六、firewalld模块

如何在开启防火墙的情况下,正常使用httpd服务呢?

首先开启防火墙

[devops@server1 ansible]$ ansible db -m service -a 'name=firewalld state=started enabled=true'

测试:
[devops@server1 ansible]$ curl server3
curl: (7) Failed connect to server3:80; No route to host

把httpd服务加入防火墙白名单:

[devops@server1 ansible]$ ansible db -m firewalld -a 'service=http state=enabled permanent=yes immediate=yes'

ansible常用模块(command、copy、file、yum、service、firewalld)_第12张图片

你可能感兴趣的:(Linux,运维)