Ansible

Ansible详解

1.Ansible配置

  • ansible简介

    ansible是目前最受欢迎的自动化运维工具,基于Python开发,实现批量系统配置、批量程序部署、批量运行命令等

  • 安装方式

    使用yum安装

    julia 复制代码
    [root@localhost ~]# yum -y install epel-release
    [root@localhost ~]# yum -y install ansible
  • 安装目录(yum安装)

    julia 复制代码
    #配置文件目录:/etc/ansible/
    #执行文件目录:/usr/bin/
    #Lib库依赖目录:/usr/lib/pythonX.X/site-packages/ansible/
    #Help文档目录::/usr/share/doc/ansible-X.X.X/
    #Man文件目录:/usr/share/man/man1/
  • 配置文件更改

    julia 复制代码
    #在/etc/ansible/ansible.cfg
    [root@localhost ~]# vim /etc/ansible/ansible.cfg +318
    ost_key_checking=False
    #在/etc/ansible/hosts
    添加控制的IP地址与登录信息
    [mysql]
    10.36.178.66   ansible_ssh_root="root"  ansible_ssh_pass="123"
    10.36.178.84   ansible_ssh_root="root"  ansible_ssh_pass="123"        

2.Ansible常用命令

  • ansible-doc命令

    julia 复制代码
    ansible-doc -l                #获取全部模块的信息
    ansible-doc -s $MOD_name      #获取指定模块的使用帮助

3.Ansible常用模块

  • 主机连通测试

    julia 复制代码
    [root@localhost ~]# ansible $ip -m ping
  • command模块

    这个模块可以直接在远程主机上执行命令,并将结果返回本主机

    julia 复制代码
    [root@localhost ~]# ansible $ip -m command -a 'ss -ntlp'
  • shell模块

    支持shell的各种功能,例如管等

    julia 复制代码
    [root@localhost ~]# ansible $ip -m shell -a 'cat /etc/passwd |grep root'
  • copy模块

    将文件复制到远程主机,同时支持给定内容生成文件和修改权限等

    julia 复制代码
    #复制文件
    [root@localhost ~]# ansible $ip -m copy -a 'src=hello dest=/opt/hello'
    #给定内容生成文件,并指定权限
    [root@localhost ~]# ansible $ip -m copy -a 'content=hello\n dest=/opt/test.txt mode=777'
  • file模块

    该模块主要用与设置文件的属性,比如创建文件,创建链接文件,删除文件等

    julia 复制代码
    #创建目录
    [root@localhost ~]# ansible $ip -m file -a 'path=/opt/name state=directory'
    #创建链接文件
    [root@localhost ~]# ansible $ip -m file -a 'path=/opt/test.jpg src=/tmp/test1.jpg state=link'
    #删除文件
    [root@localhost ~]# ansible $ip -m file -a 'path=/opt/test.txt state=absent'
  • yum模块

    该模块主要用于软件安装

    julia 复制代码
    [root@localhost ~]# ansible $ip yum -a 'name=nginx state=present'
  • service模块

    该模块用于服务程序管理

    julia 复制代码
    #开启服务并设置自动启动
    [root@localhost ~]# ansible $ip -m service -a 'name=nginx state=started enable=true'
    #关闭服务
    [root@localhost ~]# ansible $ip -m service -a 'name=nginx state=stopped'

4.Ansible Playbook

  • 执行一些简单的任务,就像shell脚本

  • 格式

    playbook的格式严格,需要注意规范空格

    julia 复制代码
    #文件名需以yml、yaml结尾
    [root@localhost ~]# vim nginx.yml
    ---
    #安装nginx服务
    - host: nginx
      remote_user: root
      gather_facts: no
      tasks:
    	- name: install nginx
    	  yum: name=nginx state=present

    核心元素

    julia 复制代码
    hosts              #目标主机
    remote_user        #在远程主机执行任务的用户
    tasks              #任务列表
相关推荐
likeyou~coucou2 天前
自动化之ansible(二)
运维·自动化·ansible
明阳mark2 天前
Ansible 学习笔记
笔记·学习·ansible
DC_BLOG2 天前
Linux-Ansible模块进阶
linux·运维·服务器·ansible
DC_BLOG2 天前
Linux-Ansible命令
linux·运维·服务器·ansible
李匠20245 天前
云计算架构学习之Ansible-playbook实战、Ansible-流程控制、Ansible-字典循环-roles角色
学习·云计算·ansible
fenglei20208 天前
Ansible批量配置服务器免密登录步骤详解
运维·git·github·ansible
敖光 SRE8 天前
自动化运维之ansible快速入门
运维·自动化·ansible
大新新大浩浩8 天前
ceph部署-14版本(nautilus)-使用ceph-ansible部署实验记录
ceph·ansible
罗狮粉 9911 天前
AlmaLinux使用Ansible自动部署k8s集群
容器·kubernetes·ansible
Lz__Heng11 天前
Ansible 主机清单语法
ansible