ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

ansible常用模块和http协议简述

2022-06-04 22:31:16  阅读:172  来源: 互联网

标签:http name websrvs data 简述 ansible mysql root


实验环境(共三台主机)

系统版本:CentOS7.8

#准备三台主机:
ansible:172.31.5.5
web1: 172.31.5.6
web2: 172.31.5.7

#########在ansible主机执行以下操作############
#安装ansible:
yum instal -y epel-release (配置epel源)
yum install -y ansible

#配置ansible到web1和web2的ssh免密认证
[root@ansible ~]# vim /etc/ssh/ssh_config
#修改下面一行后重启服务
StrictHostKeyChecking no    -- 关闭ssh首次连接询问是否信任远程主机

#执行以下脚本:
IPLIST="  #要远程的主机IP清单
172.31.5.6
172.31.5.7
"
rpm -q sshpass &> /dev/null || yum -y install sshpass  
[ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa  -P ''
export SSHPASS=root  #远程主机密码
for IP in $IPLIST;do
   { sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP; } &
done
wait

#配置inventory清单:
[root@ansible ~]# cat /etc/ansible/hosts
[self]
172.31.5.5  ansible_connection=local   #指定本地连接(ansible操控本机时就不用ssh)
#执行ansible命令时显示别名,如web01
[websrvs]
web01 ansible_ssh_host=172.31.5.6
web02 ansible_ssh_host=172.31.5.7

1、ansible常用模块介绍
幂等性:同一命令执行一次或多次返回相同的结果(在ansible表现为:执行相同的命令,只会生效一次,且每次命令执行后的返回值相同)

#1.1 command 模块
功能:在远程主机执行命令,此为默认模块,可忽略 -m 选项
注意:此命令不支持 $VARNAME < > | ; & 等,可能是用shell模块实现
注意:此模块不具有幂等性
范例:
#进入/etc目录并执行cat  centos-release
[root@ansible ~]# ansible websrvs -m command -a 'chdir=/etc cat centos-elease'	# -m 指定使用哪个模块  -a 指定指定执行的命令
web02 | CHANGED | rc=0 >>
CentOS Linux release 7.9.2009 (Core)
web01 | CHANGED | rc=0 >>
CentOS Linux release 7.9.2009 (Core)

#未指定-m参数,默认使用command模块
[root@ansible ~]# ansible websrvs -a 'chdir=/etc cat centos-release'
web01 | CHANGED | rc=0 >>
CentOS Linux release 7.9.2009 (Core)
web02 | CHANGED | rc=0 >>
CentOS Linux release 7.9.2009 (Core)

#修改默认模块
vim /etc/ansible/ansible.cfg
module_name = command   #默认模块,可以修改为shell模块

#其它实例
ansible websrvs -m command -a 'service vsftpd start'
ansible websrvs -m command -a 'echo magedu |passwd --stdin wang'
ansible websrvs -m command -a 'rm -rf /data/'
ansible websrvs -m command -a 'echo hello > /data/hello.log'
ansible websrvs -m command -a "echo $HOSTNAME"


#1.2 shell模块
功能:和command相似,用shell执行命令,支持各种符号,比如:*,$, > 
注意:此模块不具有幂等性
范例:

#输出主机名(此处显示的是别名,上文已配置)
[root@ansible ~]# ansible websrvs -m shell -a "echo $HOSTNAME"
web02 | CHANGED | rc=0 >>
ansible
web01 | CHANGED | rc=0 >>
ansible
#修改账号密码
[root@ansible ~]# ansible websrvs -m shell -a 'echo centos | passwd --stdin wang'
web02 | CHANGED | rc=0 >>
Changing password for user wang.
passwd: all authentication tokens updated successfully.
web01 | CHANGED | rc=0 >>
Changing password for user wang.
passwd: all authentication tokens updated successfully.
#查看文件属性
[root@ansible ~]# ansible websrvs -m shell -a 'ls -l /etc/shadow'
web02 | CHANGED | rc=0 >>
---------- 1 root root 745 Jun  4 12:39 /etc/shadow
web01 | CHANGED | rc=0 >>
---------- 1 root root 745 Jun  4 12:39 /etc/shadow
#新建文件并添加内容
[root@ansible ~]# ansible websrvs -m shell -a 'echo hello > /data/hello.log'
web01 | CHANGED | rc=0 >>

web02 | CHANGED | rc=0 >>
#查看文件内容
[root@ansible ~]# ansible websrvs -m shell -a 'cat /data/hello.log'
web01 | CHANGED | rc=0 >>
hello
web02 | CHANGED | rc=0 >>
hello

#1.3 script模块
功能:在远程主机上运行ansible服务器上的脚本(无需执行权限)
注意:此模块不具有幂等性
范例:

#在ansible主机准备test.sh脚本(无执行权限)
[root@ansible ~]# echo 'echo hello' > /data/test.sh
[root@ansible ~]# ll /data/test.sh 
-rw-r--r-- 1 root root 11 Jun  4 12:55 /data/test.sh
#远程执行脚本
[root@ansible ~]# ansible self -m script -a /data/test.sh
172.31.5.5 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "", 
    "stderr_lines": [], 
    "stdout": "hello\n", 
    "stdout_lines": [
        "hello"
    ]
}

