Centos7 Ansible

Ansible

1) 安装Ansible

# ansible的安装依赖于epel,参照centos7 yum源的更换
yum install ansible -y 

2) 配置ansible

cd /etc/ansible
# ls 查看内部 ansible.cfg hosts roles
#备份hosts文件
cp hosts{,.bak}

# 添加inventory 主机清单
vim /etc/ansible/hosts
[webservers]
192.168.122.150
[dbservers]
192.168.122.157

3)ansible命令的使用方法

如何查看ansible模块
ansible-doc -l 
查看具体模块的帮忙内容
ansible-doc -s yum 

copy 
cron
command

example:
      1)默认执行的就是:command
      ansible webservers -m command -a 'date'
      ansible dbservers -m command -a 'tail -2 /etc/passwd'
      2)cron
      ansible webservers -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job" state="present' #创建任务 state
      ansible webservers -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job" state="absent' #移除任务
      3)user
      ansible all -m user -a 'name="user1"' 默认state="present" 创建
      ansible all -m user -a 'name="user1" state="absent"' 移除 
      ansible all -m user -a 'name=mysql uid=306 system=yes group=mysql'
      4) group
      ansible all -m group -a 'name=msyql gid=306 system=yes'
      5) 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="hi my coming in" dest=/tmp/test.ansible'
      6)file
      设定文件属性
      ansible dbservers -m file -a 'owner=mysql mode=644 path=/tmp/fstab.ansible'
      创建链接
      ansible dbservers -m file -a 'src=/tmp/fstab.ansible path=/tmp/fstab.link state=link'
      7)ping
      测试指定主机是否可以连通
      8)service
      9)shell
      运行命令的模块类似command 但是当用到管道等复杂命令时采用shell
      ansible all -m shell -a 'echo user1 | passwd --stdin user1'
      10) script
      运行本地脚本复制到远程主机并运行
      ansible all -m script -a 'test.sh'  # 运行命令时,必须到该sh的目录下,不可用绝对路径
      11)yum
      ansible all -m yum -a 'name=zsh'
      ansible all -m yum -a 'name=zsh state=absent' # 卸载
      rpm -q zsh # 查看下安装与否
      12)setup
      收集远程主机的facts
      每个被管理节点在接收并运行管理命令之前,会将自己主机相关信息如操作系统,IP地址等报告给远程的ansible主机
      ansible all -m setup

4) playbook

# ansible 中使用YAML的基础元素:
变量, Inventory 条件测试 迭代

playbook的组成结构:
Inventory
modules
ad hoc commands
Playbooks:
      Tasks
      variables
      templeate
      handles 处理器 
      roles 


- 


roles:
1) 目录名同角色名
2)目录结构有固定格式:
files:静态文件

  3)
posted @ 2020-09-28 00:47  pigeast  阅读(158)  评论(0编辑  收藏  举报