Ansible 安装以及常用命令,ansible常用命令


相关阅读: Ansible Playbook 介绍     

主机 yum 安装 ansible 客户机安装 libselinux-python
主机和客户机连接,配置密钥,并在主机文件中增加相应的客户机 ip 的分组。
一、安装 主机ip      192.168.32.158 ,需要在主机上生成密钥对 客户机ip    192.168.32.150
主机安装 ansible 即可 # yum install -y epel-release # yum install -y ansible

客户机安装扩展包 # yum install -y libselinux-python 避免后面出错,提前安装包,如果没装,在主机上执行命令的时候可能报错。 "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!" target 表示目标机器。

主机配置密钥 # ssh-keygen # cat /root/.ssh/id_rsa.pub

客户机拷贝公钥 # vim ~/.ssh/authorized_keys
#Ansible Master Key 添加注释,方便日后维护管理,特别是多个密钥的情况下 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAyJgg+0OUaSaA7cL27M2HXvft4WAdCRyZeymvxaw5OvhxfRU1HmI0aUw2ucVh3ZjctLWK/WPJ30nYNhbfUMO5AmADx7WCbqf/RqzJnm3KVomOE+tgmNU7a/f1utQxXdF6rQP3/U9ceTRs+6CvVsJKA5k9nLdju9b3mMMhjvN4ALMWhxRMSgU1tlIevQ6S4ZwODhFmjNwI3O+qFXuS4wH9wzIcVktWMB+eJ5tAM5PI1o2PPxQlrY4T2G5tiFywxP7GvTSj/pjbZxkiE/MJaW6XyxzWacWuGfbofUkKIm45WoybK5Rr33I4mGtx5ibxEISRn6mB/kk+g+0lcpY4KQY4RQ== root@Nagios 注意要修改 400 权限;以及关闭 selinux,如果出现能通讯,但是需要输入密码,有可能就是 selinux 没有关闭。 另外如果想和本地 127.0.0.1 进行 ssh 通信,需要将公钥拷贝到本机的 authorized_keys 当中。
修改配置文件 # vim /etc/ansible/hosts      [testhosts]      192.168.32.150      127.0.0.1 ansible 远程控制机器的时候,可以按不同的功能以及需求对机器进行分组,方便日后对机器的规范化管理。[ ] 中括号内是组名,可以自定义,比如 webserver,mailserver 等等,注意不要和原配置文件内的组重复,下面是机器的 ip 地址。 配置完成后,就可以对组进行操作。

                                                                                                             


命令的语法主要是调用模块, ansible [ groupname or ipaddress ] -m module -a " command or description "
常用模块 command shell copy cron yum  server
二、常用命令 语法: ansbile hosts -m module -a "command or description" 远程执行命令 语法: ansible [name of group] -m command -a "command" 中括号里面是在 /etc/ansible/hosts 里面定义的 group,也可以针对一个机器,写对应机器的 ip 地址即可。 第一个 command 是调用的命令模块,shell 模块也可以用来执行命令,但区别在于,shell 支持复杂命令,比如管道符,command 不支持,shell 使用范围大于 command。 第二个 command 是具体的命令 # ansible testhosts -m command -a 'w'                    # 单引号,双引号都可以 # ansible testhosts -m command -a "hostname" # ansible 192.168.32.150 -m command -a 'ls /tmp' # ansible 192.168.32.150 -m shell -a "hostname"           # 使用 shell 命令模块 # ansible 192.168.32.150 -m shell -a "cat /etc/passwd |grep root"     # shell 使用管道
拷贝目录或文件 语法: ansible [group or ip] -m copy -a "src=......  dest=...... owner=... group=... mode=..." 方括号里面跟group 或者 ip -m 后面跟模块名,这里用 copy 命令,则需要 copy 模块 src= 跟来源目录或者文件;dest= 跟目标文件或者目录;owner 和 group 指定属主和属组;mode= 指定属性。其中必须要有的是 src 和 dest ,其他的三个选项可以不用。 另外 copy 有个特性,和 rsync 类似,如果拷贝的两个文件内容一模一样,那就就不会进行拷贝的操作。 # ansible testhosts -m copy -a "src=/tmp/1/2/3 dest=/tmp/123 owner=root group=root mode=0644" # ansible 192.168.32.150 -m copy -a "src=/etc/passwd dest=/tmp/ansibletest owner=root group=root mode=0644"


远程执行脚本 语法: ansible [group or ip] -m shell -a "。。。。。.sh" 执行脚本调用的模块是 shell,shell 模块支持使用管道符的命令 如果要远程执行脚本,必须先拷贝脚本到远程,再去执行。不能在远程机器上执行本地的脚本。 要保证脚本有执行权限,可以在本地增加 x 权限,也可以在 copy 的时候指定权限。 # ansible testhosts -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"     拷贝本地shell脚本(要有x权限)到远程机器 # ansible 192.168.32.150 -m shell -a "/tmp/test.sh"


创建和删除计划任务 语法:ansible [group or ip] -m cron -a "name='....' job='.....' $time" -m 跟模版,使用的是 cron 模版 name= 指定计划任务的名字,方便日后管理 job= 指定具体的任务 $time 指定具体的执行时间,minute分钟,hour小时,day 月份,month 月份。weekday 0 或者 7 代表周末。 state= 选项用来指定 name 并删除 增加计划任务 # ansible testhosts -m cron -a "name='test cron' job='/bin/bash /tmp/test.sh' weekday=6"
# ansible testhosts -m cron -a "name='test n' job='/bin/bash /tmp/test.sh' weekday=6 minute=30 hour='*/8'"
# ansible testhosts -m cron -a "name='test cron' job='/bin/bash /tmp/test.sh' minute=30 hour='*/8' day='1,10,20' weekday=6"
删除计划任务 # ansible testhosts -m cron -a "name='test n' state=absent" # ansible testhosts -m cron -a "name='test cron' state=absent"


安装 rpm 包 -m 使用 yum 模块 state= installed  removed ,不加state选项默认是 installed # ansible testhosts -m yum -a "name=vim-enhanced" # ansible testhosts -m yum -a "name=wget" # ansible testhosts -m yum -a "name=wget state=removed"
NOTE:不建议在写 name 的时候使用匹配符号,比如 vim* 可以匹配 vim-enhanced 还会匹配 vim-filesystem,所以建议写完整的名字。
# ansible testhosts -m command -a "yum remove -y vim" 也可以使用 command 模块来安装 rpm 包,但是不规范,建议使用 yum 模块

管理服务 -m service 模块 name= 系统的服务,可以用 chkconfig --list 查看 enabled= 是否开机启动,yes no。 # ansible testhosts -m service -a "name=httpd state=started enabled=no" # ansible testhosts -m service -a "name=httpd state=stopped enabled=yes"

                                                                                                             


  三、Ansible 文档使用 # ansible-doc -l     列出所有模块 查看具体的模块使用方法 # ansible-doc service # ansible-doc cron # ansible-doc comman
                                                                                                             


相关内容

    暂无相关文章