Ansible's script is written in the Yaml language, with the following basic syntax:
Basic syntax:
1. Case
2, the same level to be aligned, indentation representation level
3, indentation is not allowed to use the TAB key
4, the indentation of the number of spaces does not stipulate that the same element can be distinguished
Supported Data Structures:
1. Object: A set of key-value pairs, also known as a map/hash/dictionary
Example: Name:example key: Value
2, array: A set of values in order, also known as the series/list/
Example:-apple
3, Pure quantity: a single, no longer sub-value
Example: number:12.30
Sure:true
Play-book Script
The ansible template is called by tasks to run multiple play organizations in one playbook.
Palybook components:
(1) Tasks: task, equivalent to executing a transaction
(2) Variables: variable (defined scene: Hosts file; script; command)
(3) Templates: Template
(4) Handlers: processor, satisfies the condition, triggers the execution of the operation
(5) Roles: role
A small example of a simple playbook:
Experimental environment: Two Centos7, one ansible server, one test machine.
Ansible Server address: 192.168.71.128
Test machine server Address: 192.168.71.129
vim /opt/book.yml #首先创建一个以.yml为结尾的文件
- hosts: webserver #hosts定义了配置文件中的组名 remote_user: root #剧本中的演员:root用户,也可以是你推送秘钥的任意用户 tasks: #任务,以下是执行什么任务 - name: download apache #自行定义的名称 yum: name=httpd #指定模块,模块后跟相对应的操作 - name: stopped firewalld service: name=firewalld state=stopped - name: stopped selinux command: ‘/usr/sbin/setenforce 0‘ - name: copy index.html template: src=/opt/index.html dest=/var/www/html/index.html #这里的模板要注意,需要创建推送的文件并写入你指定的内容。 - name: started apache service: name=httpd state=started
检查yml文件中语法的正确性
Ansible-playbook book.yml--syntax-check #检查yaml语法
Ansible-playbook book.yml--list-tasks #检查tasks任务
Ansible-playbook book.yml--syntax-hosts #检查生效的主机
Ansible-playbook book.yml--start-at-task= ' Copy nginx.conf
The execution process is as follows:
As follows:
playbook剧本一些常用的操作
1. Trigger Call
- hosts: webserver #定义主机组 vars: #定义变量 http_port: 80 max_clients: 200 users:root tasks: #执行的任务 - 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: #处理器,给notify调用的命令,不调用不进行任何操作。 - name: restart apache service: name=httpd state=restarted
2, execution error, skip change operation continue execution
- hosts: webserver remote_user: root tasks: - name: create mysql user: #这里不添加任何的实质性操作,会报错 ignore_errors: true #添加该行后出错跳过,继续执行 - name: apache status command: ‘systemctl status httpd‘
Ansible-playbook Script Foundation (i)