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              #任务列表
相关推荐
Hy行者勇哥5 天前
用Cursor智能编写Ansible/Terraform脚本,打通CI/CD链路
ci/cd·ansible·terraform
芷栀夏7 天前
飞牛NAS怎么部署Immich?家庭照片自动备份与远程访问教程
服务器·nginx·ansible
悠然南风17 天前
Ansible常见模块总结及LDAP Role 编写与调试
ansible
祺风挽楠1 个月前
ansible编辑
网络·ansible
芳心粽伙饭1 个月前
Ansible课后作业
ansible
烁3471 个月前
Ansible初识
ansible
烁3471 个月前
Ansible安装部署调试
ansible
烁3471 个月前
Ansible命令
ansible
小义_1 个月前
【Ansible】(三)基础配置与连接设置
云原生·ansible
炸炸鱼.1 个月前
Ansible 企业级实战:Playbook 与 Roles 完全指南
网络·ansible