#1.4 copy模块
#在web1上新建普通文件passwd_copy,查看所有者。
[root@web1 ~]# touch /data/passwd_copy
[root@web1 ~]# ll /data/passwd_copy 
-rw-r--r-- 1 root root 0 Jun  4 13:00 /data/passwd_copy

#调用copy模块,将/etc/passwd拷贝到web1和web2并重命名为passwd_copy
[root@ansible ~]# ansible websrvs -m copy -a "src=/etc/passwd dest=/data/passwd_copy mode=600 backup=yes"  #backup=yes,如果目标文件存在则先备份
#验证结果
[root@ansible ~]# ansible websrvs -a 'ls /data/'
web02 | CHANGED | rc=0 >>
hello.log
passwd_copy
web01 | CHANGED | rc=0 >>
hello.log
passwd_copy
passwd_copy.51539.2022-06-04@13:04:11~   #web1 生成了一个备份文件(以时间戳为后缀)

#指定内容,直接生成目标文件 
[root@ansible ~]# ansible websrvs -m copy -a "content='test line1\ntest line2\n' dest=/data/test.txt" 
#查看结果
[root@ansible ~]# ansible websrvs -a 'cat /data/test.txt' 
web01 | CHANGED | rc=0 >>
test line1
test line2
web02 | CHANGED | rc=0 >>
test line1
test line2

#复制/etc目录自身,注意/etc/后面没有/  #与rsync类似
ansible websrvs -m copy -a "src=/etc dest=/backup"
#复制/etc/下的文件,不包括/etc/目录自身,注意/etc/后面有/
ansible websrvs -m copy -a "src=/etc/ dest=/backup"

#1.5  Get_url 模块
功能: 用于将文件从http、https或ftp下载到被管理机节点上
常用参数如下:
url: 下载文件的URL,支持HTTP,HTTPS或FTP协议
dest: 下载到目标路径(绝对路径),如果目标是一个目录,就用服务器上面文件的名称,如果目标设置了名
称就用目标设置的名称
owner:指定属主
group:指定属组
mode:指定权限
force: 如果yes,dest不是目录,将每次下载文件,如果内容改变,替换文件。如果否,则只有在目标不存在时才会下载该文件
checksum: 对目标文件在下载后计算摘要,以确保其完整性
           示例: checksum="sha256:D98291AC[...]B6DC7B97",
               checksum="sha256:http://example.com/path/sha256sum.txt"
url_username: 用于HTTP基本认证的用户名。 对于允许空密码的站点,此参数可以不使用`url_password'
url_password: 用于HTTP基本认证的密码。 如果未指定`url_username'参数,则不会使用`url_password'参数
validate_certs:如果“no”,SSL证书将不会被验证。 适用于自签名证书在私有网站上使用timeout: URL请求的超时时间,秒为单位

范例:
[root@ansible ~]# ansible websrvs -m get_url -a 'url=http://nginx.org/download/nginx-1.18.0.tar.gz dest=/data/nginx.tar.gz'

#1.6  Fetch 模块
功能:从远程主机提取文件至ansible的主控端,copy相反,目前不支持目录
范例:
[root@ansible ~]# ansible websrvs -m fetch -a 'src=/etc/passwd dest=/data/test'
[root@ansible ~]# tree /data/test  #ansible自动创建目录分别存放远程获取的文件
/data/test
├── web01
│   └── etc
│       └── passwd
└── web02
    └── etc
        └── passwd

