1.Ansible服务的剧本(playbook)文件
采用YAML语言编写,具有强制性的格式规范,它通过空格将不同信息分组,YAML文件的开头需要先写3个减号(---),多个分组的信息需要间隔一致才能执行,而且上下也要对齐,后缀名一般为.yml。剧本文件在执行后,会在屏幕上输出运行界面,内容会根据工作的不同而变化。在运行界面中,绿色表示成功,黄色表示执行成功并进行了修改,而红色则表示执行失败。
剧本文件的结构由4部分组成,分别是target、variable、task、handler,其作用如下。
target:用于定义要执行剧本的主机范围。
variable:用于定义剧本执行时要用到的变量。
task:用于定义将在远程主机上执行的任务列表。
handler:用于定义执行完成后需要调用的后续任务。
[root@dsrw ~]# vim packages.yml
---
- name: 安装软件包
hosts: test
tasks:
- name: one
yum:
name: mariadb
state: latest
2.命令运行这个剧本文件
[root@dsrw ~]# ansible-playbook packages.yml
[root@dsrw ~]# ansible-playbook packages.yml
PLAY [安装软件包] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.1.12]
TASK [one] *********************************************************************
changed: [192.168.1.12]
PLAY RECAP *********************************************************************
192.168.1.12 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@dsrw ~]# vim packages1.yml
---
- name: 安装软件包
hosts: test
tasks:
- name: ping test
ping:
- name: mkdir dir
shell:
cmd: mkdir /dsrw
[root@dsrw ~]# ansible-playbook packages1.yml
PLAY [安装软件包] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.1.12]
TASK [ping test] ***************************************************************
ok: [192.168.1.12]
TASK [mkdir dir] ***************************************************************
[WARNING]: Consider using the file module with state=directory rather than
running 'mkdir'. If you need to use command because file is insufficient you
can add 'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
changed: [192.168.1.12]
PLAY RECAP *********************************************************************
192.168.1.12 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
请登录后查看评论内容