目录
YAML标记语言
YAML是一种表达资料序列的格式,由于参考了其他多种语言,所以具有很高的可读性。其特性如下:
- 具有很好的可读性,易于实现
- 表达能力强,扩展性好
- 和脚本语言的交互性好
- 有一个一致的信息模型
- 可以基于流来处理
基本语法规则
1.大小写敏感
2.使用缩进表示层级关系
3.缩进时不允许使用Tab键,只允许使用空格。
4.缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
1.YAML中两种常用的数据类型,分别是list和directory
list
-teacher
-student
2.列表的所有元素均使用“-”开头
directory
YAML支持的数据结构
1.对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary) 例如:name:Example Developer 键 值
2.数组:一组按次序排列的值,又称为序列(sequence) / 列表(list) 例如:-Apple
-Orange
3.纯量:单个的、不可再分的值 例如:number:11.20 sure:true
Playbook概述
playbook是由一个或者多个play组成的列表,主要功能是将task定义好的角色并为一组进行统一管理,也就是通过task调用Ansible的模块将多个paly组织在一个playbook中。playbook本身由以下各部分组成:
- Tasks:任务,即调用模块完成的某操作
- Varibles:变量
- Templates:模版
- Handlers:处理器,当某条件,满足时,触发的操作
- Roles:角色
inventory变量参数
ansible_ssh_host //将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置
ansible_ssh_port //ssh端口号.如果不是默认的端口号,通过此变量设置
ansible_ssh_user //默认的 ssh 用户名
ansible_ssh_pass //ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)
ansible_ssh_private_key_file //ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况
ansible_ssh_common_args //此设置附加到sftp,scp和ssh的缺省命令行
ansible_sftp_extra_args //此设置附加到默认sftp命令行
ansible_scp_extra_args //此设置附加到默认scp命令行
ansible_ssh_extra_args //此设置附加到默认ssh命令行
ansible_ssh_pipelining //确定是否使用SSH管道. 这可以覆盖ansible.cfg中得设置
ansible_shell_type //目标系统的shell类型.默认情况下,命令的执行使用 ‘sh’ 语法,可设置为 ‘csh’ 或 ‘fish’
ansible_python_interpreter // 目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是"/usr/bin/python",比如 *BSD, 或者 /usr/bin/python
ansible_*_interpreter //这里的"*"可以是ruby 或perl 或其他语言的解释器,作用和ansible_python_interpreter 类似
ansible_shell_executable // 这将设置ansible控制器将在目标机器上使用的shell,覆盖ansible.cfg中的配置,默认为/bin/sh
playbook的简单示例:
vi test.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=/srv/httpd.j2 dest=/etc/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的格式
ansible-playbook test.yaml
注意:/srv/httpd.j2要先创建这个文件
参数:
-k(–ask-pass) 用来交互输入ssh密码
-K(-ask-become-pass) 用来交互输入sudo密码
-u 指定用户
常用检查命令
#检查yaml文件的语法是否正确
ansible-playbook test.yaml --syntax-check
#检查tasks任务
ansible-playbook test.yaml --list-task
#检查生效的主机
ansible-playbook test.yaml --list-hosts
#指定从某个task开始运行
ansible-playbook test.yaml --start-at-task='Copy Nginx.conf'
Hosts和Users介绍
playbook的设计目的就是为了让某个或者某些主机以某个身份去执行相应的任务。其中用于指定要执行任务的主机hosts定义,可以是一个主机也可以是由冒号分隔的额多个主机组;用于指定被管理主机上执行任务的用户用remote_user来定义,例如:
- hosts: webserver
remote_user: root
remote_user也可以定义指定用户通过sudo的方法在被管理主机上运行指令,甚至可以在使用become指定sudo切换的用户。
实例:
vim /opt/a.yml
- hosts: webservers #指定主机组,可以是一个或多个组。
remote_user: root #指定远程主机执行的用户名
检查语法
[root@master ~]# ansible-playbook a.yaml --syntax-check
[WARNING]: Could not match supplied host pattern, ignoring: webservers
playbook: a.yaml
执行:
[root@master ~]# ansible-playbook a.yaml
[WARNING]: Could not match supplied host pattern, ignoring: webservers
PLAY [webservers] ********************************************************************************************
skipping: no hosts matched
PLAY RECAP ***************************************************************************************************
为每个任务定义远程执行用户
- hosts: webservers
remote_user: root
tasks:
- name: test connection
ping:
remote_user: root
执行:
ansible-playbook a.yaml
指定远程主机sudo切换主机
- hosts: mysql
remote_user: root
become: yes
become_user: lisi
tasks:
- name: copy text
copy: src=/etc/fstab dest=/home/lisi/fstab.back
执行
被管理机查看
tasks列表和action
1.Play的主体部分是task列表,task列表中的各任务按次序逐个在hosts中指定的主机上执行,即在所有主机上完成第一个任务后再开始第二个任务。
在运行playbook时(从上到下执行),如果一个host执行task失败,整个tasks都会回滚,请修正playbook 中的错误,然后重新执行即可。
Task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量.
2.每一个task必须有一个名称name,这样在运行playbook时,从其输出的任务执行信息中可以很好的辨别出是属于哪一个task的。如果没有定义name,‘action’的值将会用作输出信息中标记特定的task。
3.定义一个task,常见的格式:”module: options” 例如:yum: name=httpd
实例:
里面有错误操作
- hosts: mysql
remote_user: root
tasks:
- name: install httpd
yum: name=httpd
- name: start httpd
service: name=httpd state=started
- name: stop firwalld
service: name=firewall state=stopped //注意firewalld少个d
- name: touch index
copy: content=aa_web dest=/var/www/html/index.html
执行:
被管理查看httpd并未被安装
task中有错误操作添加ignore_errors: true
- hosts: webserver
remote_user: root
tasks:
- name: install httpd
yum: name=httpd
- name: start httpd
service: name=httpd state=started
- name: stop firwalld
service: name=firewall state=stopped //firewalld错误操作
ignore_errors: true //忽略错误,强制返回成功 下面的语句继续执行
- name: touch index
copy: content=aa_web dest=/var/www/html/index.html
被管理查看
其他任务正常运行
Handlers介绍
Handlers也是一些task的列表,和一般的task并没有什么区别。
是由通知者进行的notify,如果没有被notify,则Handlers不会执行,假如被notify了,则Handlers被执行
不管有多少个通知者进行了notify,等到play中的所有task执行完成之后,handlers也只会被执行一次
示例
vim .yml
- hosts: webservers
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=/opt/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-playbook c.yml
playbook使用变量的方法
示例
vim cjf.yml
- hosts: mysql
remote_user: root
vars:
- username: cjf
tasks:
- name: create user
user: name={{username}}
执行
ansible-playbook cjf.yml
'或者-e后面加变量直接引用'
- hosts: mysql
remote_user: root
vars:
tasks:
- name: create user
user: name={{username}}
然后执行命令:ansible-playbook cjf.yml -e "user=cjf" 都是可行的
固定变量
- hosts: mysql
remote_user: root
tasks:
- name: create file
copy: content={{ansible_all_ipv4_addresses}} dest=/opt/add.txt
'执行剧本'
ansible-playbook cjf.yml
'指定被管理者查看'
cat /opt/add.txt
条件测试
如果需要根据变量、facts(setup)或此前任务的执行结果来作为某task执行与否的前提时要用到条件测试,在Playbook中条件测试使用when子句。
在task后添加when子句即可使用条件测试:when子句支持正则表达式或语法
示例
vim a.yml
- hosts: mysql
remote_user: root
tasks:
- name: "shutdown CentOS"
command: /sbin/shutdown -h now '-h:关机 -r:重启'
when: ansible_distribution == "CentOS" '满足Centos就关机'
'执行'
ansible-playbook a.yml '发现mysql主机已经被关机'
多条件判断
vim a.yml
- hosts: mysql
remote_user: root
tasks:
- name: "shut down CentOS 7 systems"
command: /sbin/shutdown -r now
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "7"
组条件判断
vim a.yml
- hosts: mysql
remote_user: root
tasks:
- name: "shut down CentOS 6 and Debian 7 systems"
command: /sbin/shutdown -t now
when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or
(ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
'执行副本'
ansible-playbook a.yml
自定义变量进行条件测试
vim a.yml
- hosts: all
vars:
exist: "True" 'True被管理者opt目录创建test.txt文件 改为False删除'
tasks:
- name: creaet file
command: touch /tmp/test.txt
when: exist | match("True")
- name: delete file
command: rm -rf /tmp/test.txt
when: exist | match("False")
执行
ansible-playbook a.yml
迭代
当有需要重复性执行的任务时,可以使用迭代机制。其使用格式为将需要迭代的内容定义为item变量引用,并通过with_items语句指明迭代的元素列表即可。
示例
vim a.yml
- hosts: webserver
remote_user: root
tasks:
- name: "Install Packages"
yum: name={{ item }} state=latest
with_item:
- httpd
- mysql-server
- php
'执行剧本'
ansible-playbook a.yml
'查看'
tail -3 /etc/passwd
httpd:x:1001:1001::/home/httpd:/bin/bash
mysql-server:x:1002:1002::/home/mysql-server:/bin/bash
php:x:1003:1003::/home/php:/bin/bash
可以自行定义
vim b.yml
- hosts: webservers
remote_user: root
tasks:
- name: "Add users"
user: name={{ item.name }} state=present groups={{ item.groups }}
with_items:
- { name:'test1', groups:'wheel'}
- { name:'test2', groups:'root'}
'执行'
ansible-playbook b.yml
#在所有主机上查看是否有指定添加的用户
tail -5 /etc/passwd
Templates:模板文件以.j2后缀
Jinja是基于 Python的模板引擎。 Template类是 Jinja的另一个重要组件,可以看
作是一个编译过的模板文件,用来产生目标文本,传递 Python的变量给模板去替换模
板中的标记。
示例
从被管理端复制一份httpd.conf到管理端opt目录下
scp root@192.168.100.22:/etc/httpd/conf/httpd.conf ./
vim httpd.conf
Listen {{http_port}} '//后面添加变量'
ServerName {{server_name}}
MaxClients {{access_num}}
'重命名为模板'
mv httpd.conf httpd.conf.j2
'参数赋值'
vim /etc/ansible/hosts
[mysql]
192.168.100.21 http_port:192.168.100.21:80 server_name="www.yun.com:80" access_num=200
编写
vim aa.yml
- hosts: mysql
remote_user: root
vars:
- package: httpd
- service: httpd
tasks:
- name: install httpd package
yum: name={{package}} state=latest #安装最新版本的httpd
- name: install configure file
template: src=/root/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf #使用模板并根据变量进行配置
notify:
- restart httpd #调用handler
- name: start httpd server
service: name={{service}} enabled=true state=started #开启服务
handlers:
- name: restart httpd
service: name={{service}} state=restarted
'运行剧本'
ansible-playbook aa.yml
'被管理者过滤查看信息'
#在两台被管理服务器上查看
grep -i listen /etc/httpd/conf/httpd.conf
grep -i maxClient /etc/httpd/conf/httpd.conf
grep -i servername /etc/httpd/conf/httpd.conf
tags模块
在一个playbook中,我们一般会定义很多个task,如果我们只想执行其中的某一个task或多个task时就可以使用tags标签功能了。
vi bb.yml
- hosts: mysql
remote_user: root
tasks:
- name: Copy hosts file
copy: src=/etc/hosts dest=/opt/qie
tags:
- only '只会执行上上面的命令 下面不执行'
- name: touch file
file: path=/opt/op state=touch
'执行剧本'
ansible-playbook bb.yml --tags="only"
事实上,不光可以为单个或多个task指定同一个tags。playbook还提供了一个特殊的tags为always。作用就是当使用always当tags的task时,无论执行哪一个tags时,定义有always的tags都会执行。
- hosts: mysql
remote_user: root
tasks:
- name: Copy hosts file
copy: src=/etc/hosts dest=/opt/qie
tags:
- only
- name: touch file
file: path=/opt/op state=touch
- always '只要带有always都会执行'
#执行剧本
ansible-playbook bb.yml --tags="only"
'在被管理器查看即可'
ls /opt