#1.7 File 模块
功能:设置文件属性,创建软链接等
范例:
#创建空文件
ansible all -m file  -a 'path=/data/test.txt state=touch'
ansible all -m file  -a 'path=/data/test.txt state=absent'
ansible all -m file -a "path=/root/test.sh owner=wang mode=755"
#创建目录
ansible all -m file -a "path=/data/mysql state=directory owner=mysql group=mysql"
#创建软链接
ansible all -m file -a 'src=/data/testfile path|dest|name=/data/testfile-link
state=link'
#创建目录
ansible all -m file  -a 'path=/data/testdir state=directory' #递归修改目录属性,但不递归至子目录
ansible all -m file -a "path=/data/mysql state=directory owner=mysql group=mysql"
#递归修改目录及子目录的属性
ansible all -m file -a "path=/data/mysql state=directory owner=mysql roup=mysql recurse=yes"

#1.8 stat 模块
功能:检查文件或文件系统的状态
注意:对于Windows目标,请改用win_stat模块
选项:
path:文件/对象的完整路径(必须)
常用的返回值判断:
exists: 判断是否存在
isuid: 调用用户的ID与所有者ID是否匹配

范例:
#查看文件/etc/passwd属性
[root@ansible ~]# ansible self -m stat -a 'path=/etc/passwd'

#新建stat.yaml
[root@ansible ~]# cat stat.yaml 
---
- hosts: websrvs
  
  tasks:
    - name: check file
      stat: path=/data/mysql 
      register: st
    - name: debug
      debug:
        msg: "/data/mysql is not exist"
      when: not st.stat.exists

#查看执行结果
[root@ansible ~]# ansible-playbook stat.yaml 

PLAY [websrvs] ******************************************************************************************

TASK [Gathering Facts] **********************************************************************************
ok: [web01]
ok: [web02]

TASK [check file] ***************************************************************************************
ok: [web02]
ok: [web01]

TASK [debug] ********************************************************************************************
ok: [web01] => {
    "msg": "/data/mysql is not exist"
}
ok: [web02] => {
    "msg": "/data/mysql is not exist"
}

PLAY RECAP **********************************************************************************************
web01                      : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
web02                      : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

#1.9  unarchive 模块
功能:解包解压缩
实现有两种用法:
1、将ansible主机上的压缩包传到远程主机后解压缩至特定目录,设置copy=yes,此为默认值,可省略
2、将远程主机上的某个压缩包解压缩到指定路径下,设置copy=no 

常见参数:
copy:默认为yes,当copy=yes,拷贝的文件是从ansible主机复制到远程主机上,如果设置为copy=no,会在远程主机上寻找src源文件
remote_src:和copy功能一样且互斥,yes表示在远程主机,不在ansible主机,no表示文件在ansible主机上
src:源路径,可以是ansible主机上的路径,也可以是远程主机(被管理端或者第三方主机)上的路径,如果是远程主机上的路径,则需要设置copy=no
dest:远程主机上的目标路径
mode:设置解压缩后的文件权限

范例:
ansible all -m unarchive -a 'src=/data/foo.tgz dest=/var/lib/foo owner=wang group=bin'
ansible all -m unarchive -a 'src=/tmp/foo.zip dest=/data copy=no mode=0777'
ansible all -m unarchive -a 'src=https://example.com/example.zip dest=/data copy=no'
ansible websrvs -m unarchive -a
'src=https://releases.ansible.com/ansible/ansible-2.1.6.0-0.1.rc1.tar.gz dest=/data/   owner=root remote_src=yes'
ansible websrvs -m unarchive -a 'src=http://nginx.org/download/nginx-1.18.0.tar.gz dest=/usr/local/src/ copy=no'


#1.10 Archive 模块
功能:打包压缩保存在被管理节点
范例:
ansible websrvs -m archive  -a 'path=/var/log/ dest=/data/log.tar.bz2 format=bz2 owner=wang mode=0600'

#1.11 Hostname 模块
功能:管理主机名
范例:
ansible all -m hostname -a "name=websrv"
ansible self -m hostname -a 'name=www.test.com'

