Linux   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ansible部署 应用 模块大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

Top 1 案例1:环境准备 1.1 问题 本案例要求准备ansible的基础环境: 启动6台虚拟机 禁用selinux和firewalld 编辑/etc/hosts 配置yum扩展源并在管理节点安装ansible 1.2 方案 此方案需要准备六台主机,1台管理主机,5台托管主机,以实现批量程序部署,批量运行命令等功能,具体要求如表-1所示: 表-1 1.3 步骤 实现此案例需要按照如下步骤进行。
Top

1 案例1:环境准备

1.1 问题

本案例要求准备ansible的基础环境:

  • 启动6台虚拟机
  • 禁用selinux和firewalld
  • 编辑/etc/hosts
  • 配置yum扩展源并在管理节点安装ansible

1.2 方案

此方案需要准备六台主机,1台管理主机,5台托管主机,以实现批量程序部署,批量运行命令等功能,具体要求如表-1所示:

表-1

ansible部署 应用 模块

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:基础环境准备

1)启动6台虚拟机,由于已经讲过怎么创建,这里不再在案例里体现

2)真机配置yum仓库

  1. [[email protected] ~]# tar -xf ansible_soft.tar.xz
  2. [[email protected] ~]# cd ansible_soft/
  3. [[email protected] ansible_soft]# @H_610_76@mkdir /var/ftp/ansible
  4. [[email protected] ansible_soft]# cp * /var/ftp/ansible
  5. [[email protected] ansible_soft]# createrepo /var/ftp/ansible
  6. Spawning worker 0 with 1 pkgs
  7. Spawning worker 1 with 1 pkgs
  8. Spawning worker 2 with 1 pkgs
  9. Spawning worker 3 with 1 pkgs
  10. Spawning worker 4 with 1 pkgs
  11. Spawning worker 5 with 1 pkgs
  12. Workers Finished
  13. Saving PriMary Metadata
  14. Saving file lists Metadata
  15. Saving other Metadata
  16. Generating sqlite DBs
  17. sqlite DBs complete

3)修改主机名(容易区分,6台机器都需要修改)这里以ansible主机为例子

  1. [@L_674_17@ ~]# echo ansible > /etc/hostname
  2. [[email protected] ~]# hostname ansible

4)配置ip(6台机器都需要配置),这里以ansible主机为例子

  1. [[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
  2. # Generated by dracut initrd
  3. DEVICE="eth0"
  4. ONBOOT="yes"
  5. IPV6INIT="no"
  6. IPV4_FAILURE_FATAL="no"
  7. NM_CONTROLLED="no"
  8. TYPE="Ethernet"
  9. BOOTPROTO="static"
  10. Ipaddr=192.168.1.51
  11. PREFIX=24
  12. GATEWAY=192.168.1.254
  13. [[email protected] ~]# systemctl restart network
  14. [[email protected] ~]# ifconfig
  15. eth0: flags=4163<UP,BroaDCAST,RUNNING,MULTICAST> mtu 1500
  16. inet 192.168.1.51 netmask 255.255.255.0 broadcast 192.168.1.255
  17. ether 52:54:00:b2:69:9e txqueuelen 1000 (Ethernet)
  18. RX packets 234 bytes 16379 (15.9 KiB)
  19. RX errors 0 dropped 36 overruns 0 frame 0
  20. TX packets 31 bytes 2618 (2.5 KiB)
  21. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

5)配置yum客户端,在管理节点ansible上面配置

  1. [[email protected] ~]# vim /etc/yum.repos.d/local.repo
  2. [local_repo]
  3. name=CentOS-$releasever - Base
  4. baseurl="ftp://192.168.1.254/system"
  5. enabled=1
  6. gpgcheck=1
  7. [local]
  8. name=local
  9. baseurl="ftp://192.168.1.254/ansible"
  10. enabled=1
  11. gpgcheck=0
  12. [[email protected] ~]# yum clean all
  13. [[email protected] ~]# yum repolist
  14. [[email protected] ~]# yum -y install ansible
  15. [[email protected] ~]# ansible --version
  16. ansible 2.4.2.0        //显示版本说明安装成功
  17. config file = /etc/ansible/ansible.cfg
  18. configured module search path = [u‘/root/.ansible/plugins/modules‘,u‘/usr/share/ansible/plugins/modules‘]
  19. ansible python module LOCATIOn = /usr/lib/python2.7/site-packages/ansible
  20. executable LOCATIOn = /usr/bin/ansible
  21. python version = 2.7.5 (default,Aug 4 2017,00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

6)请在6台主机上面@R_618_2174@,这里以ansible主机为例子

  1. [[email protected] ansible]# cat /etc/hosts
  2. 192.168.1.51 ansible
  3. 192.168.1.52 web1
  4. 192.168.1.53 web2
  5. 192.168.1.54 db1
  6. 192.168.1.55 db2
  7. 192.168.1.56 cache

2 案例2:主机定义与分组:

2.1 问题

本案例要求:

2.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:ansible.cfg配置文件

  1. [[email protected] ~]# cd /etc/ansible/
  2. [[email protected] ansible]# ls
  3. ansible.cfg hosts roles
  4. [[email protected] ansible]# vim ansible.cfg
  5. #inventory = /etc/ansible/hosts     //指定分组文件路径,主机的分组文件hosts
  6. [selinux]        //组名称,selinux的相关选项在这个下面配置
  7. ...
  8. [colors]        //组名称,colors的相关选项在这个下面配置
  9. ...

步骤二:定义主机,分组和子组练习

1)静态主机的定义

  1. [[email protected] ansible]# vim hosts
  2. [web]
  3. web1
  4. web2
  5. [db]
  6. db[1:2]                     //1:2为db1到db2两台主机,1:20为db1到db20多台主机
  7. [other]
  8. cache
  9. [[email protected] ansible]# ansible web --list-host //显示web组的主机
  10. hosts (2):
  11. web1
  12. web2
  13. [[email protected] ansible]# ansible db --list-host        
  14. hosts (2):
  15. db1
  16. db2
  17. [[email protected] ansible]# ansible other --list-host
  18. hosts (1):
  19. cache
  20. [[email protected] ansible]# ansible all --list-host //显示所有组的主机
  21. hosts (5):
  22. web1
  23. web2
  24. cache
  25. db1
  26. db2

