ansible-playbook了解(一)--简写版
1.playbook简介
playbook是ansible用于配置,部署,和管理节点的剧本。
2.playbook格式
playbook由YAML语言编写。
3.playbook执行过程
1.将以编排好的任务集(ansible 单条命令集合)写进playbook
2.通过ansible-playbook命令分拆任务集逐条执行ansible命令,按预定规则逐条执行
4.playbook组件
hosts:运行执行任务(task)的目标主机
remote_user:在远程主机上执行任务的用户
tasks:任务,由模板定义的操作列表
handlers:任务,与tasks不同的是只有在接受到通知(notify)时才会被触发
templates:模板,使用模板语言的文本文件,使用jinja2语法。
variables:变量,变量替换{{ variable_name }}
roles:角色
5.playbook简单例子
5.1 查看ansible主机列表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@localhost home]# cat /etc/ansible/hosts # This is the default ansible 'hosts' file. # # It should live in /etc/ansible/hosts # # - Comments begin with the '#' character # - Blank lines are ignored # - Groups of hosts are delimited by [header] elements # - You can enter hostnames or ip addresses # - A hostname/ip can be a member of multiple groups # Ex 1: Ungrouped hosts, specify before any group headers. ## green.example.com ## blue.example.com ## 192.168.100.1 ## 192.168.100.10 192.168.235.147 # Ex 2: A collection of hosts belonging to the 'webservers' group [webservers] ## alpha.example.org ## beta.example.org ## 192.168.1.100 ## 192.168.1.110 43.143.98.52 # If you have multiple hosts following a pattern you can specify # them like this: ## www[001:006].example.com # Ex 3: A collection of database servers in the 'dbservers' group ## [dbservers] ## ## db01.intranet.mydomain.net ## db02.intranet.mydomain.net ## 10.25.1.56 ## 10.25.1.57 # Here's another example of host ranges, this time there are no # leading 0s: ## db-[99:101]-node.example.com
5.2 查看用yaml编写好的ansible-playbook剧本test.yml
[root@localhost home]# cat test.yml #---文档开始的符号 --- #hosts 指定哪些主机执行tasks - hosts: webservers #remote_user 指定执行远程执行ansible模块的用户 remote_user: root # 定义任务列表 tasks: #name 描述task任务说明 - name: create a newfile #file ansible模块,创建一个new-test-file文件 file: name=/home/new-test-file state=touch ...
5.3 测试playbook执行效果。ansible-playbook -C test.yml
[root@localhost home]# ansible-playbook -C test.yml PLAY [webservers] *********************************************************************************************************************************************************************************************************************************************************************** TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************************** ok: [43.143.98.52] TASK [create a newfile] ***************************************************************************************************************************************************************************************************************************************************************** ok: [43.143.98.52] PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************************** 43.143.98.52 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
5.4 执行ansible-playbook
[root@localhost home]# ansible-playbook test.yml PLAY [webservers] *********************************************************************************************************************************************************************************************************************************************************************** TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************************** ok: [43.143.98.52] TASK [create a newfile] ***************************************************************************************************************************************************************************************************************************************************************** changed: [43.143.98.52] PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************************** 43.143.98.52 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0