一、Ansible远程执行的两种方式
方式1:临时执行(ad-hoc)
命令ansible hosts -m module_name -a args ,
其中hosts为主机地址组,在/etc/ansible/hosts文件中。如:
[windows]
192.168.10.9 ansible_ssh_user=admin ansible_sheel_type=cmd
module_name为模块名,模块名列表详见文档。
args为模块的参数,例如win_chocolatey模块的部分参数"name=noteplusplus version=7.6.3 state=present",
固定格式为""包裹,引号中参数名=参数值,各个参数间以空格分隔,若参数值存在引号,需要把参数值的引号改为单引号''。
一个完整的命令示例:ansible windows -m win_chocolatey -a "name=noteplusplus version=7.6.3 state=present" 。
方式2:Playbook执行(yaml配置)
所谓的playbook实际上是指☞命令ansible-playbook ansible.yaml的执行。
其中的ansible.yaml遵循yaml语法格式,详见Yaml语法和使用playbook。
下面是一个简单的playbook示例,文件名为winrm.yaml:
---
- hosts: windows
tasks:
- name: Ensure that WinRM is started when the system has settled
win_service:
name: WinRM
start_mode: delayed
整个playbook的用处为“确认WinRM服务是否设置成了开机自启”。
首行的---为yaml文件习惯,表示文件起始位置。
第二行的- hosts: windows中的host键windows值指☞对主机地址组windows执行接下来的tasks。
第三行表示接下来需要执行的任务标记。
第四行的name键表示任务的名称。
第五行的win_service表示模块名。
第六、七行为模块win_service的参数键值对。