2)直接测试

  1. [[email protected] ansible]# ansible cache -m ping        
  2. //测试是否可以连接,若失败颜色为红色
  3. cache | UNREACHABLE! => {
  4. "changed": false,
  5. "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname cache: Name or service not kNown\r\n",
  6. "unreachable": true
  7. }

3)修改后测试

  1. [@L_419_29@ ansible]# vi hosts
  2. [other]
  3. cache ansible_ssh_user="root" ansible_ssh_pass="a"
  4. [[email protected] ansible]# ansible other -m ping //测试成功,颜色为绿色
  5. cache | succesS => {
  6. "changed": false,
  7. "ping": "pong"
  8. }

4)不检测主机的sshkey,在第一次连接的时候不用输入yes

  1. [[email protected] ansible]# vim ansible.cfg
  2. 61 host_key_checking = false
  3. [[email protected] ansible]# vim hosts
  4. [web]
  5. web1
  6. web2
  7. [web:vars] //web组:变量(vars不改),web组的多台机器共用一个用户名和密码
  8. ansible_ssh_user="root"
  9. ansible_ssh_pass="a"
  10. [[email protected] ansible]# ansible web -m ping
  11. web2 | succesS => {
  12. "changed": false,
  13. "ping": "pong"
  14. }
  15. web1 | succesS => {
  16. "changed": false,
  17. "ping": "pong"
  18. }

步骤三:定义子组

  1. [[email protected] ansible]# vi hosts
  2. [app:children] //指定子分组(app可改:children不改),web,db是提前分好的组
  3. web
  4. db
  5. [app:vars]
  6. ansible_ssh_user="root"
  7. ansible_ssh_pass="a"
  8. [[email protected] ansible]# ansible app --list-host        //查看
  9. hosts (4):
  10. web1
  11. web2
  12. db1
  13. db2
  14. [[email protected] ansible]# ansible app -m ping        //测试
  15. web1 | succesS => {
  16. "changed": false,
  17. "ping": "pong"
  18. }
  19. web2 | succesS => {
  20. "changed": false,
  21. "ping": "pong"
  22. }
  23. db1 | succesS => {
  24. "changed": false,
  25. "ping": "pong"
  26. }
  27. db2 | succesS => {
  28. "changed": false,
  29. "ping": "pong"
  30. }

步骤四:多路径练习

自定义的ansible文件只在当前路径生效

