ad hoc fact, perform a simple command - a command. For complex command was playbook.
帮助文档:
列出ansible支持的模块:
- l:获取列表
- s module_name:获取指定模块的使用信息
看所有模块(A10,华为,docker,EC2,aws等等广大厂商设备)
[root@ansible-server ~]
查看模块使用信息,了解其功能:
[root@ansible-server ~]
Common Module
1. 远程复制备份模块:copy
模块参数详解:
src=:指定源文件路径
dest=:目标地址(拷贝到哪里)
owner:指定属主
group :指定属组
mode:指定权限, 可以以数字指定比如0644
backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes| no
[root@ansible-server ~]
123123
[root@ansible-server ~]
[root@ansible-server ~]
123123
234234
[root@ansible-server ~]
注释:如果文件没有变化,不会备份。只有文件内容不同,才会做备份。
登录被控制机器其中一台查看
[root@ansible-web1 ~]
[root@ansible-server ~]
移动被控制节点的文件
1. 远程复制备份模块:copy
模块参数详解:
src=:指定源文件路径
dest=:目标地址(拷贝到哪里)
owner:指定属主
group :指定属组
mode:指定权限, 可以以数字指定比如0644
backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes| no
[root@ansible-server ~]
123123
[root@ansible-server ~]
[root@ansible-server ~]
123123
234234
[root@ansible-server ~]
注释:如果文件没有变化,不会备份。只有文件内容不同,才会做备份。
登录被控制机器其中一台查看
[root@ansible-web1 ~]
[root@ansible-server ~]
移动被控制节点的文件
3. 服务管理service模块
[root@ansible-server ~]
[root@ansible-server ~]
[root@ansible-server ~]
[root@ansible-server ~]
[root@ansible-server ~]
4. 文件模块file
模块参数详解:
owner:修改属主
group :修改属组
mode:修改权限
path=:要修改文件的路径
recurse:递归的设置文件的属性,只对目录有效
yes:表示使用递归设置
state:
touch:创建一个新的空文件
directory:创建一个新的目录,当目录存在时不会进行修改
[root@ansible-server ~]
[root@ansible-server ~]
[root@ansible-server ~]
被控节点ansible- web2操作:
[root@ansible-web2 opt]
total 0
- rw- r-- r-- . 1 root root 0 Sep 12 09:41 haha2. txt
- rw- r-- r-- . 1 nginx root 0 Sep 12 09:41 haha. txt
[root@ansible-server ~]
被控节点操作:
[root@ansible-web2 opt]
total 0
- rw- r-- r-- . 1 nginx nginx 0 Sep 12 09:41 haha2. txt
- rw- r-- r-- . 1 nginx nginx 0 Sep 12 09:41 haha. txt
5. 收集信息模块setup
[root@ansible-server ~]
[root@ansible-server ~]
filter :过滤
group 模块参数:
name参数:必须参数,用于指定组名称。
state参数:用于指定组的状态,两个值可选,present(新增),absent,默认为 present,设置为absent 表示删除组。
gid参数:用于指定组的gid。如果不指定为随机
system参数:如果是yes为系统组。-- 可选
1. 创建多个play
[root@ansible ~]
[root@ansible ansible]
- hosts: webservers1
user: root
tasks:
- name: create a group
group : name=mygrp gid=2003 system=true
- name: create a user
user: name=tom group =mygrp system=true
- hosts: webservers2
user: root
tasks:
- name: install apache
yum: name=httpd state=latest
- name: start httpd service
service: name=httpd state=started
2. 条件执行when模块
先判断when条件是否成立
[root@ansible ansible]
[webservers1]
ansible- web1
ansible- web2
[root@ansible ansible]
- hosts: webservers1
user: root
tasks:
- name: use when
file: state=touch path=/ tmp/ when. txt
- name: insert data
shell: echo 123 >> / tmp/ when. txt
when: ansible_hostname == "ansible-web1"
3. 使用变量并不显示搜集主机相关信息
gather_facts参数:指定了在任务部分执行前,是否先执行setup模块获取主机相关信息,默认值为true,改成false之后在执行过程中不会搜集主机相关信息。
==========================================
[root@ansible an sible]
- hosts: ansible- web1
user: root
gather_facts: false
vars:
- user: "jack"
- src_path: "/root/a.txt"
- dest_path: "/mnt/"
tasks:
- name: create user
user: name={ { user } }
- name: copy file
copy : src={ { src_path } } dest={ { dest_path } }
[root@ansible ansible]
123
执行:
[root@ansible ansible]