#1.12 Cron 模块
功能:计划任务
支持时间:minute,hour,day,month,weekday
范例:
#备份数据库脚本
[root@web1 ~]# cat mysql_backup.sh 
#!/bin/bash
mysqldump -A -F --single-transaction --master-data=2 -q -uroot |gzip > /data/mysql_`date +%F_%T`.sql.gz

#创建任务
ansible 172.31.5.6 -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql" job=/root/mysql_backup.sh'
ansible websrvs   -m cron -a "minute=*/5 job='/usr/sbin/ntpdate ntp.aliyun.com &>/dev/null' name=Synctime"
#禁用计划任务
ansible websrvs   -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 210.72.145.44  &>/dev/null' name=Synctime disabled=yes"
#启用计划任务
ansible websrvs   -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 210.72.145.44  &>/dev/null' name=Synctime disabled=no"
#删除任务
ansible websrvs -m cron -a "name='backup mysql' state=absent"
ansible websrvs -m cron -a 'state=absent name=Synctime'

#1.13 Yum 和 Apt 模块
功能:
yum 管理软件包,只支持RHEL,CentOS,fedora,不支持Ubuntu其它版本
apt 模块管理 Debian 相关版本的软件包
范例:
ansible websrvs -m yum -a 'name=httpd state=present'  #安装
ansible websrvs -m yum -a 'name=nginx state=present enablerepo=epel'  #启用epel源
进行安装
ansible websrvs -m yum -a 'name=* state=lastest exclude=kernel*,foo*' #升级除
kernel和foo开头以外的所有包
ansible websrvs -m yum -a 'name=httpd state=absent'   #删除
[root@ansible ~]#ansible websrvs -m yum -a 'name=sl,cowsay'
#网络安装
[root@ansible ~]#ansible websrvs -m yum -a "name=https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/5.2/rhel/7/x86_64/zabbix-agent-5.2.5-1.el7.x86_64.rpm"

#1.14 yum_repository 模块
[root@ansible ~]# cat yum_repo.yaml 
- hosts: websrvs
  tasks:
    - name: Add multiple repositories into the same file 
      yum_repository:
        name: test
        description: EPEL YUM repo
        file: external_repos
        baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
        gpgcheck: no
[root@ansible ~]# ansible-playbook yum_repo.yaml 
#验证配置结果
[root@ansible ~]# ansible websrvs -a 'cat /etc/yum.repos.d/external_repos.repo'
web02 | CHANGED | rc=0 >>
[test]
baseurl = https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
gpgcheck = 0
name = EPEL YUM repo
web01 | CHANGED | rc=0 >>
[test]
baseurl = https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
gpgcheck = 0
name = EPEL YUM repo

#取消仓库配置
[root@ansible ~]# cat remove_yum_repo.yml 
- hosts: websrvs
  tasks:
     - name: remove repo 
       yum_repository:
         name: test
         file: external_repos
         state: absent
[root@ansible ~]# ansible-playbook remove_yum_repo.yml

#1.15 模块
Service 模块
功能:管理服务
范例:
ansible all -m service -a 'name=httpd state=started enabled=yes'
ansible all -m service -a 'name=httpd state=stopped'
ansible all -m service -a 'name=httpd state=reloaded'
ansible all -m shell -a "sed -i 's/^Listen 80/Listen 8080/' /etc/httpd/conf/httpd.conf"
ansible all -m service -a 'name=httpd state=restarted'

#1.16  User 模块
功能:管理用户
范例:
#创建用户
ansible all -m user -a 'name=user1 comment="test user" uid=2048 home=/app/user1 group=root'
ansible all -m user -a 'name=nginx comment=nginx uid=88 group=nginx groups="root,daemon" shell=/sbin/nologin system=yes create_home=no home=/data/nginx non_unique=yes'
#remove=yes表示删除用户及家目录等数据,默认remove=no
ansible all -m user -a 'name=nginx state=absent remove=yes'
#生成123456加密的密码
ansible localhost -m debug -a "msg={{ '123456'| password_hash('sha512','salt')}}"
localhost | SUCCESS => {
    "msg": "$6$salt$MktMKPZJ6t59GfxcJU20DwcwQzfMvOlHFVZiOVD71w."
}
#用上面创建的密码创建用户
ansible websrvs -m user -a 'name=test 
password="$6$salt$MktMKPZJ6t59GfxcJU20DwcwQzfMvOlHFVZiOVD71w."'
#创建用户test,并生成4096bit的私钥
ansible websrvs -m user -a 'name=test generate_ssh_key=yes ssh_key_bits=4096 ssh_key_file=.ssh/id_rsa'

