1号机#
#桥接网络例如ip:192.168.1.10
#ifdown ens33;ifup ens33
#systemctl stop firewalld
#setenforce 0
#yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
#yum -y install ansible
#ansible --version
#vim /etc/hosts
添加:192.168.1.1 node1
192.168.1.2 node2
192.168.1.3 node3
保存退出
#ssh-keygen -t rsa 创建秘钥对,进行非密码验证
#ssh-copy-id root@192.168.1.1
#vim /etc/ansible/hosts
最下端添加:[web]
192.168.1.1
192.168.1.2
192.168.1.3
保存退出
使用清单测试ping命令
#ansible -i /etc/ansible/hosts web -m ping 或#ansible web -m ping
2号机#
#桥接网络
#ifdown ens33;ifup ens33
#systemctl stop firewalld
#setenforce 0
#mount /dev/cdrom /mnt
#cd /etc/yum.repos.d/
#rm -rf *
#vim a.repo
[a]
baseurl=file:///mnt
gpgcheck=0 (esc:wq)
#yum clean all
#yum -y install httpd
#systemctl start httpd
3号机# (同步2号机步骤)
1号机#
通过命令查看httpd服务状态(前提:node1、node2安装httpd)
(1)管理单台主机
ansible web -m command -a "systemctl status httpd" --limit "node1"
(2)管理清单内主机组
ansible web -m command -a "systemctl status httpd"
9.命令ping清单所有主机
ansible all -f 5 -m ping
10.列出所有主机
ansible web --list
11.查看web清单主机磁盘使用情况
ansible web -m command -a "df -hT"
12.ansible的模块
(1)command模块
使用命令关闭selinux和防火墙
ansible web -m command -a "setenforce 0"
ansible web -m command -a "systemctl stop firewalld"
使用chdir切换目录
ansible web -m command -a "chdir=/tmp ls ./"
(2)shell模块:通过调用被管理主机的shell运行命令,支持管道和重定向
使用echo 和 > 创建文件
ansible web -m shell -a 'echo "hello" >> /tmp/hello.txt'
(3)copy模块:复制文件到远程主机
主要参数:dest 目标位置
src 源文件路径
mode 目标权限
owner 目标文件属主
group 目标文件属组
content 可选内容复制,不能与src共用
复制本机/etc/hosts到远程主机/tmp,权限777
ansible web -m copy -a "src=/etc/hosts dest=/tmp mode=777 owner=nobody group=root"
(4)hostname模块:管理远程主机主机名,把node3主机名改为demo
ansible node3 -m hostname -a "name=demo"
(5)yum模块:对远程主机,管理yum程序
参数:
name:程序包名称
state=present|latest|absent:present表示安装、latest表示安装最新版本、absent表示卸载
disablerepo:临时禁用某个yum仓库
enablerepo:临时启用仓库
conf_file:yum运行时临时启用的配置文件,不是默认文件
diable_gpg_check=yes|no:是否启用完整性校验
使用yum模块,远程安装node1的httpd服务
ansible node1 -m yum -a "name=httpd state=present"
(6)service模块:管理远程主机服务
name:服务名
state=started|stoped|restarted:启动|关闭|重启
enabled=yes|no:服务是否开机自启动
runlevel:服务在那个级别开机自启动
设置node1的httpd服务开机自动启动
(7)user模块:管理远程主机的用户帐号
name:账号名
state=present|absent:present表示创建、absent表示删除
system=yes|no:是否为系统账号
uid:用户uid
group:基本组
groups:附加组
shell:默认shell
home:宿主目录
move_home=yes|no:如果宿主目录已存在,是否移动宿主目录
password:密码
comment:注释
remove=yes|no:删除用户时,是否删除宿主目录
所有web组主机创建用户user1,密码user1
ansible web -m user -a "name=user1 system=yes uid=666 group=root groups=sshd shell=/sbin/nologin home=/home/user1 password=user1"
删除web组主机user1用户
ansible web -m user -a "name=user1 remove=yes state=absent"
13.playbook配置文件
(1)执行配置 文件:使用yaml语法,简洁明了,结构清晰,类似shell脚本,可以完成command命令配置不了的复杂任务,注意语法格式必须对齐,以“---”开头,以“...”结束。
注意:所有 “-”和“:”后面都有空格
hosts:任务的目标主机
remote_user:远程主机执行任务的用户,默认root
tasks:任务
handlers:触发器,只有满足条件才会触发执行
roles:角色,由tasks和handlers等组成的特殊结构集合
案例:在web1上创建用户user2和组cwb,在web2上复制本机/etc/passwd到web2的/home目录
先修改本机/etc/ansible/hosts
vim /etc/ansible/hosts
添加:
[web1]
node1
[web2]
node2
保存退出
再创建yaml文件
vim /etc/ansible/a.yml
添加:
---
- hosts: web1
remote_user: root
tasks:
- name: adduser
user: name=user2 state=present
tags:
- aaa
- name: addgroup
group: name=cwb system=yes
tags:
- bbb
- hosts: web2
remote_user: root
tasks:
- name: copy file to web
copy: src=/etc/passwd dest=/home
tags:
- ccc
...
保存退出
执行语法检查命令:检查语法格式,很重要
ansible-playbook --syntax-check /etc/ansible/a.yml
预测试:测试执行,不写入结果
ansible-playbook -C /etc/ansible/a.yml
列出主机:
ansible-playbook --list-hosts /etc/ansible/a.yml
列出任务:
ansible-playbook --list-tasks /etc/ansible/a.yml
列出标签:
ansible-playbook --list-tags /etc/ansible/a.yml
执行任务:
ansible-playbook /etc/ansible/a.yml
(2)触发器:需要满足触发条件才能执行,下面修改node1的httpd端口为8080
在ansible主机查看node1的httpd端口
ssh node1 netstat -lnupt | grep 80
创建yml配置文件(注意:此处书上格式有错误)
vim /etc/ansible/httpd.yml
添加:
---
- hosts: web1
remote_user: root
tasks:
- name: change port
command: sed -i 's/Listen\ 80/Listen\ 8080/g' /etc/httpd/conf/httpd.conf
notify:
- restart httpd server
handlers:
- name: restart httpd server
service: name=httpd state=restarted
...
保存退出
执行配置文件
ansible-playbook /etc/ansible/httpd.yml
(3)角色:存放不同tasks文件的目录,一般在/etc/ansible/roles下
例如:Mariadb 数据库角色、Apache 网站角色
角色的目录结构:
files:存放copy和script调用的文件
templates:存放templates模块所需要的模板文件
tasks:任务目录
handlers:触发器目录
vars:变量目录
meta:元数据目录
default:默认变量目录
案例:配置远程主机数据库
创建角色数据库
mkdir -p /etc/ansible/roles/mariadb/{files,tasks,handlers}
编写主任务文件
cd /etc/ansible/roles/mariadb/tasks/
vim main.yml
添加:
---
- name: install mariadb
yum: name=mariadb-server state=present
- name: move config file
shell: "[ -e /etc/my.cnf ] && mv /etc/my.cnf /etc/my.cnf.bak"
- name: provide a new config file
copy: src=my.cnf dest=/etc/my.cnf
- name: reload mariadb
shell: systemctl restart mariadb
- name: create database testdb
shell: mysql -u root -e "create database testdb;grant all on testdb.* to 'test'@'192.168.1.%' identified by 'test123';flush privileges;"
notify:
- restart mariadb
...
保存退出
编写触发器任务文件
cd /etc/ansible/roles/mariadb/handlers/
vim main.yml
添加:
---
- name: restart mariadb
service: name=mariadb state=restarted
...
保存退出
准备数据库配置文件(现实环境提前在测试机上安装mariadb,修改完配置文件,复制到这里)
cd /etc/ansible/roles/mariadb/files
复制my.cnf到此目录
编写mariadb.yml
vim /etc/ansible/mariadb.yml
添加:
---
- hosts: web1
remote_user: root
roles:
- mariadb
...
保存退出
执行mariadb.yml文件
ansible-playbook /etc/ansible/mariadb.yml