【就业班作业】【第十六周】使用ansible的playbook实现自动化安装httpd
一、本实验准备2台主机
192.168.0.106 ansible控端 192.168.0.107 http服务器
(本次实验已用ntp时间同步,firewall关闭,selinux 关闭)
二、安装ansible程序到控端
本次直接使用yum方式安装ansible. 使用epel源: cat /etc/yum.repos.d/epel.repo [epel] name=Extra Packages for Enterprise Linux 7 baseurl=http://mirrors.aliyun.com/epel/7/x86_64 gpgcheck=0 yum clean all yum repolist yum -y install ansible
三、准备ssh的key认证登录
生成key ssh-keygen 传送key到远程主机 ssh-copy-id 192.168.0.107 验证登录: [root@centos7-ansible-server ~]# ssh-copy-id 192.168.0.107 /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.0.107's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '192.168.0.107'" and check to make sure that only the key(s) you wanted were added. [root@centos7-ansible-server ~]# ssh 192.168.0.107 Last login: Tue Dec 8 10:30:47 2020 from 192.168.0.9
四、准备yum方式安装的yml文件和主机文件
cat installhttpd.yml --- - hosts: webservers remote_user: root gather_facts: no tasks: - name: yum install httpd yum: name=httpd - name: start httpd service shell: /usr/bin/systemctl start httpd tags: start - name: enable httpd service shell: /usr/bin/systemctl enable httpd - name: create test webfiles shell: /usr/bin/echo "this is test web file......" > /var/www/html/index.html ... cat /etc/ansible/hosts [webservers] 192.168.0.107
五、测试及执行ansible-play指令
验证与执行指令 ansible-playbook -C installhttpd.yml PLAY [webservers] **************************************************** TASK [yum install httpd] **************************************************** changed: [192.168.0.107] TASK [start httpd service] ************************************************** skipping: [192.168.0.107] TASK [enable httpd service] ************************************************* skipping: [192.168.0.107] TASK [create test webfiles] ************************************************ skipping: [192.168.0.107] PLAY RECAP **************************************************** 192.168.0.107 : ok=1 changed=1 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0 执行结果: ansible-playbook installhttpd.yml PLAY [webservers] ***************************************************** TASK [yum install httpd] *********************************************** changed: [192.168.0.107] TASK [start httpd service] ********************************************** changed: [192.168.0.107] TASK [enable httpd service] ********************************************* changed: [192.168.0.107] TASK [create test webfiles] ********************************************* changed: [192.168.0.107] PLAY RECAP ************************************************************** 192.168.0.107 : ok=4 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
六、显示测试效果
[root@init-centos7-server-107 ~]# ss -ntlp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* users:(("sshd",1914,3)) LISTEN 0 100 127.0.0.1:25 *:* users:(("master",2150,13)) LISTEN 0 128 :::80 :::* users:(("httpd",4001,4),("httpd",4000,4),("httpd",3999,4),("httpd",3998,4),("httpd",3996,4),("httpd",3904,4),("httpd",3903,4),("httpd",3902,4),("httpd",3901,4),("httpd",3900,4),("httpd",3899,4)) LISTEN 0 128 :::22 :::* users:(("sshd",1914,4)) LISTEN 0 100 ::1:25 :::* users:(("master",2150,14)) [root@init-centos7-server-107 ~]# cat /var/www/html/index.html this is test web file...... [root@init-centos7-server-107 ~]#