apt-add-repository ppa:ansible/ansible
apt-get update
apt-get install ansible
pip install ansible
hosts
文件vim /etc/ansible/hosts
----------------
[test]
xxx.xx.xx
ssh-copy-id -i ~/.ssh/id_rsa.pub xxx@xxx.xx.xx
ansible test -m ping
--------------------
test | SUCCESS => {
"changed": false,
"ping": "pong"
}
Anable Ad-hoc
::是个临时命令集展示远程主机的
/www
下的所有文件以及文件夹
ansible test -m shell -a "ls /www" --user=root
目标主机需要密码如何处理?(ansible默认通过ssh公钥方式登录)
--ask-pass
参数ansible text -m shell -a "ls /www" --user=root --ask-pass
参数
-a --args 后面的整个内容当做一个参数来传给模块
-f --forks 并发控制机器
-m --module-name 指定模块,一般是ansbile自带的,比如ping,shell
-M --module-path 指定模块存放的路径,一般是自定义的一些模块
-i --inventory 改变默认的行为
-K --ask-pass 密码方式连接远程主机
--private-key 连接需要指定私钥的时候
-u --user 连接需要指定用户的时候
-c --connection 控制连接的类型
-T --timeout 控制连接的超时时间
-s --sudo 需要执行sudo命令的时候
-S --su 需要执行su切换用户命令的时候
-b --become 普通用户要切换到root用户,并且切换过程不需要密码的时候
Ansible Inventory
定义主机关系的文件 默认路径为
/etc/ansible/hosts
(没有的话,创建一下) 文件内容格式是ini
格式的
Inventory
主机组 -- > [组名]
xxx.xx.xx.xx
[组名]
xxx.xx.xx.xx
xxx.xx.xx.xx
Inventory
主机别名jumper ansible_ssh_port=22 ansible_ssh_host=xxx.xxx.xx.xx ansible_ssh_user=root
这样,这个主机的别名就是
jumper
Inventory
连接参数ansible_ssh_host # 连接的远程主机名
ansible_ssh_port # ssh端口号
ansible_ssh_user # 默认的ssh用户名
ansible_ssh_pass # ssh 密码(不安全,建议使用--ask-pass 或者ssh秘钥)
ansible_sudo_pass # 密码(不安全,建议使用--ask-sudo-pass)
Inventory
批量主机组[webservers]
www[01:50].example.com
[databases]
db-[a:f].example.com
以上都是最基本的概念,命令,之后会说说ansible的playbook的,peace yo~
本文作者为olei,转载请注明。