#1.17  Group 模块
功能:管理组
范例:
#创建组
ansible websrvs -m group  -a 'name=nginx gid=88 system=yes'
#删除组
ansible websrvs -m group  -a 'name=nginx state=absent'

#1.18 Lineinfile 模块
ansible在使用sed进行替换时,经常会遇到需要转义的问题,而且ansible在遇到特殊符号进行替换时,存在问题,无法正常进行替换 。其实在ansible自身提供了两个模块:lineinfile模块和replace模块,可以方便的进行替换
一般在ansible当中去修改某个文件的单行进行替换的时候需要使用lineinfile模块regexp参数 :使用正则表达式匹配对应的行,当替换文本时,如果有多行文本都能被匹配,则只有最后面被匹配到的那行文本才会被替换,当删除文本时,如果有多行文本都能被匹配,这么这些行都会被
删除。如果想进行多行匹配进行替换需要使用replace模块
功能:相当于sed,可以修改文件内容
范例:
ansible websrvs -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='^Listen' line='Listen 80'"
ansible all -m   lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=disabled'"
ansible all -m lineinfile  -a 'dest=/etc/fstab state=absent regexp="^#"'

#1.19 Replace 模块
该模块有点类似于sed命令,主要也是基于正则进行匹配和替换,建议使用
范例:
ansible all -m replace -a "path=/etc/fstab regexp='^(UUID.*)' replace='#\1'"  
ansible all -m replace -a "path=/etc/fstab regexp='^#(UUID.*)' replace='\1'"

#1.20 SELinux 模块
该模块管理 SELInux 策略
范例: 
#禁用ansible主机的selinux
[root@ansible ~]# ansible self -m selinux -a 'state=disabled'
172.31.5.5 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "configfile": "/etc/selinux/config", 
    "msg": "", 
    "policy": "targeted", 
    "reboot_required": false, 
    "state": "disabled"
}
#验证结果
[root@ansible ~]# grep -v '#' /etc/selinux/config 
SELINUX=disabled
SELINUXTYPE=targeted 

[root@ansible ~]# getenforce 
Disabled

#1.21 reboot模块
范例:
[root@ansible ~]#ansible websrvs -m reboot

#1.22 mount 挂载和卸载
功能: 挂载和卸载文件系统
范例:
#临时挂载
mount websrvs -m mount -a 'src="UUID=b3e48f45-f933-4c8e-a700-22a159ec9077" path=/home fstype=xfs opts=noatime state=present'
#临时取消挂载
mount websrvs -m mount -a 'path=/home fstype=xfs opts=noatime state=unmounted'
#永久挂载
ansible websrvs -m mount -a 'src=10.0.0.8:/data/wordpress path=/var/www/html/wp-content/uploads opts="_netdev" state=mounted'
#永久卸载
ansible websrvs -m mount -a 'src=10.0.0.8:/data/wordpress path=/var/www/html/wp-content/uploads state=absent'

#1.23
Setup 模块
功能: setup 模块来收集主机的系统信息,这些 facts 信息可以直接以变量的形式使用,但是如果主机
较多,会影响执行速度
可以使用 gather_facts: no 来禁止 Ansible 收集 facts 信息
范例:
ansible all -m setup
ansible all -m setup -a "filter=ansible_nodename"
ansible all -m setup -a "filter=ansible_hostname"
ansible all -m setup -a "filter=ansible_domain"
ansible all -m setup -a "filter=ansible_memtotal_mb"
ansible all -m setup -a "filter=ansible_memory_mb"
ansible all -m setup -a "filter=ansible_memfree_mb"
ansible all -m setup -a "filter=ansible_os_family"
ansible all -m setup -a "filter=ansible_distribution_major_version"
ansible all -m setup -a "filter=ansible_distribution_version"
ansible all -m setup -a "filter=ansible_processor_vcpus"
ansible all -m setup -a "filter=ansible_all_ipv4_addresses"
ansible all -m setup -a "filter=ansible_architecture"
ansible all -m setup -a "filter=ansible_uptime_seconds"
ansible all -m setup -a "filter=ansible_processor*"
ansible all -m setup -a 'filter=ansible_env'

