ansible理论
ansible:基于Python开发的管理配置工具
ansible主机将任务分发到远程主机,主机之间通过SSH协议通信。ssh(安全壳通信,端口:22)
ansible框架
- ansible:框架的核心
- 连接插件:ssh,用于实现ansible主机与远程主机之间的通信
- 核心模块:core modules,用来提供ansible主机多远程主机分配的任务,集成ansible程序中
- 扩展模块:consume module,要分配的任务核心模块不支持时,可以安装第三方模块,也就是扩展模块
- 主机列表:用来记录被分配任务的主机标识(IP/域名/主机名)
- 插件:plugin、日志、mail
- playbook:剧本
常用模块
ansible remote_host -m module -a job
- ansible:关键字
- remote_host:被分发任务的远程主机,该主机必须被记录在/etc/ansible/hosts文件中,否则主机无法识别,可以是主机的IP/域名/主机名,也可以是组名
- -m:指定要使用的模块
- -a:有的模块自带参数,需要额外指定要做的操作
ansible webserver -m ping
- ping:用来测试ansible主机与远程主机之间的连通性
在远程主机上执行命令,不支持特殊字符
ansible的安装与配置
实验环境
关闭防火墙、关闭沙盒
角色 | Server | Client | Client |
IP地址 | 192.168.1.11 | 192.168.1.22 | 192.168.1.33 |
安装ansible
[root@localhost ~]# yum -y install ansible
SSH免密登录
[root@localhost ~]# ssh-keygen
[root@localhost ~]# ssh-copy-id root@192.168.1.22
[root@localhost ~]# ssh-copy-id root@192.168.1.33
配置主机列表
[root@localhost ~]# vim /etc/ansible/hosts
[webservers] # 主机组名,可以自定义,部署任务时指定该名字就代表该组中的主机101和102
192.168.1.22 # 远程主机的IP/域名/主机名,可以同时处于多个组中
192.168.1.33
[dbservers]
192.168.1.22
常用模块
ping
# ping:连通性测试
[root@localhost ~]# ansible webservers -m ping
192.168.1.22 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.1.33 | SUCCESS => {
"changed": false,
"ping": "pong"
}
command
# touch:创建/test
[root@localhost ~]# ansible webservers -m command -a "touch /test"
[WARNING]: Consider using file module with state=touch rather than running touch
192.168.1.22 | SUCCESS | rc=0 >>
192.168.1.33 | SUCCESS | rc=0 >>
# 查询/test权限
[root@localhost ~]# ansible webservers -m command -a "ls -l /test"
192.168.1.33 | SUCCESS | rc=0 >>
-rw-r--r--. 1 root root 0 7月 31 14:56 /test
192.168.1.22 | SUCCESS | rc=0 >>
-rw-r--r--. 1 root root 0 7月 31 14:56 /test
chdir
# 改变/切换工作目录
[root@localhost ~]# ansible webservers -m command -a "chdir=/usr/src touch shuaiguoer.html"
[WARNING]: Consider using file module with state=touch rather than running touch
192.168.1.22 | SUCCESS | rc=0 >>
192.168.1.33 | SUCCESS | rc=0 >>
creates、removes
# create:指定的文件如果存在,命令不执行,如果指定的文件不存在,则执行命令
# removes:指定的文件如果存在,命令执行,如果指定的文件不存在,则不执行命令
[root@localhost ~]# ansible webservers -m command -a "creates=/test touch xixi"
192.168.1.22 | SUCCESS | rc=0 >>
skipped, since /test exists
192.168.1.33 | SUCCESS | rc=0 >>
skipped, since /test exists
[root@localhost ~]# ansible webservers -m command -a "creates=/wobucunzai touch xixi"
[WARNING]: Consider using file module with state=touch rather than running touch
192.168.1.33 | SUCCESS | rc=0 >>
192.168.1.22 | SUCCESS | rc=0 >>
shell
[root@localhost ~]# ansible webservers -m shell -a "echo www.shuaiguoer.com > /test"
192.168.1.22 | SUCCESS | rc=0 >>
192.168.1.33 | SUCCESS | rc=0 >>
script
# script:在远程主机上执行ansible主机的脚本
[root@localhost ~]# vim shuai.sh
[root@localhost ~]# chmod +x shuai.sh
[root@localhost ~]# ansible webservers -m script -a "./shuai.sh"
192.168.1.22 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.1.22 closed.\r\n",
"stdout": "www.shuaiguoer.com\r\n",
"stdout_lines": [
"www.shuaiguoer.com"
]
}
192.168.1.33 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.1.33 closed.\r\n",
"stdout": "www.shuaiguoer.com\r\n",
"stdout_lines": [
"www.shuaiguoer.com"
]
}
user
用户 | UID | 权限 | 功能 |
超级用户 | 0 | 权限大 | |
程序用户 | 1~499 | 权限低 | 用来启动或者加载指定的程序,一般不允许登录到桌面或者终端 |
普通用户 | 500 | 权限高于程序用户低于超级用户 | 可以登录到桌面或者终端,并使用应用程序,但一般不可对系统的配置进行修改 |
name
# name:指定要管理的用户名字,如果指定的用户名字不存在,则创建
[root@localhost ~]# ansible webservers -m user -a "name=test"
password
password:给用户设置密码,如果指定的用户已经有密码则覆盖
# MD5加密
[root@localhost ~]# openssl passwd -1 123.com
$1$9rbrOaH3$gLxKjfTnCxUpIb8xA6VyB0
# 设置密码
[root@localhost ~]# ansible webservers -m user -a 'name=test password="$1$9rbrOaH3$gLxKjfTnCxUpIb8xA6VyB0"'
# 查看密码
[root@localhost ~]# ansible webservers -m shell -a "tail -1 /etc/shadow"
192.168.1.22 | SUCCESS | rc=0 >>
test:$1$9rbrOaH3$gLxKjfTnCxUpIb8xA6VyB0:18108:0:99999:7:::
192.168.1.33 | SUCCESS | rc=0 >>
test:$1$9rbrOaH3$gLxKjfTnCxUpIb8xA6VyB0:18108:0:99999:7:::
group
# group:修改用户所在的基本组。
[root@localhost ~]# ansible webservers -m user -a "name=test group=root"
groups
# groups:修改用户所在的附加组,需要结合append参数使用 [root@localhost ~]# ansible webservers -m user -a "name=test groups=test append=yes"
shell
# 修改用户的shell环境
[root@localhost ~]# ansible webservers -m user -a "name=test shell=/sbin/nologin"
uid
# 修改用户的UID
[root@localhost ~]# ansible webservers -m user -a "name=test uid=233"
删除用户
# stayte=absent:删除指定的用户,搭配参数remove=yes可以同时删除用户的相关信息
[root@localhost ~]# ansible webservers -m user -a "name=test state=absent remove=yes"
group
name
添加指定的组
[root@localhost ~]# ansible webservers -m group -a "name=gg gid=1006"
gid
[root@localhost ~]# ansible webservers -m group -a "name=gg gid=1006"
删除组
state=absent:删除指定的组
[root@localhost ~]# ansible webservers -m group -a "name=gg state=absent"
service
name:指定服务名称
state:
- started:开启指定的服务
- stopped:关闭指定的服务
- restarted:重启指定的服务
- reloaded:重载指定的服务
[root@localhost ~]# ansible webservers -m service -a "name=firewalld state=stopped"
file
管理、创建、删除远程主机上的文件或者目录
- path:指定要管理的文件的绝对路径,如果指定的文件不存在则创建创建时需要根据参数
state
来指定创建的文件的类型 - state=touch # 创建普通文件
- state=directory # 创建目录
- state=link # 创建软链接文件,需要和参数
src
搭配使用 - state=absent # 删除指定的文件或目录
# 创建文件
[root@localhost ~]# ansible webservers -m file -a "path=/hello state=touch"
# 创建文件夹
[root@localhost ~]# ansible webservers -m file -a "path=/hi state=directory"
# 创建软链接文件
[root@localhost ~]# ansible webservers -m file -a "path=/world src=/hello state=link"
# 修改文件权限
owner:修改指定文件/目录的属主 group:修改指定文件/目录的属组 mode:修改指定文件/目录的权限
[root@localhost ~]# ansible webservers -m file -a "path=/hello owner=test group=test mode=777"
copy
将ansible主机上的文件/目录拷贝到远程主机指定的位置
- src:源地址,也就是ansible主机要拷贝的文件路径
- dest:目标地址,将文件/目录拷贝到远程主机的指定位置 ```bash [root@localhost ansible]# ansible webservers -m copy -a "src=/aa dest=/etc/heihei" ``` ```bash [root@localhost ansible]# ansible webservers -m shell -a "cat /etc/heihei" 192.168.1.22 | SUCCESS | rc=0 >> aaa 192.168.1.33 | SUCCESS | rc=0 >> aaa ```
- content:将指定的内容写入到远程主机中指定的文件,如果指定的远程主机中的文件已经有内容,则会被覆盖 # content ```bash [root@localhost ansible]# ansible webservers -m copy -a "content='shuaiguoer.com' dest=/etc/heihei" ```
- force:值为no时,拷贝文件时,如果在远程主机已经存在该文件,并且内容不一致,则不会进行拷贝 ```bash [root@localhost ansible]# ansible webservers -m copy -a "content='shuaiguoer.com' dest=/etc/heihei force=no" ```
- backup :当拷贝的文件在远程主机存在时,会将远程主机的文件进行备份再拷贝,备份文件的名字格式为备份的时间+文件的原名字
- 拷贝文件时可以修改权限
owner:修改指定文件/目录的属主 group:修改指定文件/目录的属组 mode:修改指定文件/目录的权限
[root@localhost ~]# ansible webservers -m copy -a "src=/bb dest=/etc/heihei owner=root group=root mode=777"
yum
name:指定要安装的软件包名
state:要执行的yum动作
- installed & present :安装软件包
- remove & absent: 卸载 关闭或者删除
- latest:更新软件包
# 安装软件包
[root@localhost ~]# ansible webservers -m yum_repository -a "file=yum name=yum description='local yum' baseurl=file:///media enabled=yes gpgcheck=no"
# 挂载
[root@localhost ~]# ansible webservers -m shell -a "mount /dev/cdrom /media"
# 安装httpd服务
[root@localhost ~]# ansible webservers -m yum -a "name=httpd state=installed"
playbook
playbook:剧本,使用yuml语言
语法格式
- 严格区分大小写
- 缩进使用空格表示
- 同一层级的语句使用相同的缩进距离
- 使用#号可以表示注释
- 使用---表示文件内容的开始,使用...表示文件内容的结束
数据类型
- 数组/列表
- 属性:key:value
- 纯值/值:字符串,整数,布尔值(true,false)
使用yaml剧本编译安装nginx
下载nginx1.11.5安装包到 /root 目录下
编写nginx.yaml文件
[root@localhost ~]# vim nginx.yaml
---
- hosts: webservers
remote_user: root
tasks:
- name: copy source file
copy: src=/root/nginx-1.11.5.tar.gz dest=/root/nginx-1.11.5.tar.gz
- yum: name=gcc,gcc-c++,zlib-devel,pcre-devel,openssl-devel state=installed state=installed
- user: name=nginx shell=/sbin/nologin
- shell: chdir=/root tar -zxf nginx-1.11.5.tar.gz -C /usr/src/
- shell: chdir=/usr/src/nginx-1.11.5 ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
- file: path=/usr/local/sbin/nginx state=link src=/usr/local/nginx/sbin/nginx
- shell: nginx
...
检查yaml文件语法
[root@localhost ~]# ansible-playbook --syntax-check nginx.yaml
playbook: nginx.yaml
执行剧本
[root@localhost ~]# ansible-playbook nginx.yaml
使用yaml剧本编译安装MySQL
下载mysql-5.5.22安装包
编写mysql.yaml文件
[root@localhost ~]# vim mysql.yaml
---
- hosts: webservers
remote_user: root
tasks:
- copy: src=/root/mysql-5.5.22.tar.gz dest=/root/mysql-5.5.22.tar.gz
- yum: name=gcc*,cmake,bison,autoconf,ncurses-devel state=installed
- user: name=mysql shell=/sbin/nologin
- shell: chdir=/root tar -zxvf mysql-5.5.22.tar.gz -C /usr/src
- shell: chdir=/usr/src/mysql-5.5.22 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc/ -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1
- shell: chdir=/usr/src/mysql-5.5.22 make && make install
- file: path=/usr/local/bin/mysq src=/usr/local/mysql/bin/mysql state=link
- file: path=/usr/local/mysql owner=mysql group=mysql
- shell: cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf
- shell: /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
- shell: chdir=/usr/src/mysql-5.5.22 cp support-files/mysql.server /etc/init.d/mysqld
- shell: chmod +x /etc/rc.d/init.d/mysqld
- shell: echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
- shell: source /etc/profile
- shell: /etc/init.d/mysqld start
...
检查yaml文件语法
[root@localhost ~]# ansible-playbook --syntax-check mysql.yaml
playbook: mysql.yaml
执行剧本
[root@localhost ~]# ansible-playbook mysql.yaml
使用yaml剧本编译安装PHP
下载php-5.3.28安装包
编写php.yaml文件
[root@localhost ~]# vim php.yaml
---
- hosts: webservers
remote_user: root
tasks:
- copy: src=/root/php-5.3.28.tar.gz dest=/root//php-5.3.28.tar.gz
- yum: name=gcc*,gd,libxml2-devel,libjpeg-devel,libpng-devel state=installed
- shell: chdir=/root tar -zxvf php-5.3.28.tar.gz -C /usr/src
- shell: chdir=/usr/src/php-5.3.28 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php --with-gd --with-zlib --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --enable-fpm --enable-mbstring --with-jpeg-dir=/usr/lib
- shell: chdir=/usr/src/php-5.3.28 make && make install
- shell: cp /usr/src/php-5.3.28/php.ini-development /usr/local/php/php.ini
- lineinfile: path=/usr/local/php/php.ini regexp="^default_charset =" line="default_charset = 'utf-8'"
- lineinfile: path=/usr/local/php/php.ini regexp="^short_open_tag =" line="short_open_tag = On"
- shell: cp /usr/src/php-5.3.28/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
- shell: chmod +x /etc/init.d/php-fpm
- shell: cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
- shell: /etc/init.d/php-fpm start
检查yaml文件语法
[root@localhost ~]# ansible-playbook --syntax-check php.yaml
playbook: php.yaml
执行剧本
[root@localhost ~]# ansible-playbook php.yaml