剧本内容
文件名:install.yaml
- hosts: hello
connection: ssh
remote_user: root
tasks:
- name: install git
yum: name=git state=installed
剧本检查
ansible-playbook --syntax-check install.yaml
剧本执行
ansible-playbook -C install.yaml
执行效果
root@debian:~# ansible-playbook -C install.yaml
PLAY [hello] *****************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************
ok: [server1]
TASK [install git] ***********************************************************************************************************************************************************************************************
[DEPRECATION WARNING]: State 'installed' is deprecated. Using state 'present' instead.. This feature will be removed in version 2.9. Deprecation warnings can be disabled by setting deprecation_warnings=False
in ansible.cfg.
ok: [server1]
PLAY RECAP *******************************************************************************************************************************************************************************************************
server1 : ok=2 changed=0 unreachable=0 failed=0
剧本解析
- hosts: hello
connection: ssh
remote_user: root
tasks:
- name: install git
yum: name=git state=installed
内容 | 作用 |
---|---|
- hosts: hello | 指定要操作的主机 |
connection: ssh | 连接的方式 |
remote_user: root | 使用的用户 |
tasks: | 定义一个任务 |
- name: install git | 这个子任务的名称 |
yum: name=git state=installed | 使用的模块(yum)及参数 |
注意事项
YAML采用变态的空格来做缩进,且对格式非常严格,所以如果格式不对请检查你的空格是否多了或者少了。
几个技巧
首行必须使用-
来定义,然后一个空格加主线(hosts),以此定义被操作的对象
第二行必须使用两个空格去定义(同等级都是这样)
当遇到有子任务的主线(tasks),需要以冒号结尾,然后下一行参照第一行的格式继续