下面介绍的是ansible相关
一、ansible基础
1、ansible简介
ansible 是基于模块工作的,具有三个主要的功能,分别是:批量系统配置、批量程序部署、批量运行命令。
ansible 的架构组成有:连接插件、Host Inventory、Modules、playbook。
ansible 的特性:高度模块化、基于python语言(paramiko、PyYAML、jinja2三个模块)、部署为agentless(ansible)、支持自定义模块、支持剧本(playbook)、幂等性。
2、ansible 的程序环境
配置文件:/etc/ansible/ansible.cfg
主机清单:/etc/ansible/hosts
主程序:ansible、ansible-doc、ansible-playbook
3、ansible命令
格式:ansible
ansible
常用选项:
-a:指定模块参数
-e:设置任务变量
-m:选择模块,默认为command模块
例:
# ansible all -m ping
4、anisible模块
# ansible-doc -l -----------获取模块列表
# ansible-doc -s module_name ---------获取模块使用规则
(1)、command模块 ----------远程执行命令
# ansible all -m command -a 'useradd user1'
(2)、shell模块 ------------远程执行
# ansible all -m shell -a "echo 123 | passwd --stdin user1"
(3)、user模块 ---------管理用户
模块参数解释如下:
name:用户名
createhome:是否为用户创建家目录
system:是否为系统用户
uid:为用户指定uid
state:创建(present)、删除(absent)
remove:state=absent时使用
例:
# ansible all -m user -a "name=user2 system=yes createhome=no uid=777 state=present"
# ansible all -m user -a "name=user2 state=absent"
(4)、group模块
模块参数解释如下:
name:组账户名称
system:是否创建为系统组
gid:为组组账户指定gid
state:创建(present)、删除(absent)
(5)、copy模块 -------复制文件到远端
模块参数解释:
dest:复制的目标位置
src:源文件位置
content:字符信息
group:目标属组
owner:目标属主
mode:目标权限
例:
# ansible websrvs -m copy -a "content='hello everyone\n' dest=/;tmp/hello.txt owner=ftp mod=0600"
# ansible websrvs -m copy -a "src=/tmp/test.conf dest=/tmp owner=ftp mode=0600"
(6)、cron模块
模块参数解释:
name:cron任务名
minute:分钟
hour:小时
day:天
month:月
weekday:星期
job:工作内容,各种命令
state:创建(present)、删除(absent)
例:
# ansible all -m cron -a "name='update time' minute=*/5 job='/sbin/ntpdate 172.16.72.1 &> /dev/null' "
# ansible all -m cron -a "name='update time' minute=*/5 job='/sbin/ntpdate 172.16.7.1 &> /dev/null' state=absent"
(7)、file模块 ----------设置文件属性
模块参数解释:
path:远程主机文件路径
group:属组
owner:属主
mode:权限
src:符号链接文件路径
state:present、absent、link、file、directory
例:
# ansible websrvs -m file -a "src=/tmp/hello.txt path=/tmp/hello.txt.link state=link"
# ansible all -m file -a "path=/tmp/testdir state=directory"
(8)、hostname模块 ------管理主机名
模块参数解释:
name:要修改的主机名
例:
# ansible 172.16.72.2 -m hostname -a "name=test.little.com"
(9)、setup模块 ---------获取远程主机的变量
# ansible 172.16.67.2 -m setup
(10)、service模块 ----------管理服务
模块参数解释:
name:管理的服务名
enabled:服务是否开机自启
state:started、stopped、reloaded、restarted
例:
# ansible websrvs -m service -a "name=httpd state=started enabled=yes"
(11)、yum模块 ----------yum管理
模块参数解释:
name:管理的程序包名
state:安装:present、installed、latest
卸载:absent、removed
例:
# ansible websrvs -m yum -a "name=httpd state=present"
(12)、script模块 ----------远端运行程序脚本
# ansible 172.16.72.2 -m script -a "/root/adduser.sh"
二、ansible剧本
1、ansible 的剧本(playbook)
playbook文件的文件格式为YAML,具体的实现是通过列表和字典来实现的。
(1)、playbook的基本结构如下:
- hosts:
remote_user:
var:
tasks:
- name:
module_name: argument=value
- name:
module_name:
argument: value
template: src=/PATH/TO/xxx.j2 dest=/PATH/TO/SOMEWHERE
template:
src:
dest:
notify:
tags:
handlers:
例1:
- host: all
remote_user: root
tasks:
- name: create user auser1
user: name=auser1 uid=333 system=yes createhome=no
- name: create user auser2
user: name=auser2 uid=555 system=yes createhome=no
例2:ansible部署nginx
- host: ngxsrvs
remote_user: root
tasks:
- name: install nginx package
yum: name=nginx state=present
- name: provide config file
copy: src=../files/nginx.conf dest=/etc/nginx/nginx.conf
- name: start nginx service
service:
name: nginx
state: started
enaled: yes
(2)、运行playbook方法
1)、测试
# ansible-playbook --check /PATH/TO/YAML_FILE
2)、运行
# ansible-playbook /PATH/TO/YAML_FILE
3)、列表给出运行任务主机
# ansible-playbook --list-hosts /PATH/TO/YAML_FILE
(3)、handlers出发任务实例 ------创建ngx.yml
- hosts: ngxsrvs
remote_user: root
tasks:
- name: install nginx package
yum: name=nginx state=present
tags:setupnginx
- name: provide config file
copy: src=../files/nginx.conf dest=/etc/nginx/nginx.conf
notify: reload conf
tags: provconf
- name: start nginx service
service:
name: nginx
state: started
enabled: yes
handlers:
- name: reload conf
service: name=nginx state=reloaded
handlers调用方式:
# ansible-playbook -t provconf ngx.yml
2、变量
ansible-playbook命令:
常用选项:
-t:指明标签
-e:命令行传递自变量
# ansible-playbook -e EXTRA_VARS, --extra-vars=EXTRA_VARS
在YAML文件中定义变量并引用:
- host: all
remote_user: root
var: var1=value1 var2=value2
var:
var3: value3
var4: value4
引用变量的方式:` VAR_NAME `
3、template模块 ----------输出到远程服务器的模板
模板参数:
src=
dest=
mode=
group=
owner=
例:
- hosts: ngxsrvs
remote_user: root
tasks:
- name: install nginx package
yum: name=nginx state=present
tags: setupnginx
- name: provide config file
template: src=../files/nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: reload conf
tags: provconf
- name: start nginx service
service:
name: nginx
state: started
enabled: yes
handlers:
- name: reload conf
service: name=nginx state=reloaded
4、条件判断实例
- host: dbsrvs
remote_user: root
vars:
pkgstat: present
servicestat: started
tasks:
- name: install mysql-server for CentOS 6
yum: name="mysql-server" state=` pkgstat `
when: ansible_distribution_major_version == '6'
tags: managepkg
- name: install mariadb-server for CentOS 7
yum: name="mariadb-server" state=` pkgstat `
when: ansible_distribution_major_version == '7'
tags: managepkg
- name: start mysql on CentSO 6
service: name=mysqld state=` servicestat ` enabled=true
when: ansible_distribution_major_version == '6'
tags: managedb
- name: start mariadb on CentOS 7
service: name=mariadb state=` servicestat ` enabled=true
when: ansible_distribution_major_version == '7'
tags: managedb
5、循环实例
- host: websrvs
remote_user: root
tasks:
- name: install httpd
yum: name=` item ` state=latest
with_items:
- httpd
- php
- mariadb-server
循环用户管理:
- host: all
remote_user: root
tasks:
- name: add a group
group: name=` item ` state=present
with_items:
- group110501
- group110502
- group110503
- name: add some users
user: name=` item`.`name ` group=` item`.`group ` state=present
with_items:
- { name: 'user110501',group: 'group110501' }
- { name: 'user110502',group: 'group110502' }
- { name: 'user110503',group: 'group110503' }
6、ansible的角色
角色目录:/etc/ansible/roles
例如创建角色:/etc/ansible/roles/nginx
其下包括:files、template、tasks、handlers、vars、meta、default。