1)多路径

  1. [[email protected] ~]# mkdir aaa
  2. [[email protected] ~]# cd aaa/
  3. [[email protected] aaa]# vim myhost
  4. [app1]
  5. web1
  6. db1
  7. [app2]
  8. web2
  9. db2
  10. [app:children]
  11. app1
  12. app2
  13. [other]
  14. cache
  15. [app:vars]
  16. ansible_ssh_user="root"
  17. ansible_ssh_pass="a"
  18. [[email protected] aaa]# touch ansible.cfg
  19. [[email protected] aaa]# grep -Ev "^#|^$" /etc/ansible/ansible.cfg
  20. [defaults]
  21. roles_path = /etc/ansible/roles:/usr/share/ansible/roles
  22. host_key_checking = false
  23. [inventory]
  24. [privilege_escalation]
  25. [paramiko_connection]
  26. [ssh_connection]
  27. [persistent_connection]
  28. [accelerate]
  29. [selinux]
  30. [colors]
  31. [diff]
  32. [[email protected] aaa]# vim ansible.cfg
  33. [defaults]
  34. inventory = myhost
  35. host_key_checking = false

2)测试结果

  1. [[email protected] aaa]# ansible app1 -m ping
  2. web1 | succesS => {
  3. "changed": false,
  4. "ping": "pong"
  5. }
  6. [[email protected] aaa]# ansible app -m ping
  7. web1 | succesS => {
  8. "changed": false,
  9. "ping": "pong"
  10. }
  11. [[email protected] aaa]# ansible app --list-host
  12. hosts (4):
  13. web1
  14. db1
  15. web2
  16. db2
  17. [[email protected] aaa]# cd
  18. [[email protected] ~]# ansible app1 --list-host //切换到别的目录,测试失败
  19. [WARNING]: Could not match supplied host pattern,ignoring: app1
  20. [WARNING]: No hosts matched,nothing to do
  21. hosts (0):

3 案例3:动态主机

3.1 问题

本案例要求:

3.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:脚本输出主机列表

  1. [[email protected] ~]# cd aaa
  2. [[email protected] aaa]# vim host.py
  3. #!/usr/bin/python
  4. import json
  5. hostlist = {}
  6. hostlist["bb"] = ["192.168.1.52","192.168.1.53"]
  7. hostlist["192.168.1.54"] = {
  8. "ansible_ssh_user":"root","ansible_ssh_pass":"pwd"
  9. }
  10. hostlist["aa"] = {
  11. "hosts" : ["192.168.1.55","192.168.1.56"],
  12. "vars" : {
  13. "ansible_ssh_user":"root","ansible_ssh_pass":"pwd"
  14. }
  15. }
  16. print(json.dumps(hostlist))
  17. [[email protected] aaa]# chmod 755 ./host.py

步骤二:脚本输出样例(这样写输出的结果有些乱)

  1. [[email protected] aaa]# ./host.py
  2. {"aa": {"hosts": ["192.168.1.55","vars": {"ansible_ssh_user": "root","ansible_ssh_pass": "a"}},"192.168.1.54": {"ansible_ssh_user": "root","ansible_ssh_pass": "a"},"bb": ["192.168.1.52","192.168.1.53"]}

步骤三:可以用sHell脚本输出

  1. [[email protected] aaa]# vim my.sh
  2. #!/bin/bash
  3. echo
  4. { "aa": {
  5. "hosts":
  6. ["192.168.1.55","192.168.1.56"],
  7. "vars": {
  8. "ansible_ssh_user": "root",
  9. "ansible_ssh_pass": "a"}
  10. },
  11. }‘
  12. [[email protected] aaa]# chmod 755 my.sh
  13. [[email protected] aaa]# ./my.sh
  14. { "aa": {
  15.     "hosts":
  16.         ["192.168.1.55",
  17. "vars": {
  18.         "ansible_ssh_user": "root",
  19.         "ansible_ssh_pass": "a"}
  20. },
  21. }
  22. [[email protected] aaa]# vim ansible.cfg
  23. [defaults]
  24. inventory = my.sh
  25. host_key_checking = false
  26. [[email protected] aaa]# ansible aa -m ping
  27. 192.168.1.55 | succesS => {
  28. "changed": false,
  29. "ping": "pong"
  30. }
  31. 192.168.1.56 | succesS => {
  32. "changed": false,
  33. "ping": "pong"
  34. }

步骤二:批量执行

1)查看负载

  1. [[email protected] aaa]# ansible app -m command -a ‘uptime‘    
  2. db1 | succesS | rc=0 >>
  3. 11:35:52 up 1:59,2 users,load average: 0.00,0.01,0.01
  4. web1 | succesS | rc=0 >>
  5. 11:35:52 up 2:00,0.02
  6. db2 | succesS | rc=0 >>
  7. 11:35:53 up 1:59,0.03
  8. web2 | succesS | rc=0 >>
  9. 11:35:52 up 1:59,0.02

2)查看时间

  1. [[email protected] aaa]# ansible app -m command -a ‘date +%F\ %T‘
  2. db1 | succesS | rc=0 >>
  3. 2018-09-06 11:42:18
  4. web1 | succesS | rc=0 >>
  5. 2018-09-06 11:42:18
  6. web2 | succesS | rc=0 >>
  7. 2018-09-06 11:42:18
  8. db2 | succesS | rc=0 >>
  9. 2018-09-06 11:42:19

4 案例4:批量部署证书文件

4.1 问题

本案例要求:

  • 创建一对密钥
  • 给所有主机部署密钥

4.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:批量部署证书文件,给所有主机部署密钥

1)创建密钥

  1. [[email protected] aaa]# cd /root/.ssh/
  2. [[email protected] .ssh]# vi /etc/ansible/hosts
  3. [web]
  4. web1
  5. web2
  6. [db]
  7. db[1:2]
  8. [other]
  9. cache
  10. [[email protected] .ssh]# ansible all -m ping //直接ping会报错
  11. [[email protected] .ssh]# ssh-keygen -t rsa -b 2048 -N ‘‘ //创建密钥

2)给所有主机部署密钥

  1. [[email protected] .ssh]# ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key=‘$(< /root/.ssh/id_rsa.pub)‘" -k
  2. SSH password:        //输入密码
  3. [[email protected] .ssh]# ansible all -m ping //成功
  4. web2 | succesS => {
  5. "changed": false,
  6. "ping": "pong"
  7. }
  8. cache | succesS => {
  9. "changed": false,
  10. "ping": "pong"
  11. }
  12. [[email protected] .ssh]# ssh web1        //不需要输入密码,可以直接登陆
  13. Last login: Thu Sep 6 11:49:00 2018 from 192.168.1.51
  14. [[email protected] ~]#

5 案例5:练习模块

5.1 问题

本案例要求:

  • 练习使用COR_267_11845@mand,sHell,raw,script模块

5.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:练习模块

ansible-doc //模块的手册,相当于man

ansible-doc -l //列出所有模块

ansible-doc 模块名 //查看指定模块的帮助信息

1)ping模块

  1. [[email protected] .ssh]# ansible web1 -m ping
  2. web1 | succesS => {
  3. "changed": false,
  4. "ping": "pong"
  5. }

2)command模块

  1. [[email protected] .ssh]# ansible web1 -m command -a ‘chdir=/tmp touch f1‘ //创建成功
  2. [[email protected] ~]# cd /tmp/
  3. [[email protected] tmp]# ls        //在web1上面查看
  4. f1

3)sHell模块

  1. [[email protected] .ssh]# ansible web1 -m sHell -a ‘chdir=/tmp touch f2‘ //创建成功
  2. [[email protected] ~]# cd /tmp/
  3. [[email protected] tmp]# ls    //在web1上面查看
  4. f2

4)raw模块

  1. [[email protected] .ssh]# ansible web1 -m raw -a ‘chdir=/tmp touch f3‘
  2. //文件可以创建,但无法切换目录,文件用户家目录下生成
  3. web1 | succesS | rc=0 >>
  4. Shared connection to web1 closed.
  5. [[email protected] tmp]# cd /root/
  6. [[email protected] ~]# ls        //在web1上面查看
  7. f3

5)script模块

对于太复杂的命令,可以写个脚本,然后用script模块执行

在web1主机上创建zhangsan3用户修改zhangsan3的密码为123456,设置zhangsan3第一次登陆必须@L_874_131@

用命令写:

  1. [[email protected] .ssh]# ansible web1 -m sHell -a ‘useradd zhangsan3‘
  2. [[email protected] .ssh]# ansible web1 -m sHell -a ‘echo 123456 | passwd --stdin zhangsan3‘
  3. [[email protected] .ssh]# ssh -l zhangsan3 web1
  4. [email protected]‘s password: //输入zhangsan3的密码
  5. [[email protected] .ssh]# ansible web1 -m sHell -a ‘chage -d 0 zhangsan3
  6. [[email protected] .ssh]# ssh -l zhangsan3 web1

