ansibles的hoc命令行

 

ansible

Ansible:Ansible的核心程序
Python
Paramiko
Jiaja2
Yam1

Host Lnventory:记录了每一个由Ansible管理的主机信息,信息包括ssh端口,root帐号密码,ip地址等等。可以通过file来加载,可以通过CMDB加载
Playbooks:YAML格式文件,多个任务定义在一个文件中,使用时可以统一调用,“剧本”用来定义那些主机需要调用那些模块来完成的功能.
Core Modules:Ansible执行任何管理任务都不是由Ansible自己完成,而是由核心模块完成;Ansible管理主机之前,先调用core Modules中的模块,然后指明管理Host Lnventory中的主机,就可以完成管理主机。
Custom Modules:自定义模块,完成Ansible核心模块无法完成的功能,此模块支持任何语言编写。
Connection Plugins:连接插件,Ansible和Host通信使用
 
1.Ansible优点:
• Stupied Simple ,上手简单,学习曲线平滑
• SSH by default ,安全,无需安装客户端
• 配置简单、功能强大、扩展性强
• 支持API及自定义模块,可通过Python轻松扩展
• 通过Playbooks来定制强大的配置、状态管理
• 提供一个功能强大、操作性强的Web管理界面和REST API接口——AWX平台
• 幂等性:一种操作重复多次结果相同
2.ansible安装
1. yum install epel-release
2. yum install ansible -y
3.ansible配置客户端(无密码登录)
第一种方法:
1. server: ssh-keygen
2. scp id_rsa.pub root@192.168.254.25:/root/.ssh/authorized_keys
第二种方法:
1.vim /etc/ansible/hosts
[zxw]
192.168.126.7 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=123
192.168.126.6 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=123
~
 3.三种模式
hoc:命令
palybooks:剧本:脚本
Roles : 角色
4.ansible常用命令
• ansible-doc -l #查看支持的模块
• ansible-doc -s MODEL_NAME #查看模块用法
• ansible命令应用基础
1. ansible <host-pattern> [options]
-f forks:启动并发线程数 默认并发数:5个
-m model_name:要使用的模块
-a args:特有的参数
• ansible zxw -m ping #查看client端是否正常ping通客户端p
192.168.126.7 | FAILED! => {
"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
}
从输出提示上基本可以了解到由于在本机的~/.ssh/known_hosts文件中并有fingerprint key串,ssh第一次连接的时候一般会提示输入yes 进行确认为将key字符串加入到 ~/.ssh/known_hosts 文件中

基于这个原因,可以修改/etc/ansible/ansible.cfg配置文件

[root@zxw8 ~]# vim /etc/ansible/ansible.cfg

# uncomment this to disable SSH key host checking
host_key_checking = False
• ansible webserver -m setup #查看客户端信息
• ansible webserver -m copy -a 'src=/root/git_test/code.txt dest=/root/test' #copy文件到cient端
[root@zxw8 ~]# ansible zxw -m copy -a 'src=/root/zxw dest=/root/'
组名 -m'模块 -a 参数 (原文件 ,目标文件)
• ansible webserver -m user -a "name=test state=present" #创建test用户


• ansible webserver -m user -a "name=test state=absent" #删除test用户
• ansible webserver -m yum -a ‘name=epel-relese state=latest‘ #yum安装
[root@zxw8 ~]# ansible zxw -m service -a 'name=network state=started'

启动
[root@zxw8 ~]# ansible zxw -m service -a 'name=network state=stoped'
关闭
[root@zxw8 ~]# ansible zxw -m service -a 'name=network state=retarted'
重启
• ansible webserver -m script -a ‘/tmp/test.sh‘ #运行脚本
• ansible webserver -m command 'date' #查看时间
[root@zxw8 ~]# ansible zxw -m command -a 'date "+%F %T"'
组名 -m 模块 -a ‘命令’
命令警告
[root@zxw8 ~]# vim /etc/ansible/ansible.cfg
# instead of shelling out to the git command.
command_warnings = False
打开注释

 

原文地址:https://www.cnblogs.com/itzhao/p/11262003.html