RHCE学习

编程入门 行业动态 更新时间:2024-10-19 06:17:26

<a href=https://www.elefans.com/category/jswz/34/1736233.html style=RHCE学习"/>

RHCE学习

RHCE学习 — 第八次作业(ansible)


\

一、基础环境:

采用一台centos7的主机为控制节点master,两台centos8的主机为受控节点(node01,node02)

三台主机均安装ansible并可以使用

三台主机有IP地址且可以互相ping通

均配置时间服务器,可以同步时间

均可以使用yum安装软件

均关闭防火墙与selinux


二、环境搭建

首先在三台主机上都创建一个普通用户用于远程连接

[root@node01 ~]# useradd inui
[root@node01 ~]# echo toko | passwd --stdin inui
Changing password for user inui.
passwd: all authentication tokens updated successfully.


然后配置该普通用户的sudo

[root@node01 ~]# grep inui /etc/sudoers
inui ALL=(ALL)      NOPASSWD: ALL


由于ansible采用OpenSSH进行传输,因此为了方便起见,配置控制节点的inui用户可以用密钥登录受控节点inui用户

且控制节点的root可以免密登录受控节点的root

也为了方便起见,在/etc/hosts文件中写入三台主机所对应的域名

[root@master ~]# su - inui
[inui@master ~]$ ssh-keygen
[inui@master ~]$ ssh-copy-id inui@node01
[inui@master ~]$ ssh-copy-id inui@node02
[inui@master ~]$ ssh-copy-id inui@master

三、ansible基本配置操作


ansible的配置文件生效与当前工作目录有关,有下面三种形式,优先级依次提高
[root@master ~]# ll /etc/ansible/ansible.cfg
[root@master ~]# ll ~/.ansible.cfg
[root@master ~]# ll ./ansible.cfg


所以这里我自己创建一个cfg文件


文件内容如下:


其中包含了inventory清单文件

ansible默认查找生效ansible.cfg文件中所指定的inventory文件,也可以使用ansible --list -i选项临时指定inventory文件
清单文件的默认示例文件在/etc/ansible/hosts

由于不需要分组,所以这里只需要把三台主机都写到清单文件里即可


这时候测试一下


所有主机都success,ansible的基本配置就完成了


四、作业题目


ansible有许多模块

ansible-doc -l #查看所有模块
ansible-doc 模块名 #查看模块用法
ansible 主机组 -m 模块 [-a ‘命令’] #ansible运行临时命令


1,以all主机组为目标执行id命令


2,使用copy模块修改所有主机上的/etc/motd文件内容为welcome to ansible

[inui@master ansible]$ ansible all -m copy -a 'content="welcome to ansible\n" dest=/etc/motd'


3,使用command模块查看/etc/motd文件的内容


4,使用user模块创建用户wukong,uid为2000

[inui@master ansible]$ ansible all -m user -a 'name=wukong state=present uid=2000'


5,使用yum模块安装httpd软件包并使用service模块启动该服务

[inui@master ansible]$ ansible all -m yum -a 'name=httpd state=latest'
[inui@master ansible]$ ansible all -m service -a 'name=httpd state=started'

更多推荐

RHCE学习

本文发布于:2024-02-10 18:52:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1676794.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:RHCE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!