目录
要求:
一、环境配置
二、实验过程
实验一:
实验二:
实验三:
实验四:
三、总结
要求:
ansible:
需求一:使用shell或者command模块创建用户:testuser1
需求二:使用script/shell/command模块执行脚本:脚本内容为:echo "Hello Wor
ld"
需求三:使用copy模块:将管理主机(manage)上 /root/ansible/copyfile 文件拷贝到 00被管理主机 node1上的/home/testuser1目录下
需求四:使用blockinfile模块:向/home/testuser1/copyfile文件中插入:Hello World
在Hello World之后插入: Hello Shaanxi
在Hello World之前插入:Hello China
删除 Hello World
替换 Hello Shaanxi 为 Hello Xian
一、环境配置
1.准备两台虚拟机本实验两台均为rhel,一台为manage(管理主机 192.168.80.131),
node1(被管理主机 192.168.80.132)。
2.在manage上配置ansible下载以下安装包:
yum install epel-release.noarch -y
yum install ansible -y
二、实验过程
实验一:
1.在manage上进入到ansible下:cd /root/ansible
2.创建用户testuser1;使用shell模块:ansible node1 -m shell -a 'sudo useradd testuser1'
3.在manage查看是否创建testuser1: ansible node1 -m command -a 'ls /home'
4.在node1查看testuser1:
(1) cat /etc/passwd
(2)ls -l /home
实验二:
1.在manage上使用file模块在/home/testuser1/创建一个文件test文件:
ansible node1 -m file -a 'path=/home/testuser1/test state=touch'
2.在node1的/home/testuser1/test文件添加内容为Hello World :
echo "Hello World" >/home/testuser1/test
3.在manage上使用command模块查看内容:
ansible node1 -m command -a 'cat test chdir=/home/testuser1/test/'
实验三:
1.在manage的/root/ansible/copyfile/目录下创建一个test文件:
touch test /root/ansible/copyfile/
2.在test文件中输入以下内容:
echo "from manage copyfile" > /root/ansible/copyfile/test
3.将manage的/root/ansible/copyfile的文件传送到node1的/home/testuser1/
ansible node1 -m copy -a 'src=/root/ansible/copyfile/ dest=/home/testuser1/'
4.在manage中查看是否成功传入到node1:
ansible node1 -m command -a 'cat test chdir="/home/testuser1/"'
5.在node1下查看是否传输成功:
cat /home/testuser1/test
实验四:
1.在manage向/home/testuser1/test文件中插入内容Hello World:
ansible node1 -m blockinfile -a 'path=/home/testuser1/test block="Hello World" marker="#
{mark} first"'
2.进入node1的/home/testuser1/
3.在node1上产看test内容:cat test
4.在Hello World之后插入: Hello Shaanxi
ansible node1 -m blockinfile -a 'path=/home/testuser1/test block="Hello Shaanxi"
insertafter="Hello World" marker="#{mark} second"'
5.在node1上产看test内容:cat test
6.在Hello World之前插入:Hello China:
ansible node1 -m blockinfile -a 'path=/home/testuser1/test block="Hello China"
insertbefore="Hello World" marker="#{mark} third"'
7.在node1上产看test内容:cat test
8. 删除 Hello World:
ansible node1 -m blockinfile -a 'path=/home/testuser1/test block=" " marker="#{mark} first"'
9.在node1上产看test内容:cat test
10.替换 Hello Shaanxi 为 Hello Xian:
ansible node1 -m blockinfile -a 'path=/home/testuser1/test block="Hello Xian" marker="#
{mark} first"'
11.在node1上产看test内容:cat test
三、总结
1.常见问题与解决
(1)不会使用相关指令,查询:ansible-doc -s 模块
(2)在使用blockinfile模块的时候注意增加标签
2.实验心得
本实验主要考察的是ansible的模块操作应用,利用ansible帮助我们能够快速的解决多台主机同时管理的问题,本实验并不是很难主要注意格式:ansible 被管理主机 -m 模块 -a '模块的相关指令',注意学会查看模块帮助:ansible-doc -s 模块。记住在配置的时候注意格式的问题。