ansible默认的主机清单是/etc/ansible/hosts文件
主机清单可以手动设置,也可以通过Dynamic Inventory动态生成
一般主机名使用FQDN
vi /etc/ansible/hosts
[webserver] #使用方括号设置组名
www1.example.org #定义被监控主机,这边可以是主机名也可以是IP地址
www2.example.org:2222 #冒号后定义远程连接端口,默认是ssh的22端口
如果是名称类似的主机,可以使用列表的方式标识各个主机
[webserver]
//[01:50]表示匹配从01到50,后面跟着内置变量,这里定义了ssh的访问的用户名和密码,用于免交互登录
www[01:50].example.org ansible_ssh_user=root ansible_ssh_pass=abc123
[dbbservers]
//[a:f]表示支持匹配a到f
db-[a:f].example.org
Inventory中的变量
主机变量
[webserver]
//定义变量http_port(开放的端口信息)和maxRequestsChild(最大进程数)
www1.magedu.com http_port=80 maxRequestsChild=808
www2.magedu.com http_port=8080 maxRequestsChild=909
组变量
[servers:vars]
ntp_server=ntp.example.org
nfs_server=nfs.example.org
组嵌套
[apache]
http1.example.org
http2.example.org
[nginx]
ngx1.example.org
ngx2.example.org
//定义一个组名,将刚才定义的两个组名放入,即webservers组包含apache组和nginx组的主机
[webservers]
apache
nginx
inventory变量参数
YAML:另一种标记语言。是用来写配置文件的语言,非常简洁和强大。
YAML语法和其他语言类似,也可以表达散列表、标量等数据结构。
结构通过空格来展示;序列里配置项通过-来代表;Map里键值用:来分隔;YAML的扩展名为yaml
对象
键值对的集合,又称为映射(mapping)/哈希(hashes)/字典(dictionary)
例如: name (键): Example(值)
类class:(物品)
对象1:(桌子)
属性(名称,长、宽、高等)
方法(动词,放东西)
...
对象2
对象3
数组
一组按次序排列的值,又称为序列(sequence)/列表(list)
例如:-Apple
-Orange
纯量
单个的、不可再分的值
例如:number: 12.30
sure: true
通过task调用ansible的模板将多个play组织在一个playbook中运行。
我们先看一个playbook的示例简单了解一下
vim httpd.yaml
- hosts: webserver
vars:
http_port: 80
max_clients: 200
user: root
tasks:
- name: ensure apache is at the latest version
yum: pkg=httpd state=latest
- name: write the apache config file
template: src=/root/test/httpd.j2 dest=/etc/httpd/conf/httpd.conf
notify:
- restart apache
- name: ensure apache is running
service: name=httpd state=started
handlers:
- name: restart apache
service: name=httpd state=restarted
#---------------------playbook解释------------------------
- hosts: webserver //定义的主机组,即应用的主机
vars: //定义变量
http_port: 80
max_clients: 200
user: root //指定用户执行任务
tasks: //执行的任务
- name: ensure apache is at the latest version //自定义输出内容,任务名称
yum: pkg=httpd state=latest //yum模块:指定软件包和版本参数;即使用yum安装最新版的httpd
- name: write the apache config file
template: src=/srv/httpd.j2 dest=/etc/httpd.conf //定义一个httpd的模板(在管理端自己创建)
notify: //调用下面的操作
- restart apache //操作的名字,在handlers下定义
- name: ensure apache is running
service: name=httpd state=started //输出httpd状态
handlers: //处理器
- name: restart apache //被调用的操作名字
service: name=httpd state=restarted //重启httpd服务
格式:
ansible-playbook [yaml文件名]
例如:ansible-playbook ping.yml
参数:-k(–ask-pass) 用来交互输入ssh密码
-K(-ask-become-pass) 用来交互输入sudo密码
-u 指定用户
补充命令:
ansible-playbook XXXX.yaml --syntax-check #检查yaml文件的语法是否正确
ansible-playbook XXXX.yaml --list-task #检查tasks任务
ansible-playbook XXXX.yaml --list-hosts #检查生效的主机
ansible-playbook XXXX.yaml --start-at-task='ensure apache is at the latest version' #指定从某个task开始运行
还可以为每个任务定义远程执行用户:
vim mysql.yaml
- hosts: mysql
remote_user: root
tasks:
- name: test connection
ping:
remote_user: root
执行playbook时:ansible-playbook mysql.yaml -k
指定远程主机sudo切换用户:
vim mysql.yaml
- hosts: mysql
remote_user: root
tasks:
- name: test connection
ping:
remote_user: fff
vim hosts.yaml
- hosts: webserver
remote_user: root
tasks:
- name: create nginx group
group: name=nginx system=yes gid=208
- name: create nginx user
user: name=nginx uid=208 group=nginx system=yes
- hosts: mysql
remote_user: root
tasks:
- name: copy file to mysql
copy: src=/etc/inittab dest=/opt/inittab.back
vim error.yaml
- hosts: webserver
remote_user: root
tasks:
- name: disable selinux
command: '/sbin/setenforce 0'
- name: make sure apache is running
service: name=httpd state=started
只要执行命令的返回值不为0,就会报错,tasks停止
修改如下
- hosts: webserver
remote_user: root
tasks:
- name: disable selinux
command: '/sbin/setenforce 0'
ignore_errors: True #忽略错误,强制返回成功
- name: make sure apache is running
service: name=httpd state=started
Handlers也是一些task的列表,和一般的task并没有什么区别。
是由通知者进行的notify,如果没有被notify,则Handlers不会执行,假如被notify了,则Handlers被执行
不管有多少个通知者进行了notify,等到play中的所有task执行完成之后,handlers也只会被执行一次
vim handler.yaml
- hosts: webserver
remote_user: root
tasks:
- name: install httpd package
yum: name=httpd state=latest
- name: install configuration file for httpd
copy: src=/root/handler/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: start httpd service
service: enabled=true name=httpd state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted
vim test1.yml
- hosts: mysql
remote_user: root
vars:
- user:
tasks:
- name: add new user
user: name={{user}}
然后执行命令: ansible-playbook test1.yml -e “user=lisi”
可以执行命令查看:ansible mysql -m shell -a ‘cat /etc/shadow |grep “lisi”’
vim test2.yml
- hosts: webserver
remote_user: root
vars:
package: httpd
service: httpd
tasks:
- name: install httpd package
yum: name={{package}} state=latest
- name: install configuration file for httpd
copy: src=/root/handler/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: start httpd service
service: enabled=true name={{service}} state=started
handlers:
- name: restart httpd
service: name={{service}} state=restarted
引用ansible的固定变量
vim test3.yml
- hosts: mysql
remote_user: root
tasks:
- name: copy file
copy: content="{{ansible_all_ipv4_addresses}}," dest=/opt/vars.txt
执行命令:ansible-playbook test3.yml
查看vars.txt文件内容:ansible mysql -a ‘cat /opt/vars.txt’
引用主机变量
vim /etc/ansible/hosts
在mysql组的主机后面添加如下
[mysql]
192.168.10.17 testvar="10.15" #定义testvar变量的值为163.200
vim test3.yml #添加{{testvar}}主机变量
- hosts: mysql
remote_user: root
tasks:
- name: copy file
copy: content="{{ansible_all_ipv4_addresses}},{{testvar}}" dest=/opt/vars.txt
执行命令:ansible-playbook test3.yml
查看vars.txt文件内容:ansible mysql -a ‘cat /opt/vars.txt’
如果需要根据变量、facts(setup)或此前任务的执行结果来作为某task执行与否的前提时要用到条件测试,在Playbook中条件测试使用when子句。
在task后添加when子句即可使用条件测试:when子句支持jinjia2表达式或语法,例如:
vim when.yml
- hosts: mysql
remote_user: root
tasks:
- name: "shutdown CentOS"
command: /sbin/shutdown -h now
when: ansible_distribution == "CentOS"
vim when.yml
- hosts: mysql
remote_user: root
tasks:
- name: "shutdown CentOS 7 systems"
command: /sbin/shutdown -r now
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "7"
vim when.yml
- hosts: mysql
remote_user: root
tasks:
- name: "shutdown CentOS 7 and Debian 7 systems"
command: /sbin/shutdown -r now
when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "7") or
(ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
当有需要重复性执行的任务时,可以使用迭代机制。其使用格式为将需要迭代的内容定义为item变量引用,并通过with_items语句指明迭代的元素列表即可。例如:
vim install.yaml
- hosts: webserver
remote_user: root
tasks:
- name:
command: rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
- name: "Install Packages"
yum: name={{ item }}
with_items:
- httpd
- mysql-server
- php