1.编写和运行 playbook
查看指定用户的临时命令
[root@workstation ~]# ansible -m user -a "name=student uid=1000 state=present" servera.lab.example.com
yaml 格式通常以 yml 为扩展名,yaml 对于缩进量没有严格要求,但是有两个基本原则:
1.处于同一层次结构中同一级别的数据元素必须具有相同的缩进量
2.如果项目属于其他项目的子项,其缩进量必须大于父项
改写为 playbook
[root@workstation ~]# cat user.yml
— #开头三个破折号,文档的开始标记
- name: Configure User #可选,但是建议使用
hosts: servera.lab.example.com
tasks:
- name: Student User
user:
name: student
uid: 1000
state: present
… #结尾三个省略号,结束标记(通常省略)
play 本身是一个键值对集合,同一 play 中的键应当使用相同的缩进量
安装 apache
[root@workstation playdemo]# cat webserver.yml
- name: Setup Webserver
hosts: servera.lab.example.com
tasks:
- name: Http Installed
yum:
name: httpd
state: latest
[root@workstation playdemo]# ansible-playbook webserver.yml
例子:确保服务开机启动
[root@workstation playdemo]# cat service.yml ---
- name: Setup Webserver
hosts: servera.lab.example.com
tasks:
- name: Apache is enabled
service:
name: httpd
enabled: true
- name: Postfix is enabled
service:
name: postfix
enabled: true
提高输出详细程度
ansible-playbook 默认输出不提供详细任务执行信息。-v 参数提供,共四个级别:
-v #显示任务结果
-vv #显示任务结果和任务配置
-vvv #包含关于与受管主机的连接信息
-vvvv #增加连接插件相关的额外详细程度选项(包括受管主机上用于执行脚本的用户及所 执行的脚本)
#执行 playbool 前最好进行语法验证:
[root@workstation playdemo]# ansible-playbook --syntax-check webserver.yml #无语法错误
playbook: webserver.yml
[root@workstation playdemo]# ansible-playbook --syntax-check webserver.yml #有语法错误,会提示错误位置
#执行空运行
[root@workstation playdemo]# ansible-playbook -C webserver.yml
空运行会报告执行这个 playbook 将会发生什么,但不会改变目标主机
安装、配置默认发布页并启动 apache
[root@workstation playbook-basic]# cat site.yml ---
- name: Install and Start Apache
host: web
tasks:
- name: Apache is present
yum:
name: httpd
state: present
- name: Change index.html
copy:
src: files/index.html
dest: /var/www/html/index.html
- name: Start Apache
service:
name: httpd
state: started
enabled: true
#检测语法
[root@workstation playbook-basic]# ansible-playbook --syntax-check site.yml playbook: site.yml
#创建配置文件和清单
[root@workstation playbook-basic]# cat ansible.cfg
[defaults]
inventory = ./inventory
[root@workstation playbook-basic]# cat inventory
[web]
serverb.lab.example.com
serverc.lab.example.com
默认发布页面
[root@workstation playbook-basic]# cat files/index.html
www.westos.org
执行 playbook
[root@workstation playbook-basic]# ansible-playbook site.yml
测试
[root@workstation playbook-basic]# curl serverb.lab.example.com www.westos.org
[root@workstation playbook-basic]# curl serverc.lab.example.com www.westos.org
2.实施多个 play
- name: first play
hosts: servera.example.com
tasks:
- name: first task
yum:
name: httpd
status: present
- name: second task
service:
name: httpd
enabled: true
- name: second play
hosts: serverb.example.com
tasks:
- name: first task
service:
name: mariadb
enabled: true
练习 1
ansible-doc -l #列出所有模块
[root@workstation ~]# ansible-doc yum #列出 yum 的用法和示例
[root@workstation ~]# ansible-doc -s yum #终端中输出 yum 模块中各参数的用法
######PLAYBOOK 语法变化
###yaml 注释
#This is a YAML comment
some data # This is also a YAML comment
###yaml 字符串 this is a string
‘this is a string’
“this is a string”
练习
[root@workstation playbook-multi]# ls
ansible.cfg intranet.yml inventory
[root@workstation playbook-multi]# cat ansible.cfg [defaults]
inventory = ./inventory
[root@workstation playbook-multi]# cat inventory [web]
servera.lab.example.com
[root@workstation playbook-multi]# vim intranet.yml
- name: Enable intranet services
hosts: web
become: yes
tasks:
- name:lastest version of http and firewall installed #检测 httpd 是否安装和是否为最新版本
yum:
name:
- httpd
- firewalld
stase:latest
- name: test html page is configured #检测是否配置默认发布页面
copy:
content: "Welcome to westos!\n"
dest: /var/www/html/index.html
- name:firewalld enabled and running #检测防火墙是否开启并且处于enable状态
#检测 httpd 是否开启和设置开机启动
service:
name: firewalld
enabled: true
state: started
- name: firewalld permits access to httpd service #检测防火墙是否允许 httpd 服务访问
firewalld:
service: http
permanent: true
state: enabled
immediate: yes
- name: httpd enabled and running
service:
name: httpd
enabled: true
state: started
- name: Test intranet web server #在本机测试
hosts: localhost
become: no
tasks:
- name:connect to instranet web server #测试访问servera
uri:
uri:http://severa.lab.example.com
return_content:yes
status_code:200
[root@workstation playbook-multi]# ansible-playbook --syntax-check intranet.yml #检测语法
[root@workstation playbook-multi]# ansible-playbook -v intranet.yml #运行
可以看到返回了内容 welcome to westos 和状态码 200
4.管理变量和事实
将 playbook 中的某些值使用变量代替,从而
简化 playbook 的编写
管理变量
ansible 变量简介
变量可能包含下面这些值:
要创建的用户、
要安装的软件包、
要重启的服务、
要删除的文件、
要从互联网检索的文档
命名变量:
变量名称必须以字母开头,并且只能含有字母、数字和下划线
错误
web server
westos.file
1st file
正确:
web_server
remote_file
file1
#定义变量
三个范围级别
全局范围:从命令行或 ansible 配置设置的变量
play 范围:在 play 和相关结构中设置的变量
主机范围:由清单、事实收集或注册的任务,在主机组和个别主机上设置的变量
‘如果多个级别上定义了相同名称的变量,优先采用级别最高的变量,窄范围优先于广范围’
##playbook 中的变量
#在 playbook 中定义变量
1.常见方式:在 playbook 开头的 vars 块中:
- host: all vars:
user: student
home: /home/student
2.在外部文件定义 playbook 变量
- hosts: all
vars_files:
- vars/users.yml
在 users.yml 文件中写入
user: student
home: /home/student
#在 playbook 中使用变量
将变量名称放在花括号内即可
vars:
user: westos
tasks:
- name: Create user {{ user }}
user:
name: "{{ user }}"
‘注意:当变量用作开始一个值的第一元素时,必须使用引号’