【RHCE】作业:firewall-cmd操作&ansible自动化运维入门

练习一、firewall-cmd: 设置访问web服务器的80端口时转发到8081端口

Ⅰ、准备工作,准备好8081的验证页面&开启firewalld服务

        vim /etc/httpd/conf.d/myhosts.conf【复习,这个是自己创建虚拟主机的.conf文件】

        因为要验证8081端口这里选择基于同一IP不同端口配置

        

         然后添加个首页验证文件

        

         

Ⅱ、启动firewalld服务并配置转发方法【2选1】

        富规则方法转发:[root@localhost services]# firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.174.0/24" forward-port port="80" protocol="tcp" to-port="8081" to-addr="192.168.174.128"'

                        

         富规则设置完了记得firewall-cmd --reload        

        富规则添加成功 【验证也转发成功了 】

        

 正常方法转发:firewall-cmd --add-forward-port=port=5423:proto=tcp:toport=8081:toaddr=192.168.233.100

练习二、ansible:

① 使用shell或者command模块创建用户:testuser1

②使用script/shell/command模块执行脚本: 脚本内容为:echo "Hello World"

③使用copy模块:将管理主机(manage)上 /root/ansible/copyfile 文件拷贝到 被管理主机node1上的/home/testuser1目录下

④使用blockinfile模块:向/home/testuser1/copyfile文件中插入:Hello World 在Hello World之后插入: Hello Shaanxi 在Hello World之前插入:Hello China 删除 Hello World 替换 Hello Shaanxi 为 Hello Xian

Ⅰ、准备工作:准备三台虚拟机,依次命名(修改hostname)为manage  node1  node2方便管理

        在管理主机manage上:

        [root@manage ~]# yum install epel-release.noarch -y

        [root@manage ~]# yum install ansible -y

         vim /root/ansible/inventory

        []里是分组可以少设置几个,然后上边的node和IP要对应[:children相当于继承什么组]

        

        然后去配置免密登录

[root@manage ansible]# ssh-keygen -t rsa

[root@manage ansible]# ssh-copy-id root@node1的IP

[root@manage ansible]# ssh-copy-id root@node2的IP

vim /root/ansible/ansible.cfg

 

【下一次作业里边我不知道配置什么出问题ssh连接不上node1和node2的ansible用户了,所以建议这里的remote_user=root后面提权那些相当于没用了,】 

[defaults]
#清单文件的配置
inventory=/root/ansible/inventory
#远程登录的用户
remote_user=ansible
#是否询问ssh登录密码(因为我们配置了免密登录.配置成false)
#ask_pass=false
#sudo切换的用户
sudo_user=root
#是否询问切换的密码
ask_sudo_pass=False
#是否检查主机的密钥
#ssh有两种认证方式:基于口令的(用户名和密码登录false),基于公钥的认证(免密登录true)
host_key_checking=true
#日志存放位置
log_path=/var/log/ansible.log
#配置远端执行py文件的临时目录
remote_tmp=~/.ansible/tmp

[privilege_escalation]
#是否切换用户
become=True
#特权升级切换的方法
become_method=sudo
#切换到哪个用户
become_user=root
#是否询问密码
become_ask_pass=False

        【可以验证一下】

 Ⅱ、开始操作【注意ansible管理需要一直在ansible目录下操作】

        ① 使用shell或者command模块创建用户:testuser1

        【在上边准备工作里把.cfg文件配置好了他创建用户的时候会自己提权的】

        [root@manage ansible]# ansible node1 -m shell -a 'sudo useradd testuser1'

        

        【在node1上验证一下,创建成功】 

         

         ②使用script/shell/command模块执行脚本: 脚本内容为:echo "Hello World"

        ansible node1 -m shell -a 'echo "Hello World"'

 

         ③使用copy模块:将管理主机(manage)上 /root/ansible/copyfile 文件拷贝到 被管理主机node1上的/home/testuser1目录下

        记得先在管理主机下创建这个文件再随便编辑点内容方便验证【还是别放内容了0.o,没想到第四题要在里边放内容】

        ansible node1 -m copy -a 'src=/root/ansible/copyfile dest=/home/testuser1'

 

         ④使用blockinfile模块:向/home/testuser1/copyfile文件中插入:Hello World 在Hello World之后插入: Hello Shaanxi 在Hello World之前插入:Hello China 删除 Hello World 替换 Hello Shaanxi 为 Hello Xian

        插入hello world

        ansible node1 -m blockinfile -a 'path=/home/testuser1/copyfile block="Hello

 

         在hello world之后插入hello shaanxi 在hello之前插入hello china

        【这里注意加入mark标签来标记一下他们要不然就会发生替换的情况了】

        ansible node1 -m blockinfile -a 'path=/home/testuser1/copyfile block="Hello Shaanxi" insertafter="Hello World" marker="{mark} test2" state=present'
         ansible node1 -m blockinfile -a 'path=/home/testuser1/copyfile block="Hello China" insertbefore="Hello World" marker="{mark} test3" state=present'

         一系列操作后就成功的出现了bug

        原因是:插入的时候是在hello world之前和之后插入的,此时插入的内容算在标签里面,所以当执行后边删除操作的时候把hello world那个标签删掉相当于把所有内容删掉了,我的想法是在写标记位的时候直接写标签,不写真正的文本内容了【相当于把一部分里有三部分换成看着只有三部分了】

[root@manage ansible]# ansible node1 -m blockinfile -a 'path=/home/testuser1/copyfile block="Hello World" marker="{mark}# test1" state=present'

[root@manage ansible]# ansible node1 -m blockinfile -a 'path=/home/testuser1/copyfile block="Hello Shaanxi" insertafter="^END" marker="{mark}# test2" state=present'

[root@manage ansible]# ansible node1 -m blockinfile -a 'path=/home/testuser1/copyfile block="Hello China" insertbefore="^BEGIN" marker="{mark}# test3" state=present'

 

         【现在就简单了,直接根据他们的标签内容进行操作即可】

        删除hello world 和把hello Shaanxi内容换成xian

 

【成功】 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值