1、ping模块
ansible -i inventories/production shanghai -m ping
ansible-playbook test.yml -i inventories/production -l parent.child -m ping
2、查询任务标签功能
ansible-playbook test.yml -i inventories/production -l node1 --list-tasks
查询到task后,执行需要执行的指定任务
3、管理机器的层次划分
inventories/production 管理文件
[shanghai:children]
shanghai_loadbalance
shanghai_nginx
shanghai_services
[all:vars]
env=test
执行如下命令探测
ansible-playbook test.yml -i inventories/production -l shanghai_loadbalance -m ping
4、编写运行脚本
cat test.yml
---
- name: Deploy Test Service
hosts: all
become: true
gather_facts: false
vars_files: "vars/{{ env }}.yml"
roles:
- role: test
tags: test
- role: test1
tags: test1
- role: test2
tags: test2
通用配置
ansible.cfg
配置秘钥:
帮助文档
ansible-doc 模块命
ansible-doc -s 模块命
匹配 主机列表
ansible 1.1.1.1, 1.2.2.2 -m ping
anisble webservers:dbservers -m ping
anisble webservers:&dbservers -m ping
ansible 参数介绍:
-v -vv -vvv 打印详情
-b 使用sudo执行
--list-hosts 显示主机列表
anisble webservers:&dbservers --list-hosts
模块对比
commnad默认自带,不需要 -m 指定。
shell模块接收特殊字符,比commnad更灵活,如$, |等。
script支持脚本直接执行
copy支持文件拷贝