ansible批量管理服务器,ansible-playbook

ansible的安装:

[root@ansible ~]#yum install ansible

[root@ansible ~]#ansible  --version      //验证ansible是否安装成功

 ansible配置文件查找顺序:
        – 首先检测ANSIBLE_CONFIG变量定义的配置文件
        – 其次检查当前目录下的 ./ansible.cfg 文件
        – 再次检查当前用户家目录下 ~/ansible.cfg 文件
        – 最后检查/etc/ansible/ansible.cfg文件
/etc/ansible/ansible.cfg是ansible的默认配置文件路径

ansible.cfg 配置文件:
        – inventory 定义托管主机地址配置文件路径名
        – inventory 指定的配置文件,写入远程主机的地址。

ansible.cfg 配置文件:
        – ssh 主机 key 验证配置参数
        – host_key_checking = False
        – 如果为 False,不需要输入 yes
       – 如果为 True,等待输入 yes

ansible命令基础:
• ansible  主机集合  -m  模块名称  -a  模块参数
– 主机集合 主机名或分组名,多个使用"逗号"分隔
– -m 模块名称,默认 command 模块
– -a or --args 模块参数
• 其他参数
– -i inventory文件路径,或可执行脚本
– -k 使用交互式登录密码
– -e 定义变量
– -v 显示详细信息

列出要执行的主机:
[root@ansible ~]#ansible all --list-hosts

批量检测主机:
[root@ansible ~]#ansible all -m ping -k      //<-k>带密码检测是否可以ssh远程

ansible 是通过 SSH 在远程执行命令的, ssh 远程执行命令必须要通过认证才行

[root@ansible ~]# ssh-keygen -N '' -f  /root/.ssh/id_rsa         //创建密钥对

[root@ansible ~]#ssh-copy-id  [目标主机IP地址]            //发送公钥到目标主机

inventory 参数说明:
• vars 变量定义,用于组名后面
– 例如
– [all:vars]
– ansible_ssh_private_key_file="/root/.ssh/key"
• children 子组定义,用于引用其他组名称
– 例如
– [app:children]
– web
– db

分组定义、范围定义样例:
– 子组定义
[app:children]
web
db
– 变量引用
[other]
cache ansible_ssh_port=222
[all:vars]                                 //指明所有的主机都用私有密钥ssh远程
ansible_ssh_private_key_file="/root/.ssh/key"

ansible的常用模块:

shell模块:(可以直接执行linux命令)

[root@ansible ~]#ansible web -m shell -a 'echo ${HOSTNAME}'        //<-m>模块,<-a>要执行的命令,这里必须用单引号

[root@ansible ~]#ansible cache -m shell -a 'touch testfile'

script模块:(在本地写脚本,然后使用script模块批量执行)

[root@ansible ~]#ansible t1 -m script -a 'urscript'            //urscript为shell脚本

注意:该脚本包含但不限于shell脚本,只要指定Sha-bang解释器的脚本都可运行

yum模块:(使用yum包管理器来管理软件包)

[root@ansible ~]#ansible db -m yum -a 'name="mariadb-server"  state=installed'

[root@ansible ~]#ansible cache -m yum -a 'name="lrzsz" state=removed'

service模块:(启动,停止服务,重新加载等)

[root@ansible ~]#ansible t1 -m service -a 'name="sshd" enabled="yes" state="started"'

copy模块:(将本地的文件拷贝到远程主机上)

– 复制文件
[root@ansible ~]#ansible all -m copy -a 'src=/etc/resolv.conf dest=/etc/resolv.conf'
– 复制目录
[root@ansible ~]#ansible all -m copy -a 'src=/etc/yum.repos.d/ dest=/etc/yum.repos.d/'

lineinfile模块:(类似sed的一种行编辑替换模块,替换的是整行)

[root@ansible ~]#ansible db -m lineinfile -a '  path="/etc/my.cnf"  regexp="^binlog-format"  line="binlog-format = row" '

replace模块:(类似sed的一种行编辑替换模块,替换的是指定的字符)

[root@ansible ~]#ansible db -m replace -a '  path="/etc/my.cnf"  regexp="row"  replace="mixed" '          //将row,替换为mixed

setup模块:(主要用于获取主机信息)

[root@ansible ~]#ansible cache -m setup -a 'filter=ansible_distribution'        //filter过滤所需信息

在使用ansible管理中,我们通常都是通过书写一个yaml文件来批量执行的:

[root@ansible ~]# ansible-doc  copy          //可以查看copy模块的参数,末行模式输入EXAMPLES,可以查看到案例,将案例拷贝到yaml文件中,稍作修改就可以.

ansible在实际管理过程中是不能直接改配置文件操作的,我们需要自己创建一个文件夹,将关键配置写进去,还要创建主机列表,名称根据配置文件中的来创建:

[root@ansible ~]# mkdir /opt/xxoo

[root@ansible ~]# cd /opt/xxoo

[root@ansible ~]# vim ansible.cfg

[defaults]

inventory  = /opt/xxoo/hosts                 //定义主机列表
host_key_checking = False                  //不让每次对主机操作的时候提示,应为它会每台都提示,我们需要关掉它

[root@ansible xxoo]#vim es.yml 
---
- hosts: es1,es2,es3
  remote_user: root
  tasks:
    - copy:
        src: local.repo
        dest: /etc/yum.repos.d/local.repo
        owner: root
        group: root
        mode: 0644

    - name: install the latest version of Apache
      yum:
        name: elasticsearch
        state: installed

    - name: install java-1.8.0-openjdk.x86_64
      yum:
        name: java-1.8.0-openjdk.x86_64
        state: installed
      tags: openjdk
      notify:
           restart es

    - template:
        src: elasticsearch.yml
        dest: /etc/elasticsearch/elasticsearch.yml
        owner: root
        group: root
        mode: 0644
      tags: es_change
      notify:
        - restart es

  handlers:
    - name: restart es
      service:
        name: elasticsearch
        state: restarted
        enabled: yes
用ansible同时管理多台服务器:

[root@ansible xxoo]#ansible-playbook es.yml

[root@ansible xxoo]#ansible-playbook es.yml  -C        //检测是否正确,命令并不会真正运行

我们在es.yml文件中定义了,tags和handlers,所以我们可以ansible只执行,特定的模块命令,以避免命令的重复,提高代码利用率:

[root@ansible xxoo]#ansible-playbook es.yml -t openjdk

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值