#1.24 debug 模块
此模块可以用于输出信息,并且通过 msg 定制输出的信息内容
注意: msg后面的变量有时需要加 " " 引起来
范例: debug 模块默认输出Hello world

[root@ansible ~]# ansible self -m debug
172.31.5.5 | SUCCESS => {
    "msg": "Hello world!"
}
#准备剧本
[root@ansible ~]# cat debug.yml 
---
- hosts: websrvs
  tasks: 
    - name: output Hello world
      debug:

#执行剧本
[root@ansible ~]# ansible-playbook debug.yml 

PLAY [websrvs] ******************************************************************************************

TASK [Gathering Facts] **********************************************************************************
ok: [web02]
ok: [web01]

TASK [output Hello world] *******************************************************************************
ok: [web01] => {
    "msg": "Hello world!"
}
ok: [web02] => {
    "msg": "Hello world!"
}

PLAY RECAP **********************************************************************************************
web01                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
web02                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

范例: 利用debug 模块输出变量
[root@ansible ~]# cat debug.yml 
---
- hosts: websrvs
  tasks:
    - name: output variables
      debug:
        msg: Host "{{ ansible_nodename }}" Ip  "{{ ansible_default_ipv4.address }}"

[root@ansible ~]# ansible-playbook debug.yml

2、ansible-playbook实现MySQL的二进制部署

