您的位置:首页 > 运维架构 > Linux

Linux下部署ansible简单介绍和playbook的使用

2020-06-07 06:13 267 查看

   简介:Ansible 是一个配置管理和应用部署工具,功能类似于目前业界的配置管理工具 Chef,Puppet,Saltstack。Ansible 是通过 Python 语言开发。Ansible 平台由 Michael DeHaan 创建,他同时也是知名软件 Cobbler 与 Func 的作者。Ansible 的第一个版本发布于 2012 年 2 月。Ansible 默认通过 SSH 协议管理机器,所以 Ansible 不需要安装客户端程序在服务器上。您只需要将 Ansible 安装在一台服务器,在 Ansible 安装完后,您就可以去管理控制其它服务器。不需要为它配置数据库,Ansible 不会以 daemons 方式来启动或保持运行状态。Ansible 可以实现以下目标:

      自动化部署应用
      自动化管理配置
      自动化的持续交付
      自动化的(AWS)云服务管理。
   Ansible 是一个模型驱动的配置管理器,支持多节点发布、远程任务执行。默认使用 SSH 进行远程连接。无需在被管理节点上安装附加软件,可使用各种编程语言进行扩展。管理端支持local 、ssh、zeromq 三种方式连接被管理端,默认使用基于ssh的连接。

一、安装ansible

yum install ansible -y
cat /etc/ansible/hosts

[test]
192.168.1.75
192.168.1.120
生成密匙下发到节点
ssh-keygen -t dsa
ssh-copy-id -i ~/.ssh/id_dsa.pub root@192.168.1.75
测试ssh的连接是否成功
ssh 192.168.1.75
ssh 192.168.1.235 “free -m”

二、ansible模块简单的使用介绍

对服务的管理,或者 command 命令模块,:
ansible ax -a ‘supervisorctl status’
lineinfile文件操作:
追加:
ansible ax -m lineinfile -a “dest=/test/test.txt line=‘he’”
替换添加行
ansible ax -m lineinfile -a “dest=/test/test.txt regexp=‘he’ line=‘x’”
删除
ansible ax -m lineinfile -a “dest=/root/test.txt regexp=‘aa(.)’ state=absent"
在某一行后面插入一行,insertafter
插入:
ansible all -m lineinfile -a "dest=/root/test.txt insertbefore='aa(.)’ line=‘eeee’”
内容修改:
ansible all -m lineinfile -a “dest=/root/test.txt regexp=‘bbb’ line=‘bbbbbbb’”
copy模块本地文件复制到远程主机:
ansible ax -m copy -a “src=/root/test1.txt dest=/test”
shell模块:
ansible ax -m shell -a ‘echo “x”’
cron定时任务模块:
1 backup:对远程主机上的原任务计划内容修改之前做备份
2 cron_file:如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划
3 day:日(1-31,,/2,……)
4 hour:小时(0-23,,/2,……)
5 minute:分钟(0-59,,/2,……)
6 month:月(1-12,,/2,……)
7 weekday:周(0-7,,……)
8 job:要执行的任务,依赖于state=present
9 name:该任务的描述
10 special_time:指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly
11 state:确认该任务计划是创建还是删除
12 user:以哪个用户的身份执行
添加定时任务:ansible ax -m cron -a 'minute="/10" job="/bin/echo hello" name=“test cron job” state=“present”’
1、定时设置指定值的写入即可,没有设置的可以不写(默认是*)
2、name必须写
3、state有两个状态:present(添加(默认值))or absent(移除)
file模块:
创建文件目录( touch/directory):
ansible ax -m file -a “path=/test/test2.txt state=touch”
创建软连接: ansible ax -m file -a “path=/test/linkfile1 state=link src=/test/testfile1”
创建硬链接: ansible ax -m file -a “path=/test/hardfile2 state=hard src=/test/testfile2”
主组权限:ansible ax -m file -a “path=/test/abd state=directory owner=es group=es recurse=yes”
ansible ax -m file -a "path=/test/abb state=touch mode=0644
ansible ax -m file -a “path=/test/abb mode=0644”
ansible ax -m file -a “path=/test/binfile mode=4700”
ansible ax -m file -a “path=/test/abb state=directory mode=0644”
force:需要在两种情况下强制创建软链接,一种是源文件不存在但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
group:定义文件/目录的属组
mode:定义文件/目录的权限
owner:定义文件/目录的属主
path:必选项,定义文件/目录的路径
recurse:递归的设置文件的属性,只对目录有效
src:要被链接的源文件的路径,只应用于state=link的情况
dest:被链接到的路径,只应用于state=link的情况
state: directory:如果目录不存在,创建目录 file:即使文件不存在,也不会被创建 link:创建软链接 hard:创建硬链接 touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间 absent:删除目录、文件或者取消链接文件
yum模块:
config_file:yum的配置文件
disable_gpg_check:关闭gpg_check
disablerepo:不启用某个源
enablerepo:启用某个源
name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm包的路径
state:状态(present安装,absent卸载,latest最新)
shell模块:
ansible ax -m shell -a “ps -ef|grep httpd”
script模块:
script只能执行脚本,不能调用其他指令,但是script执行的是存放在ansbile管理机上的脚本在远程主机执行
ansible ax -m script -a ‘/test/script.sh’

三、编写playbook文件
复制路径下多个文件到目标主机
vi test.ymal

   - hosts: test
    tasks:
       - name: copy file
          copy:
          src: ‘{{ item.src }}’
          dest: ‘{{ item.dest }}’
          owner: root
          group: root
          mode: 644
       with_items:
           - { src: ‘/home/test–7.3.0.tar.gz’,dest: ‘/home/test-7.3.0.tar.gz’ }
            - { src: ‘/home/test–7.3.1tar.gz’,dest: ‘/home/test–7.3.1.tar.gz’ }

复制脚本文件到远程主机并执行
vi esuptate.ymal

   - hosts: test
       tasks:
          - name: copy file
             copy:
                src: /home/esupdate.sh
                dest: /home/esupdate.sh
                owner: root
                group: root
                mode: 755
          - name: demo shell
                shell: sh /home/esupdate.sh

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: