ansible-server,IP地址:192.168.0.24
web1,IP地址:192.168.0.27 web2,IP地址:192.168.0.28
[root@ansible-server ~]# systemctl stop firewalld
[root@ansible-server ~]# systemctl disable firewalld
[root@ansible-server ~]# setenforce 0
[root@ansible-server ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@ansible-server ~]# vim /etc/hosts
...
192.168.0.26 web1
192.168.0.27 web2
192.168.0.28 web3
[root@ansible-server ~]# ssh-keygen
[root@ansible-server ~]# for i in web1 web2 web3
> do
> ssh-copy-id $i
> done
[root@ansible-server ~]# ssh web1
[root@web1 ~]# exit
[root@ansible-server ~]# ssh web2
[root@web2 ~]# exit
[root@ansible-server ~]# ssh web3
[root@web3 ~]# exit
[root@ansible-server ~]# yum -y install wget #下载wget工具
[root@ansible-server ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo #下载阿里Base源
[root@ansible-server ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo #下载阿里epel源
[root@ansible-server ~]# yum -y install ansible
[root@ansible-server ~]# ansible --version
ansible 2.9.17
[root@ansible-server ~]# vim /etc/ansible/hosts
[websrvs]
192.168.0.26
192.168.0.27
192.168.0.28
[root@ansible-server ~]# ansible websrvs --list-host
hosts (4):
192.168.0.26
192.168.0.27
192.168.0.28
-l #列出所有模块列表 -s #查看模块帮助信息
[root@ansible-server ~]# ansible-doc -l
[root@ansible-server ~]# ansible-doc -s ping
[root@ansible-server ~]# ansible websrvs -m ping
#chdir参数表示执行命令之前,会先进入到指定的目录中
[root@ansible-server ~]# ansible websrvs -m command -a 'chdir=/root/ ls'
#creates参数表示如果/etc/passwd文件存在于远程主机中,则不执行对应命令,如果不存在,才执行”touch”命令
[root@ansible-server ~]# ansible websrvs -m command -a 'creates=/etc/passwd touch /opt/passwd'
#演示:creates参数在文件不存在情况下才会执行“touch”命令
[root@ansible-server ~]# ansible websrvs -m command -a 'creates=/opt/passwd touch /opt/passwd'
#removes参数表示如果/opt/abc文件不存在,就不执行“mv”命令,如果文件存在则执行“mv”命令
[root@ansible-server ~]# ansible websrvs -m command -a 'removes=/opt/abc mv /opt/abc /root/'
#演示:removes参数如果/opt/passwd文件不存在,就不执行“mv”命令
[root@ansible-server ~]# ansible websrvs -m command -a 'removes=/opt/passwd mv /opt/passwd /root/'
[root@ansible-server ~]# ansible websrvs -m command -a 'hostname'
[root@ansible-server ~]# ansible websrvs -m command -a 'ip a s ens32'
[root@ansible-server ~]# ansible websrvs -a 'uname -r' #查看内核信息
[root@ansible-server ~]# ansible websrvs -a 'free -h' #查看内存信息
提示: shell模块中chdir、creates、removes参数的作用与command模块中的作用都是相同的,此处不再举例
#查看/etc/passwd文件,并通过管道符传递给“wc -l”统计文件行数
[root@ansible-server ~]# ansible websrvs -m shell -a 'cat /etc/passwd | wc -l'
#查看/etc/passwd文件,并通过“>”将文件内容重定向至/opt/passwd文件中
[root@ansible-server ~]# ansible websrvs -m shell -a 'cat /etc/passwd > /opt/passwd'
#通过chdir参数进入/opt/目录,执行“ls”
[root@ansible-server ~]# ansible websrvs -m shell -a 'chdir=/opt/ ls'
#通过chdir参数进入/opt目录,执行“cat”查看passwd文件内容
[root@ansible-server ~]# ansible websrvs -m shell -a 'chdir=/opt/ cat passwd'
#查看主机名
[root@ansible-server ~]# ansible websrvs -m shell -a 'hostname'
#查看内核信息
[root@ansible-server ~]# ansible websrvs -m shell -a 'uname -r'
#查看ens32网卡的IP地址信息
[root@ansible-server ~]# ansible websrvs -m shell -a 'ip a s ens32 | grep inet'
#查看内存使用信息
[root@ansible-server ~]# ansible websrvs -m shell -a 'free -h'
#查看分区使用信息
[root@ansible-server ~]# ansible websrvs -m shell -a 'df -h'
#关闭防火墙&&设置开机不自起
[root@ansible-server ~]# ansible websrvs -m shell -a 'systemctl stop firewalld && systemctl disable firewalld'
1.Ansible是什么?
2.Ansible的特点是什么?
3.Ansible核心组件都有哪些?
4.Ansible基于什么协议管理被控节点?
5.在Ansible清单文件中,定义被控主机的方式有哪些?
6.如何查看Ansible模块的用法?
7.在Ansible的命令返回值中,绿色代表什么含义?
8.在Ansible的命令返回值中,黄色代表什么含义?
9.在Ansible的命令返回值中,红色代表什么含义?
10.Ansible执行命令的方式有哪些?
提示: shell模块中chdir、creates、removes参数的作用与command模块中的作用都是相同的,此处不再举例
1.mkdri /mnt/centos
2.mount /dev/cdrom /mnt/centos
3.rm -rf /etc/yum.repos.d/*
4.vim /etc/yum.repos.d/local.repo
[local_file]
name=loal_file
baseurl=fiel:///mnt/centos
enabled=1
gpgcheck=0
#编写搭建yum仓库脚本
[root@ansible-server ~]# vim yum.sh
#!/bin/bash
mkdir /mnt/centos
mount /dev/cdrom /mnt/centos
echo "/dev/cdrom /mnt/centos iso9660 defaults 0 0" >> /etc/fstab
rm -rf /etc/yum.repos.d/*
echo "[local_centos7]
name=local_centos7
baseurl=file:///mnt/centos
enabled=1
gpgcheck=0" > /etc/yum.repos.d/centos.repo
yum repolist
#使用script模块执行此脚本
[root@ansible-server ~]# ansible websrvs -m script -a '/root/yum.sh'
yum模块示例:
#使用yum模块安装vsftpd软件包(本地yum源默认已经禁用软件包检测)
[root@ansible-server ~]# ansible websrvs -m yum -a 'name=vsftpd'
#卸载vsftpd软件包
[root@ansible-server ~]# ansible websrvs -m yum -a 'name=vsftpd state=removed'
#ftp组内主机安装vsftpd服务
[root@ansible-server ~]# ansible websrvs -m yum -a 'name=vsftpd'
service 模块,用于管理远程主机的服务,如:启动或停止服务
常用参数:
- restarted:此状态用于重启服务
#重启服务
[root@ansible-server ~]# ansible ftp -m service -a 'name=vsftpd state=restarted'
- stopped:此状态用于停止服务
#停止vsftpd服务
[root@ansible-server ~]# ansible ftp -m service -a 'name=vsftpd state=stopped'
enabled:此参数 用于指定是否将服务设置为开机启动项,设置为yes表示将对应服务设置为开机启动,设置为no表示不会开机启动。
copy 模块,用于将文件复制到远程主机
常用参数:
#在ansible本地主机创建文件
[root@ansible-server ~]# touch /tmp/test.txt
#将本地/tmp/test.txt文件拷贝至ftp组内主机的/var/ftp目录下
[root@ansible-server ~]# ansible ftp -m copy -a 'src=/tmp/test.txt dest=/var/ftp/'
[root@ansible-server ~]# ansible ftp -m shell -a 'ls /var/ftp'
content:此参数当不使用src指定拷贝的文件时,可以使用content直接指定文件内容,src与content两个参数必有其一,否则会报错
force:此参数当远程主机的目标路径中已经存在同名文件,并且与ansible主机中的文件内容不同时,是否强制覆盖,可选值有yes和no,默认值为yes,表示覆盖,如果设置为no,则不会执行覆盖拷贝操作,远程主机中的文件保持不变
backup:此参数当远程主机的目标路径中已经存在同名文件,并且与ansible主机中的文件内容不同时,是否对远程主机的文件进行备份,可选值有yes和no,当设置为yes时,会先备份远程主机中的文件,然后再将ansible主机中的文件拷贝到远程主机
owner:此参数指定文件拷贝到远程主机后的属主,但是远程主机上必须有对应的用户,否则会报错。
group:此参数指定文件拷贝到远程主机后的属组,但是远程主机上必须有对应的组,否则会报错
mode:此参数指定文件拷贝到远程主机后的权限,如果你想将权限设置为”rw-r–r–“,则可以使用mode=0644表示
#使用ping模块测试远程主机联通性,并在远程主机创建目录
[root@ansible-server ~]# vim ping_websrvs.yml
---
- hosts: websrvs
remote_user: root
tasks:
- name: ping websrvs
ping:
- name: mkdir directory test
shell:
cmd: mkdir /test
#第一行:playbook剧本以---开头表明yaml格式文件
#第二行:使用”- “作为开头,”- “表示一个列表项,”- “后面使用hosts关键字指定要操作的主机组名,(注意:横杠后面有空格)表示我要在websrvs这组主机上进行操作,在YAML语法中,键值对需要使用冒号作为分隔,而且冒号后边必须还要有一个空格作为分隔
#第三行:remote_user关键字可以指定在进行远程操作时使用哪个用户进行操作
#第四行:tasks关键字是用来指定要执行哪些操作任务,之后的行都属于tasks任务列表中的任务,每个任务都以”- “开头,每个任务都有自己的名字,任务名使用name关键字进行指定
#运行剧本需要使用’ansible-playbook’命令
[root@ansible-server ~]# ansible-playbook ping_websrvs.yml
TASK [Gathering Facts] ****************************************************************
ok: [192.168.0.28]
ok: [192.168.0.27]
ok: [192.168.0.26]
TASK [ping websrvs] ****************************************************************
ok: [192.168.0.26]
ok: [192.168.0.27]
ok: [192.168.0.28]
TASK [mkdir directory test] ****************************************************************
changed: [192.168.0.28]
changed: [192.168.0.26]
changed: [192.168.0.27]
PLAY RECAP ******************************************************************
192.168.0.26 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.0.27 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.0.28 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansible-server ~]# vim nginx.yml
---
- hosts: websrvs
remote_user: root
tasks:
- name: Copy nginx
copy: src=/root/nginx-1.18.0.tar.gz dest=/tmp
- name: install Rely
yum: name=gcc,pcre-devel,openssl-devel,zlib
- name: unpacek nginx
shell: tar -xf /tmp/nginx-1.18.0.tar.gz -C /tmp
- name: install nginx
shell: cd /tmp/nginx-1.18.0 && ./configure && make && make install
- name: Copy index.html
copy: src=/root/index.html dest=/usr/local/nginx/html
- name: start nginx
shell: /usr/local/nginx/sbin/nginx
#--syntax-check 用于检测playbook语法是否正确,如果正确只返回playbook名称
[root@ansible-server ~]# ansible-playbook --syntax-check nginx.yml
playbook: nginx.yml
#--check 用于模拟执行playbook
[root@ansible-server ~]# ansible-playbook --check nginx.yml
PLAY [websrvs] ****************************************************
TASK [Gathering Facts] *******************************************************************
ok: [192.168.0.28]
ok: [192.168.0.26]
ok: [192.168.0.27]
TASK [Copy nginx] *************************************************
ok: [192.168.0.28]
ok: [192.168.0.27]
ok: [192.168.0.26]
TASK [install Rely] ***********************************************
ok: [192.168.0.26]
ok: [192.168.0.27]
ok: [192.168.0.28]
TASK [unpacek nginx] **********************************************
skipping: [192.168.0.26]
skipping: [192.168.0.28]
skipping: [192.168.0.27]
TASK [install nginx] ******************************************************************
skipping: [192.168.0.26]
skipping: [192.168.0.27]
skipping: [192.168.0.28]
TASK [Copy index.html] ******************************************************************
ok: [192.168.0.27]
ok: [192.168.0.26]
ok: [192.168.0.28]
TASK [start nginx] ******************************************************************
skipping: [192.168.0.26]
skipping: [192.168.0.27]
skipping: [192.168.0.28]
PLAY RECAP ******************************************************************
192.168.0.26 : ok=4 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
192.168.0.27 : ok=4 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
192.168.0.28 : ok=4 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
#使用playbook剧本批量在远程主机源码安装nginx
[root@ansible-server ~]# ansible-playbook nginx.yml
PLAY [websrvs] ******************************************************************
TASK [Gathering Facts] ******************************************************************
ok: [192.168.0.28]
ok: [192.168.0.27]
ok: [192.168.0.26]
TASK [Copy nginx] ******************************************************************
changed: [192.168.0.28]
changed: [192.168.0.27]
changed: [192.168.0.26]
TASK [install Rely] ******************************************************************
ok: [192.168.0.26]
ok: [192.168.0.27]
ok: [192.168.0.28]
TASK [unpacek nginx] ******************************************************************
changed: [192.168.0.28]
changed: [192.168.0.26]
changed: [192.168.0.27]
TASK [install nginx] *******************************************************************
changed: [192.168.0.26]
changed: [192.168.0.28]
changed: [192.168.0.27]
TASK [Copy index.html] *******************************************************************
changed: [192.168.0.27]
changed: [192.168.0.28]
changed: [192.168.0.26]
TASK [start nginx] ********************************************************************
changed: [192.168.0.27]
changed: [192.168.0.28]
changed: [192.168.0.26]
PLAY RECAP ********************************************************************
192.168.0.26 : ok=7 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.0.27 : ok=7 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.0.28 : ok=7 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0