用脚本写,script模块执行:

  1. [[email protected] .ssh]# vim user.sh
  2. #!/bin/bash
  3. useradd zhangsan3
  4. echo 123456 | passwd --stdin zhangsan3
  5. chage -d 0 zhangsan3
  6. echo
  7. [[email protected] .ssh]# ansible web1 -m script -a ‘./user.sh‘
  8. web1 | succesS => {
  9. "changed": true,
  10. "rc": 0,
  11. "stderr": "Shared connection to web1 closed.\r\n",
  12. "stdout": "Changing password for user zhangsan3.\r\npasswd: all authentication tokens updated successfully.\r\n\r\n",
  13. "stdout_lines": [
  14. "Changing password for user zhangsan3.",
  15. "passwd: all authentication tokens updated successfully.",
  16. ""
  17. ]
  18. }
  19. [[email protected] .ssh]# ssh -l lisi web1
  20. [email protected]‘s password:
  21. You are required to change your password immediately (root enforced)
  22. Last login: Thu Sep 6 14:51:33 2018 from 192.168.1.51
  23. WARNING: Your password has expired.
  24. You must change your password Now and login again!
  25. Changing password for user lisi.
  26. Changing password for lisi.
  27. (current) UNIX password:

6 案例6:模块练习

6.1 问题

本案例要求:

6.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:模块练习

1)使用COpy模块同步数据

src:要复制到进程主机的文件在本地的地址,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用"/"来结尾,则只复制目录里的内容,如果没有使用"/"来结尾,则包含目录在内的整个内容全部复制,类似于rsync

dest:必选项。进程主机的绝对路径,如果源文件一个目录,那么该路径也必须是个目录

BACkup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no

force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。认为yes

  1. [[email protected] .ssh]# ansible all -m sHell -a ‘cat /etc/resolv.conf‘
  2. //查看/etc/resolv.conf
  3. cache | succesS | rc=0 >>
  4. ; generated by /usr/sbin/dhclient-script
  5. nameserver 192.168.1.254
  6. search localhost
  7. db2 | succesS | rc=0 >>
  8. ; generated by /usr/sbin/dhclient-script
  9. nameserver 192.168.1.254
  10. search localhost
  11. web1 | succesS | rc=0 >>
  12. ; generated by /usr/sbin/dhclient-script
  13. nameserver 192.168.1.254
  14. search localhost
  15. web2 | succesS | rc=0 >>
  16. ; generated by /usr/sbin/dhclient-script
  17. nameserver 192.168.1.254
  18. search localhost
  19. db1 | succesS | rc=0 >>
  20. ; generated by /usr/sbin/dhclient-script
  21. nameserver 192.168.1.254
  22. search localhost
  23. [[email protected] .ssh]# vi /etc/resolv.conf
  24. nameserver 172.40.1.10
  25. [[email protected] .ssh]# ansible all -m copy -a ‘src=/etc/resolv.conf dest=/etc/resolv.conf‘ //复制本机的resolv.conf到其他主机
  26. [[email protected] .ssh]# ansible all -m sHell -a ‘cat /etc/resolv.conf‘
  27. //查看有nameserver 172.40.1.10
  28. [[email protected] ~]# mkdir aa
  29. [[email protected] ~]# ansible all -m copy -a ‘src=/root/aa dest=/root/a.log‘
  30. //复制本机的目录/root/aa到其他机器的/root/a.log,复制目录只能少数批量执行同步
  31. [[email protected] ~]# ansible all -m sHell -a ‘ls -ld /root‘
  32. db2 | succesS | rc=0 >>
  33. dr-xr-x---. 4 root root 167 Sep 6 11:48 /root
  34. web2 | succesS | rc=0 >>
  35. dr-xr-x---. 4 root root 167 Sep 6 11:48 /root
  36. cache | succesS | rc=0 >>
  37. dr-xr-x---. 4 root root 177 Sep 6 14:35 /root
  38. db1 | succesS | rc=0 >>
  39. dr-xr-x---. 4 root root 167 Sep 6 11:48 /root
  40. web1 | succesS | rc=0 >>
  41. dr-xr-x---. 4 root root 177 Sep 6 14:35 /root

2)使用lineinfile模块编辑文件

以行为基础,整行修改(整行被替换掉)

  1. [[email protected] ~]# ansible cache -m lineinfile \
  2. -a ‘path=/etc/sysconfig/network-scripts/ifcfg-eth0 \
  3. regexp="^ONBOOT=" line="ONBOOT=\"no\""‘
  4. cache | succesS => {
  5. "BACkup": "",
  6. "changed": true,
  7. "msg": "line replaced"
  8. }

3)使用replace模块修改文件

