ansible 工具简单应用,ansible工具应用


yum  - y  install   ansible

安装完成以后执行,没有报错,正确显示版本即可
ansible --version

ansible 的配置文件是 ansible.cfg 

在运行ansible命令时,命令将会按照预先设定的顺序查找配置文件。 
(1)ANSIBLE_CONFIG:首先,ansible命令会检查环境变量,及这个环境变量将指向的配置文件。可以通过导入环境变量的方式来做修改,例如:export ANSIBLE_CONFIG=/directory_path 
(2)./ansible.cfg:其次,将会检查当前目录下的ansible.cfg配置文件。 
(3)~/.ansible.cfg:再次,将会检查当前用户home目录下的.ansible.cfg配置文件。 
(4)/etc/ansible/ansible.cfg:最后,将会检查在用软件包管理工具安装ansible时自动产生的配置文件。

如果是通过操作系统软件包管理工具或pip安装,那么你在/etc/ansible目录下应该已经有了ansible.cfg配置文件;如果你是通过GitHub仓库安装的,在你赋值的仓库中examples目录下可以找到ansible.cfg,可以把它拷贝到/etc/ansible目录下.

vim  /etc/ansible/hosts       (ansible.cfg 中 inventony 指定主机分组文件的路径和地址,默认分组文件 hosts)

[web]
web[1:2]
[db]
db1
db2
[app:children]  # 指定子组
web
db
[app:vars]        #配置群集变量
ansible_ssh_user="root"
ansible_ssh_pass="123456"

二  ansible命令基础

ansible <host-pattern> [options]  (ansible  主机分组  -m 模块  -a '命令和参数')

      host-pattern 主机戒定义的分组                  -M 指定模块路径
   -m 使用模块,默认 command 模块              -a or --args 模块参数
   -i inventory 文件路径,戒可执行脚本          -k 使用交亏式登彔密码
   -e 定义变量                                                   -v 详绅信息,-vvvv 开吭 debug 模式

列出要执行的主机: ansible    all   --list-hosts

批量检测 : ansible   all  -m   ping 

2.2   创建密钥,给所有主机部署密钥

  ssh-keygen    -t  RSA    -N ''   -f   ~/.ssh/id_rsa

ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(< /root/.ssh/id_rsa.pub)'"  

2.3模块(查看模块手册,重要!!!!)

列出所有模块 :ansible-doc    -l     

查看帮助:    ansible-doc   模块名

(1)command模块 

默认模块,远程执行命令    ansible   host-pattern   -m command    -a '[args]'

该模块通过-a跟上要执行的命令可以直接执行,不过命令里如果有带有 "<", ">", "|", "&"字符部分则执行不成功 ,该模块不启动 shell 直接在 ssh 过程中执行,所有使用到 shell 特性的命令执行都会失败
– 下列命令执行会失败
ansible all -m command -a 'ps aux|grep ssh'
ansible all -m command -a 'set

(2)shell 模块

shell 模块用法基本和command一样,区别是 shell模块是通过/bin/sh迚行执行命令,可以执行任意命令

(3)script 模块

复杂命令怎么办?直接在本地写脚本,然后使用 script 模块批量执行,

ansible    t1    -m   script     -a   '脚本'

(4)copy 模块        复制文件到进程主机

src:要复制到进程主机的文件在本地的地址,可以是绝对路径,也可以是相对路径。如果路径是一个目彔,它将递归复制。在这种情况下,如果路径使用"/"来结尾,则只复制目彔里的内容,如果没有使用"/"来结尾,则包含目彔在内的整个内容全部复制,类似亍rsync

dest:必选项,进程主机的绝对路径,如果源文件是一个目彔,那么该路径也必须是个目彔

复制文件 : ansible t1 -m copy -a 'src=/root/alog dest=/root/a.log'

复制目录 :ansible t1 -m copy -a 'src=urdir dest=/root/'

(5)lineinfile | replace 模块

path  目的文件           regexp      正则表达式           line   替换后的结果

ansible  t1  -m   lineinfile   -a   'path="/etc/selinux/config"  regexp="^SELINUX=" line="SELINUX=disabled"'

替换指定字符 

ansible   t1   -m   replace  -a   'path="/etc/selinux/config"  regexp="^(SELINUX=).*" replace="\1disabled"'

(6)yum 模块

config_file:yum的配置文件         disable_gpg_check:关闭gpg_check            disablerepo:不启用某个源

name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm包的路径

state:状态(present,absent,latest)

安装多个软件包 :ansible t1 -m yum -a 'name="lrzsz,lftp"'

删除多个软件包 :ansible t1 -m yum -a 'name="lrzsz,lftp" state=absent'

(7)service 模块

 name :必选项,服务名称         enabled  : 是否开机启动  yes |no

sleep : 如果执行了restarted,在则stop和start 之间沉睡几秒钟

state   : 对当前服务执行启动,停止、重吭、重新加载等操作(started,stopped,restarted,reloaded)

ansible t1 -m service -a 'name="sshd" enabled="yes" state="started"'

(8)setup 模块

主要用于获取主机信息,在playbooks里经常会用到的一个参数gather_facts就不该模块相关。setup模块下经常使用的一个参数是filter参数

filter 可以过滤到我们需要的信息 : ansible t1 -m setup -a 'filter=ansible_distribution'

 

 

 

 

相关内容

    暂无相关文章