安装: 依赖于 epel 源
yum install ansible -y
配置文件:/etc/ansible/ansible.cfg
Invertoory: /etc/ansible/hosts
如何查看模块帮助:
- ansible-doc -l
- ansible-doc -s MODULE_NAME`
ansible 命令应用基础:
语法: ansible <host-pattern> [options]
-f forks: 启动的并发线程数
-m module_name: 要使用的模块
-a args: 模块特有的参数
command: 命令模块, 默认模块, 用于在远程执行命令
- ansible all -a 'date'
- cron:
- */10 * * * */bin/echo hello Linux
- state:
- present:
- #安装 ?ansible all -m cron -a 'minute="*/10"job="/bin/echo hello linux"name="test cron_job"state=present'
- absent:
- #移除 ?ansible all -m cron -a 'minute="*/10"job="/bin/echo hello linux"name="test cron_job"state=absent'
- ?ansible all -a "crontab -l"
- user:
- ?
- # 创建用户
- ? ansible all -m user -a 'name="user1"'
- ? cat /etc/passwd # 最后一行
- ?
- # 删除用户
- ? ansible all -m user -a 'name="user1"state="absent"'
- group:
- ?
- # 创建组
- ?
- ? ansible all -m group -a 'name=mysql gid=306 system=yes'
- ? cat /etc/group
- ?
- # 删除组
- ?
- ? ansible all -m group -a 'name=mysql gid=306 system=yes state=absent'
- copy:
- ?
- # 复制文件 src 定义本地源文件路径 dest 定义远程主机路径 (绝对路径)
- ?
- ? ansible all -m copy -a 'src=/etc/fstab dest=/tmp/fstab.ansible owner=root mode=640'
- ?
- # content 生成内容的方式进行远程复制
- ?
- ? ansible all -m copy -a 'content=" 你好崔建忠 \ n"dest=/tmp/test.ansible'
- file: # 设定文件属性
- ?
- # 改变文件的属主属组
- ?
- ? ansible all -m file -a 'owner=mysql group=mysql mode=644 path=/tmp/fstab.ansible'
- ?
- # link 创建文字符号链接
- ?
- ? ansible all -m file -a 'path=/tmp/fstab.link src=/tmp/fstab.ansible state=link'
- ping:
- ?
- # 测试主机是否能链接
- ?
- ? ansible all -m ping
- ?
- ?
- service:
- ?
- # 设置开机自启动, 并且现在启动起来
- ?
- ? ansible all -m service -a 'enabled=true name=httpd state=started'
- ?
- # 关闭某一个服务
- ?
- ? ansible all -m service -a 'enabled=true name=httpd state=stopped'
- ?
shell: 执行 shell 命令
- ? ansible all -m shell -a 'hello ansible|passwd --stdin user1'
- ?
script: 将本地脚本复制到远程主机并运行
- ? ansible all -m script -a '/root/test.sh'
- ?
yum: 安装程序包
name=: 指明要安装的程序包, 可以带上版本号
state=: present,latest 表示安装, absent 表示卸载
?
setup: 收集远程主机的 facts
每个被管理节点在接收并且运行管理命令之前, 会将自己主机相关的信息, 如操作系统版本, ip 地址等报告给远程的 ansible 主机;
来源: http://www.bubuko.com/infodetail-2948784.html