1.模块
-m 使用模块 默认的是command模块 不过功能没有shell强大 所以我们可以使用shell模块
1.1使用
我们可以在命令行加 -m shell使用shell 模块
-m shell
#也可以在配置文件里面可以更改
vim /etc/ansible/ansible.cfg
#更改前
#module_name = command
#更改后
module_name = shell
2.playbook剧本
-C 进行检查
ansible-playbook -C $.yml
3.角色
角色的话 就是 将每个模块建立各自的文件夹,每个文件夹里面 ,放单一的任务
常用文件夹简介
文件 | 描述 |
---|---|
tasks | 任务目录 |
handlers | 触发器目录 |
templates | 模版目录:一般是配置文件 |
vars | 变量目录 |
files | 文件目录 |
[root@c7-47 ansible]# pwd
/root/ansible
[root@c7-47 ansible]# ls #里面存放的是剧本文件
app_role.yml httpd_role.yml memcache_role.yml nginx_roles.yml roles some_role.yml
[root@c7-47 ansible]# tree
.
├── app_role.yml
├── httpd_role.yml
├── memcache_role.yml
├── nginx_roles.yml
├── roles #角色目录
│ ├── app #app角色目录
│ │ ├── files #文件目录
│ │ │ └── vhosts.conf
│ │ ├── handlers #触发器目录
│ │ │ └── main.yml
│ │ ├── tasks #任务目录
│ │ │ ├── copy.yml
│ │ │ ├── group.yml
│ │ │ ├── main.yml #定义任务执行的顺序
│ │ │ ├── start.yml
│ │ │ ├── template.yml
│ │ │ ├── user.yml
│ │ │ └── yum.yml
│ │ ├── templates #版本目录
│ │ │ └── httpd.conf.j2 #配置文件 一般是j2格式
│ │ └── vars #变量文件夹
│ │ └── main.yml
│ ├── httpd
│ │ ├── files
│ │ │ └── httpd.conf
│ │ └── tasks
│ │ ├── copy.yml
│ │ ├── main.yml
│ │ └── user.yml
│ ├── memcached
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── tasks
│ │ │ ├── main.yml
│ │ │ ├── start.yml
│ │ │ ├── template.yml
│ │ │ └── yum.yml
│ │ └── templates
│ │ └── memcached.j2
│ ├── mysql
│ └── nginx
│ ├── tasks
│ │ ├── group.yml
│ │ ├── main.yml
│ │ ├── restart.yml
│ │ ├── start.yml
│ │ ├── template.yml
│ │ ├── user.yml
│ │ └── yum.yml
│ └── templates
│ └── nginx.conf.j2
└── some_role.yml
主要说一下目录的格式
一般目录下 都要有main.yml文件
tasks任务目录下
1.特殊格式
[root@c7-47 ansible]# cat roles/app/tasks/main.yml
- include: group.yml
- include: user.yml
- include: yum.yml
- include: template.yml
- include: copy.yml
- include: start.yml
2.template
[root@c7-47 tasks]# cat template.yml
- name: copy conf
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify: restart service #一般会写notify 调用的是handlers目录下的main.yml文件
[root@c7-47 tasks]# cat ../handlers/main.yml
- name: restart service
service: name=httpd state=restarted
3.变量目录
[root@c7-47 tasks]# cat ../vars/main.yml
username: app
groupname: app
4.playbook
[root@c7-47 ansible]# cat app_role.yml
- hosts: webserver
remote_user: root
roles:
- app #调用剧本