3. Implement playbook #########
The goal of this chapter: write a basic ansible playbook
3.1 Write and run playbook
3.1.1 View temporary commands of a specified user
'The yaml format usually has the extension yml, and yaml has no strict requirements for indentation, but there are two basic principles'
' 1. Data elements at the same level in the same hierarchy must have the same indentation '
' 2. If the item belongs to a child of another item, the indentation must be greater than the parent item '
ansible -m user -a "name=student uid=1000state=present" servera.lab.example.com
Rewritten as playbook
[root@workstation ~]# cat user.yml
--- 开头三个破折号,文档的开始标记
- name: Configure User 可写可不写,但是建议使用
... 结尾三个省略号,结束标记(通常省略)
Play itself is a set of key and value pairs. The keys in the same play should use the same indentation.
hosts is a key, and the following content is the value. This is a nested collection.
playbook execution:
3.1.2 Install apache
Create a new directory for easy operation
mkdir /demo
vim webserver.yml
It can be seen that the httpd service has been installed.
3.1.3 Make sure the httpd service is started
vim service.yml
effect:
3.1.4 Improve output detail level
The default output of ansible-playbook does not provide detailed task execution information. The -v parameter provides four levels:
- -v #Display task results
- -vv #Display task results and task configuration
- -vvv #contains information about connections to managed hosts
- -vvvv #Add additional verbosity options related to connecting plugins (including users who execute scripts and scripts executed on managed hosts)
# Best to perform syntax verification before executing playbook
# No syntax error
Change the playbook:
If there is a grammatical error, it will prompt the wrong location.
3.1.5 Perform dry run, just preview the result
We first remove the httpd service on the 71 host:
then execute the playbook;
ansible-playbook -C webserver.yml
is still not installed. As can be seen.
'Dry run will report what will happen when this playbook is executed, but will not change the target host'
3.1.6 Install, configure the default release page and start apache
vim site.yml
Detection grammar
ansible-playbook --syntax-check site.yml
no problem.
# Create the configuration file and list
# Create the default publishing page
cat files / index.html to
execute the playbook:
service installation and enable.
The publishing page was changed successfully.
# Test
curl obtain remote browser content.
3.2 Implement multiple play
Template:
ansible-doc -l
#List all modules ansible-doc yum #List usage and examples of
yum ansible-doc -s yum #Use terminal to output usage of each parameter in yum module
3.3 PLAYBOOK yaml syntax changes
- Comments by yaml
- yaml string
directly input
this is a string
single quotes
'this is a string'
multiple quotes
"this is a string"
# Exercise
Change the remote host page without closing the firewall and visit:
vim webserver.yml
---
- name: Enable internet service
hosts: web
become: yes
tasks:
- name: apache and firewalld installed # 检测firewalld和httpd是否为最新版
yum:
name:
- httpd
- firewalld
state: latest
- name: Configure index.html # 配置默认发布页面
copy:
content: "welcome to westos!\n"
dest: /var/www/html/index.html
- name: firewalld enabled and runing # 检测firewalld是否开机自启并启用
service:
name: firewalld
enabled: true
state: started
- name: firewald primits http # 检测firewalld是否添加http服务
firewalld:
service: http
permanent: true
state: enabled
immediate: yes
- name: httpd enabled and runing # 检测httpd服务是否自启并运行
service:
name: httpd
enabled: true
state: started
- name: test webserver 本地主机测试访问
hosts: localhost
become: no
tasks:
- name: connect web server
uri:
url: http://rhel71.com
return_content: yes
status_code: 200
# Detection grammar
# run
ansible-playbook -v webserver.yml
You can see the content welcome to westos
and status code 200 are returned
3.4 Management variables and facts
Use some variables in playbook instead of variables to simplify playbook writing
3.4.1 Management variables
Introduction to ansible variables
Variables may contain the following values:
users to be created,
software packages to be installed,
services to be restarted,
files to be deleted, documents to
be retrieved from the Internet
Named variable
Variable names must start with a letter, and can only contain letters, numbers, and underscores.
Incorrect
web server web_server
westos.file remote_file
1file file1
Define variables
Three scope levels
Global scope: Variables set from the command line or ansible configuration
Play scope: Variables set in play and related structures
Host scope: Tasks collected or registered by inventory, facts, set on host groups and individual hosts Variables
' If variables with the same name are defined on multiple levels, the variable with the highest level is preferred, and narrow scope takes precedence over wide scope
3.4.2 Variables in playbook
Define variables in playbook
1. Common way: In the vars block at the beginning of the playbook:
2. Define the playbook variable in the external file
cat user.yml
Use variables in playbook
Put the variable name in curly brackets.
'Note: When the variable is used as the first element to start a value, you must use quotation marks.'