修改文件的某一部分(替换一行中匹配的内容),以正则表达式匹配为基础修改

  1. [[email protected] ~]# ansible cache -m replace -a \
  2. ‘path=/etc/sysconfig/network-scripts/ifcfg-eth0 \
  3. regexp="^(ONBOOT=).*" replace="\1\"yes\""‘
  4. cache | succesS => {
  5. "changed": true,
  6. "msg": "1 replacements made"
  7. }

7 案例7:综合练习

7.1 问题

本案例要求:

  • 安装Apache并修改监听端口为8080
  • 修改ServerName配置,执行apachectl -t命令不报错
  • 设置认主页Hello world
  • 启动服务并设开机自启

7.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:熟悉模块

1)yum模块

  1. [[email protected] ~]# ansible other -m yum -a ‘name="lrzsz" state=removed‘
  2. //lrzsz软件包名,removed=absent删除
  3. [[email protected] ~]# ansible other -m yum -a ‘name="lrzsz,lftp" state=installed‘
  4. //安装多个软件包,不写state认为安装

2)service模块

  1. [[email protected] ~]# ansible other -m service -a ‘name="sshd" enabled="yes" state="started"‘ //sshd服务名,开机启动同时启动这个服务

3)setup模块

filter 过滤指定的关键字(可以过滤到我们需要的信息)

  1. [[email protected] ~]# ansible cache -m setup -a ‘filter=os‘
  2. cache | succesS => {
  3. "ansible_facts": {},
  4. "changed": false
  5. }
  6. [[email protected] ~]# ansible cache -m setup -a ‘filter=ansible_diStribution‘
  7. cache | succesS => {
  8. "ansible_facts": {
  9. "ansible_diStribution": "CentOS"
  10. },
  11. "changed": false
  12. }

步骤二:安装Apache

1)安装Apache服务设置开机自启

  1. [[email protected] ~]# ansible cache -m yum -a ‘name=httpd state=installed‘
  2. [[email protected] ~]# ansible cache -m service -a ‘name=httpd enabled=yes state=started‘

2)修改端口号为8080

  1. [[email protected] ~]# ssh cache
  2. Last login: Thu Sep 6 15:30:33 2018 from 192.168.1.51
  3. [@L_289_197@ ~]# cat /etc/httpd/conf/httpd.conf | grep Listen
  4. Listen 80
  5. [[email protected] ~]# ansible cache -m lineinfile -a ‘path="/etc/httpd/conf/httpd.conf" regexp="^Listen " line="Listen 8080"‘cache | succesS => {
  6. "BACkup": "",
  7. "msg": "line replaced"
  8. }
  9. [[email protected] ~]# ssh cache
  10. Listen 8080

步骤三:修改ServerName配置,执行apachectl -t命令不报错

1)没有修改之前

  1. [[email protected] ~]# apachectl -t //有报错
  2. AH00558: httpd: Could not reliably determine the server‘s fully qualified domain name,using 192.168.1.56. Set the ‘ServerName‘ directive globally to suppress this message
  3. Syntax OK

2)修改之后

  1. [[email protected] ~]# ansible cache -m lineinfile -a ‘path="/etc/httpd/conf/httpd.conf" regexp="^ServerName " line="ServerName 0.0.0.0"‘
  2. cache | succesS => {
  3. "BACkup": "",
  4. "msg": "line added"
  5. }
  6. [[email protected] ~]# ssh cache
  7. Last login: Thu Sep 6 15:36:08 2018 from 192.168.1.51
  8. [[email protected] ~]# apachectl -t
  9. Syntax OK

步骤四:设置认主页为Hello world

  1. [[email protected] ~]# ansible cache -m copy -a ‘src=/root/index.html dest=/var/www/html/index.html‘ ///root/index.html这个页面可以自己写
  2. cache | succesS => {
  3. "changed": true,
  4. "checksum": "22596363b3de40b06f981fb85d82312e8c0ed511",
  5. "dest": "/var/www/html/index.html",
  6. "gid": 0,
  7. "group": "root",
  8. "md5sum": "6f5902ac237024bdd0c176cb93063dc4",
  9. "mode": "0644",
  10. "owner": "root",
  11. "size": 12,
  12. "src": "/root/.ansible/tmp/ansible-tmp-1536219767.29-30682157793478/source",
  13. "state": "file",
  14. "uid": 0
  15. }

大佬总结

以上是大佬教程为你收集整理的ansible部署 应用 模块全部内容,希望文章能够帮你解决ansible部署 应用 模块所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。