![image](https://www.icode9.com/i/l/?n=22&i=blog/2798441/202206/2798441-20220604153033106-1514019010.png)
- playbook 剧本是由一个或多个"play"组成的列表
- play的主要功能在于将预定义的一组主机,装扮成事先通过ansible中的task定义好的角色。Task实
- 际是调用ansible的一个module,将多个play组织在一个playbook中,即可以让它们联合起来,按
- 事先编排的机制执行预定义的动作
- Playbook 文件是采用YAML语言编写的

#准备mysql配置文件
[root@ansible ~]# cat /data/my.cnf 
[mysqld]
socket=/tmp/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
log-bin
pid-file=/data/mysql/mysqld.pid
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log

#准备安装剧本
[root@ansible ~]# cat install_mysql.yml 
---
#install mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
- hosts: websrvs
  remote_user: root
  gather_facts: no
  tasks:
   - name: install packages
     yum: name=libaio,perl-Data-Dumper,perl-Getopt-Long
   - name: create mysql group
     group: name=mysql gid=306
   - name: create mysql user
     user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
   - name: download mysql.tar.gz
     get_url: url='http://ftp.iij.ad.jp/pub/db/mysql/Downloads/MySQL-5.6/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz' dest=/data/
   - name: copy tar to remote host and file mode 
     unarchive: src=/data/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz dest=/usr/local/ owner=root group=root 
   - name: create linkfile /usr/local/mysql 
     file: src=/usr/local/mysql-5.6.46-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
   - name: data dir
     shell: chdir=/usr/local/mysql/ ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
     tags: data
   - name: config my.cnf
     copy: src=/data/my.cnf  dest=/etc/my.cnf 
   - name: service script
     shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
   - name: enable service
     shell: /etc/init.d/mysqld start;chkconfig --add mysqld;chkconfig mysqld on 
     tags: service
   - name: PATH variable
     copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh

#执行安装
[root@ansible ~]# ansible-playbook install_mysql.yml

3、Ansible playbook实现apache批量部署,并对不同主机提供以各自IP地址为内容的index.html

#准备安装剧本
[root@ansible ~]# cat install_httpd.yml 
- hosts: websrvs
  remote_user: root
  gather_facts: no
  tasks:
    - name: Instal1 httpd
      yum: name=httpd
    - name: Modify config list port
      lineinfile: 
        path: /etc/httpd/conf/httpd.conf 
        regexp: '^Listen'
        line: 'Listen 8080'
    - name: Modify config data1
      lineinfile: 
        path: /etc/httpd/conf/httpd.conf 
        regexp: '^DocumentRoot "/var/www/html"'
        line: 'DocumentRoot "/data/html"'
    - name: Modify config data2
      lineinfile: 
        path: /etc/httpd/conf/httpd.conf 
        regexp: '^<Directory "/var/www/html">'
        line: '<Directory "/data/html">'
    - name: Mkdir website dir
      file: path=/data/html state=directory
    - name: Web html
      shell: echo $(hostname -I) > /data/html/index.html
    - name: Start service
      service: name=httpd state=started enabled=yes

#执行安装
[root@ansible ~]# ansible-playbook install_httpd.yml
#验证执行结果
[root@ansible ~]# ansible  websrvs -a 'curl -s localhost:8080' 
web02 | CHANGED | rc=0 >>
172.31.5.7
web01 | CHANGED | rc=0 >>
172.31.5.6

4、http的报文结构和状态码总结
转载于 https://www.cnblogs.com/myseries/p/11239662.html
image

  • 请求报文结构
    image

  • request报文格式

    <method> <request-URL> <version>
    <headers>
    <entity-body>
    
  • 范例:

    GET / HTTP/1.1
    Accept: */*
    Accept-Encoding: gzip, deflate
    Connection: keep-alive
    Host: www.magedu.com
    User-Agent: HTTPie/0.9.4
    

image

  • 响应码
    状态码:由3位数字组成,第一个数字定义了响应的类别
    
    1xx:指示信息,表示请求已接收,继续处理
    2xx:成功,表示请求已被成功接受,处理。
    200 OK:客户端请求成功
    204 No Content:无内容。服务器成功处理,但未返回内容。一般用在只是客户端向服务器发送信息,而服务器不用向客户端返回什么信息的情况。不会刷新页面。
    206 Partial Content:服务器已经完成了部分GET请求(客户端进行了范围请求)。响应报文中包含Content-Range指定范围的实体内容
    3xx:重定向
    301 Moved Permanently:永久重定向,表示请求的资源已经永久的搬到了其他位置。
    
    302 Found:临时重定向,表示请求的资源临时搬到了其他位置
    
    303 See Other:临时重定向,应使用GET定向获取请求资源。303功能与302一样,区别只是303明确客户端应该使用GET访问
    
    307 Temporary Redirect:临时重定向,和302有着相同含义。POST不会变成GET
    
    304 Not Modified:表示客户端发送附带条件的请求(GET方法请求报文中的IF…)时,条件不满足。返回304时,不包含任何响应主体。虽然304被划分在3XX,但和重定向一毛钱关系都没有
    一个304的使用场景:
    缓存服务器向服务器请求某一个资源的时候,服务器返回的响应报文具有这样的字段:Last-Modified:Wed,7 Sep 2011 09:23:24,缓存器会保存这个资源的同时,保存它的最后修改时间。下次用户向缓存器请求这个资源的时候,缓存器需要确定这个资源是新的,那么它会向原始服务器发送一个HTTP请求(GET方法),并在请求头部中包含了一个字段:If-Modified-Since:Wed,7 Sep 2011 09:23:24,这个值就是上次服务器发送的响应报文中的最后修改时间。
    
    假设这个资源没有被修改,那么服务器返回一个响应报文:
    
    
    HTTP/1.1 304 Not Modified
    	Date:Sat, 15 Oct 2011 15:39:29
    	(空行)                                      
    	(空响应体)
    用304告诉缓存器资源没有被修改,并且响应体是空的,不会浪费带宽。
    
    4xx:客户端错误
    400 Bad Request:客户端请求有语法错误,服务器无法理解。
    401 Unauthorized:请求未经授权,这个状态代码必须和WWW-Authenticate报头域一起使用。
    403 Forbidden:服务器收到请求,但是拒绝提供服务
    404 Not Found:请求资源不存在。比如,输入了错误的url
    415 Unsupported media type:不支持的媒体类型
    5xx:服务器端错误,服务器未能实现合法的请求。
    500 Internal Server Error:服务器发生不可预期的错误。
    503 Server Unavailable:服务器当前不能处理客户端的请求,一段时间后可能恢复正常
    

标签:http,name,websrvs,data,简述,ansible,mysql,root
来源: https://www.cnblogs.com/peen/p/16314339.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有