Ansible学习笔记——playbook:单个playbook运行测试

playbook的格式

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
4:ansible的自带模块中,command模块和shell模块无需使用key=value格式

语法格式

必须以—开头
一个play必须以破折号空格作为开头 - name:
每个play之间需要用空格作为分隔,不可以用tab标记
每个task需要缩进对齐
vim编辑器的设置:在$home/vimrc文件中添加:
autocmd FileType yaml setlocal ai ts=2 sw=2 et

编辑yaml文件时,使用tab间可以执行双空格缩进。

yaml注释:左侧输入#,表示注释本行
yaml字符串:‘string’ “string” string 都可以表示一个字符串
yaml字符串换行:使用 | 可以保留换行符,可以多行输出; 使用 > 可以直接多行输出,不保留换行符

yaml列表建议使用如下方式,不建议使用 host: [servera,serverb,serverc]

---
- name: Configure important user consistently
  hosts:
    - test
    - web

语法验证:ansible-playbook --syntax-check webserver.yml
可对yml文件进行语法验证,对于有问题的地方进行简单提示

空运行:-C参数 会报告改操作对主机的更改内容,但不会实际操作主机。

[student@workstation wangxc]$ cat playbook-test.yaml 
---
-name: Configure important user consistently
 hosts: servera.lab.example.com
 tasks:
   -name: newbie exists with UID 4000
    user:
      name: newbie
      uid: 4000
      state: present

运行一个简单的playbook

本示例中只有一个play,一个play对应只有一个task。
实现的内容为,到servera中创建一个uid为4000的用户 newbie

[student@workstation wangxc]$ cat playbook-test.yaml 
---
- name: Configure important user consistently
  hosts: servera.lab.example.com
  tasks:
   - name: newbie exists with UID 4000
     user:
       name: newbie
       uid: 4000
       state: present

运行结果:

[student@workstation wangxc]$ ansible-playbook playbook-test.yaml 

PLAY [Configure important user consistently] *****************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************
ok: [servera.lab.example.com]

TASK [newbie exists with UID 4000] ***************************************************************************************************
changed: [servera.lab.example.com]

PLAY RECAP ***************************************************************************************************************************
servera.lab.example.com    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

示例:在servera中安装web服务器

[student@workstation wangxc]$ cat webserver.yml 
---
- name: play to setup web server
  hosts: servera.lab.example.com
  tasks:
    - name: latest httpd version installed
      yum: 
        name: httpd
        state: latest

[student@workstation wangxc]$ ansible-playbook webserver.yml 

PLAY [play to setup web server] ******************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************
ok: [servera.lab.example.com]

TASK [latest httpd version installed] ************************************************************************************************
changed: [servera.lab.example.com]

PLAY RECAP ***************************************************************************************************************************
servera.lab.example.com    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

playbook执行详细的输出结果

在这里插入图片描述

playbook实验练习

环境背景

[student@workstation playbook-basic]$ pwd
/home/student/playbook-basic
[student@workstation playbook-basic]$ ll
total 8
-rw-r--r--. 1 student student 144 Mar  3 13:53 ansible.cfg
drwxr-xr-x. 2 student student  24 Mar  3 13:51 files
-rw-r--r--. 1 student student  55 Mar  3 13:53 inventory
[student@workstation playbook-basic]$ cat ansible.cfg 
[defaults]
inventory=inventory
remote_user=devops

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
[student@workstation playbook-basic]$ cat inventory 
[web]
serverc.lab.example.com
serverd.lab.example.com

实验需求:
在这里插入图片描述

编写并检查语法:

[student@workstation playbook-basic]$ cat site.yml 
---
- name: Install ansd start Apache HTTPD
  hosts: web
  tasks:
    - name: httpd package id present
      yum:
        name: httpd
        state: present
    - name: correct index.html id present
      copy:
        src: files/index.html
        dest: /var/www/html/index.html
    - name: httpd id started
      service:
        name: httpd
        state: started
        enabled: true

[student@workstation playbook-basic]$ ansible-playbook --syntax-check site.yml 

playbook: site.yml

运行结果:

[student@workstation playbook-basic]$ ansible-playbook site.yml 

PLAY [Install ansd start Apache HTTPD] ***********************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************
ok: [serverd.lab.example.com]
ok: [serverc.lab.example.com]

TASK [httpd package id present] ******************************************************************************************************
changed: [serverd.lab.example.com]
changed: [serverc.lab.example.com]

TASK [correct index.html id present] *************************************************************************************************
changed: [serverd.lab.example.com]
changed: [serverc.lab.example.com]

TASK [httpd id started] **************************************************************************************************************
changed: [serverc.lab.example.com]
changed: [serverd.lab.example.com]

PLAY RECAP ***************************************************************************************************************************
serverc.lab.example.com    : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
serverd.lab.example.com    : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

目前已完成运行,所有机器输出为 changed。为检查结果,可再次运行次playbook。可以看到结果没有任何更改,表示已经成功安装好。

[student@workstation playbook-basic]$ ansible-playbook site.yml 

PLAY [Install ansd start Apache HTTPD] ***********************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************
ok: [serverc.lab.example.com]
ok: [serverd.lab.example.com]

TASK [httpd package id present] ******************************************************************************************************
ok: [serverd.lab.example.com]
ok: [serverc.lab.example.com]

TASK [correct index.html id present] *************************************************************************************************
ok: [serverd.lab.example.com]
ok: [serverc.lab.example.com]

TASK [httpd id started] **************************************************************************************************************
ok: [serverd.lab.example.com]
ok: [serverc.lab.example.com]

PLAY RECAP ***************************************************************************************************************************
serverc.lab.example.com    : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
serverd.lab.example.com    : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

验证:访问serverc和serverd,可以看到已经部署了index.html

[student@workstation playbook-basic]$ curl serverc.lab.example.com
This is a test page.
[student@workstation playbook-basic]$ curl serverd.lab.example.com
This is a test page.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值