macos环境下ansible学习笔记

简介

ansible是一种自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

安装

本机环境:
macos high sierra 10.13
python 2.7.10

在macos下安装ansible主要有三种方法,建议采用源码安装的方法安装ansible。
1.brew安装(需要安装homebrew模块)

brew install ansible 

2.pip安装

pip install ansible

3.源码安装

git clone https://github.com/ansible/ansible.git
cd ansible
sudo python setup.py install 

安装完成后 用 ansible --version 命令 检查安装是否成功

tips:mac下安装后缺少 /etc/ansible 目录,需要手动添加,并从源码中将example目录中的文件拷贝到创建的 /etc/ansible 目录下。

主机免秘钥连接

生成本地ssh秘钥

ssh-keygen

将本地秘钥发给远端服务器(例:用户名为root ip地址为192.168.1.1)

ssh-copy-id root@192.168.1.1

出现Are you sure you want to continue connecting (yes/no)? 时,输入yes,之后输入目标服务器的密码,大功告成。

再次测试连接远端服务器,不需要密码连接上就表明成功

ansible的简单使用

当我们需要使用ansible直接向远端服务器发送指令时,一般需要以下步骤:

配置hosts文件,默认的hosts文件在 /etc/ansible 目录下

vim /etc/ansible/hosts

hosts文件基本格式(将需要控制的远端服务器ip地址写入,后面也可写入参数)

[hostname] #可以随意命名
192.168.1.2
192.168.1.3
[hostname1]#可以有多个分组
192.168.1.4
192.168.1.5

测试能否ping通

ansible all -m ping 
192.168.1.2 | SUCCESS => {
    "changed": false, 
    "failed": false, 
    "ping": "pong"
}
ansible命令参数及说明

《macos环境下ansible学习笔记》

ansible远程批量命令

远程执行命令的模块有command、shell、scripts、以及raw模块;

command模块

command模块为ansible默认模块,不指定-m参数时,使用的就是command模块;comand模块比较简单,常见的命令都可以使用,但其命令的执行不是通过shell执行的,所以,像这些 “<“, “>”, “|”, and “&”操作都不可以,当然,也就不支持管道;示例:显示远程路径:

ansible hostname  -a 'pwd'
192.168.1.2 | success | rc=0 >>
/home/root
192.168.1.3 | success | rc=0 >>
/home/root
10.6.143.37 | success | rc=0 >>
/home/root

缺点:不支持管道,就没法批量执行命令;

shell模块

使用shell模块,在远程命令通过/bin/sh来执行;所以,我们在终端输入的各种命令方式,都可以使用; 但是我们自己定义在.bashrc/.bash_profile中的环境变量shell模块由于没有加载,所以无法识别;如果需要使用自定义的环境变量,就需要在最开始,执行加载自定义脚本的语句;
对shell模块的使用可以分成两块:1) 如果待执行的语句少,可以直接写在一句话中:

ansible hostname  -a ". .bash_profile;ps -fe |grep sa_q" -m shell
  1. 如果在远程待执行的语句比较多,可写成一个脚本,通过copy模块传到远端,然后再执行;但这样就又涉及到两次ansible调用;对于这种需求,ansible已经为我们考虑到了,script模块就是干这事的;
script模块

使用scripts模块可以在本地写一个脚本,在远程服务器上执行(远程服务器不需要python环境):

ansible hostname -m script -a "/home/test/test.sh"

ansible playbook

playbook就是把上述在命令行的操作,以yml格式写在文件中来执行而已。复杂的playbook只是更多的命令行操作的集合。

例:将本机的某个文件拷贝到远端服务器

 vim copytest.yml   #创建yml执行文件
---
- hosts: hostname  # hosts中指定
  remote_user: root  # 如果和当前用户一样,则无需指定
  tasks:
      - name: copyfile   #建议每个任务指定一个name,方便出错时检查
        copy: src=/root/test.txt dest=root/test.txt  #  本地拷贝到远端

注解:
tasks定义了playbook中要执行的任务,包括任务名name以及具体的任务内容,tasks只有一个,而任务可以有多个.

---
- hosts: hostname  
  remote_user: root 
  tasks:
      - name: copyfile  
        copy: src=~  dest=~
      - name: testshell
        shell: 'touch test.txt'
      - name: testshell2     
        shell: 'touch test2.txt'      #遵循这样的格式

playbook执行方法:

sudo ansible-playbook copytest.yml    #主机名、执行命令都已在yml中指定了。

小测试

目标:通过连接git仓库的方法,远端服务器将本机的bash配置文件分发给其他指定的服务器,并执行生效。

1.创建yml文件编写task,并上传到git仓库

- hosts: host
  remote_user: root
  tasks:
    - name: copyprofile
      copy: src=/root/.bash_profile dest=/root/.bash_profile owner=root  mode=0600
    - name: runprofile
      shell: source /root/.bash_profile

2.编写shell脚本,交给远端服务器执行

# !bin/bash
mypath="User/root/itservercontrol"
if [ ! -d "$mypath"]; then
  git clone git@example.git #填写git仓库地址
    sudo ansible-playbook Change_Profile.yml
else
    cd itservercontrol
    git pull
    sudo ansible-playbook Change_Profile.yml
fi

3.向远端服务器发送本地编写的脚本,并执行

ansible hostname -m script -a "/root/itservertest.sh"

《macos环境下ansible学习笔记》

运行成功!

本笔记仅供参考,水平较低,还望包涵。

    原文作者:pjwpjwpjw
    原文地址: https://www.jianshu.com/p/873975e0abb8
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