ansible学习

在使用之前的系统优化:

文件句柄数优化:

1  sudo launchctl limit maxfiles 1024 2048

主控机需要python版本:

1 python2.6 或者2.7

 安装方式:

1 源码安装或者yum安装。

节点python要求:

1 python2.4以上,低于2.5需要安装模块:
2 yum install -y   python-simplejson

安装pip:

 1 1下载:
 2 # wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate
 3 1.2 pip安装
 4 
 5 # tar -xzvf pip-1.5.4.tar.gz
 6 # cd pip-1.5.4
 7 # python setup.py install

安装过程中由于paramiko报错。源码进行安装:

 1 1、下载安装包:
 2 
 3   https://pypi.python.org/packages/source/p/paramiko/paramiko-1.14.0.tar.gz#md5=e26324fd398af68ad506fe98853835c3
 4 
 5 2、解压缩:
 6 
 7   tar -xzf paramiko-1.14.0.tar.gz
 8 
 9   cd paramiko-1.14.0
10 
11   执行命令:
12 
13   python setup.py install
14 
15   安装完成后进入到demos目录中运行如下命令检测安装结果:
16 
17   python demo.py localhost
18 
19 3、可能出现的问题:
20 
21 a.ImportError: No module named ecdsa
22 
23   解决办法:
24 
25   下载安装包:https://pypi.python.org/packages/source/e/ecdsa/ecdsa-0.11.tar.gz#md5=8ef586fe4dbb156697d756900cb41d7c
26 
27   cd ecdsa-0.11
28 
29 
30   解压缩:
31 
32   tar -xzf ecdsa-0.11.tar.gz
33 
34   python setup.py install
35 
36 
37 b.ImportError: No module named Crypto.PublicKey
38 
39   解决办法:
40 
41   yum -y install pycrypto

 

在安装过程由于paramiko需要sshpass 源码进行安装:

源码安装:

1 curl -O -L http://downloads.sourceforge.net/project/sshpass/sshpass/1.05/sshpass-1.05.tar.gz
2 
3 tar xvzf sshpass-1.05.tar.gz
4 
5 cd sshpass-1.05
1 ./configure
2 
3 make
4 
5 sudo make install 

 ansible 默认执行并发为5.


 

inventory文件:

默认情况在执行的时候 不指定主机列表文件,会找默认的路径的:/etc/ansible/hosts

该文件可以存储主机IP列表、主机名。

格式为:

 1 mail.example.com
 2 
 3 [webservers]
 4 foo.example.com
 5 bar.example.com
 6 
 7 [dbservers]
 8 one.example.com
 9 two.example.com
10 three.example.com
  • ip列表和主机列表可以使用类似python的切片形式:
1 [webservers]
2 www[01:50].example.com

用来表示如:www1.example.com到www50.example.com范围的主机。当然可以用:[1:50]形式。意义是一样的。

在执行的时候可以指定执行那个组执行。

1 ansible  webserver -m command  -a 'uptime' -u  --ask-pass

该文件我们可以定义一系列参数,如:不需要密码输入的。

1 1 # cat /etc/ansible/hosts 
2 2 [test]
3 3 192.168.31.222 ansible_ssh_user=root ansible_ssh_pass=123
4 
5 如果不想输入密码进行远程执行的话 需要设置:ansible_ssh_user=root ansible_ssh_pass=123

 我们可以给组定义变量:

1 [atlanta]
2 host1
3 host2
4 
5 [atlanta:vars]
6 ntp_server=ntp.atlanta.example.com
7 proxy=proxy.atlanta.example.com 

如果有多台主机话。我们不能每个主机都需要输入密码,这个时候用组定义来实现;

  •  实现批量没密码登录:定义vars变量  写全局配置。然后登陆test主机组的时候,直接用vars定义的变量进行登陆。
1 [test]
2 192.168.31.222
3 [test:vars]
4 ansible_ssh_port=22
5 ansible_ssh_user=root
6 ansible_ssh_pass=123

inventory文件的参数说明:

 1 ansible_ssh_host
 2       将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.
 3 
 4 ansible_ssh_port
 5       ssh端口号.如果不是默认的端口号,通过此变量设置.
 6 
 7 ansible_ssh_user
 8       默认的 ssh 用户名
 9 
10 ansible_ssh_pass
11       ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)
12 
13 ansible_sudo_pass
14       sudo 密码(这种方式并不安全,我们强烈建议使用 --ask-sudo-pass)
15 
16 ansible_sudo_exe (new in version 1.8)
17       sudo 命令路径(适用于1.8及以上版本)
18 
19 ansible_connection
20       与主机的连接类型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前默认使用 paramiko.1.2 以后默认使用 'smart','smart' 方式会根据是否支持 ControlPersist, 来判断'ssh' 方式是否可行.
21 
22 ansible_ssh_private_key_file
23       ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况.
24 
25 ansible_shell_type
26       目标系统的shell类型.默认情况下,命令的执行使用 'sh' 语法,可设置为 'csh''fish'.
27 
28 ansible_python_interpreter
29       目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是"/usr/bin/python",比如  \*BSD, 或者 /usr/bin/python
30       不是 2.X 版本的 Python.我们不使用 "/usr/bin/env" 机制,因为这要求远程用户的路径设置正确,且要求 "python" 可执行程序名不可为 python以外的名字(实际有可能名为python26).
31 
32       与 ansible_python_interpreter 的工作方式相同,可设定如 ruby 或 perl 的路径....

例子:

1 some_host         ansible_ssh_port=2222     ansible_ssh_user=manager
2 aws_host          ansible_ssh_private_key_file=/home/example/.ssh/aws.pem
3 freebsd_host      ansible_python_interpreter=/usr/local/bin/python
4 ruby_module_host  ansible_ruby_interpreter=/usr/bin/ruby.1.9.3

 

  • 在执行的过程出现一个问题,就是第一次登陆的机器执行ansible 报错,第二次就好了。需要升级openssh 详见:http://blog.csdn.net/tacuhuh/article/details/52121787
  • pip安装ansible的时候,需要注意:默认ansible.cfg 配置文件没有。需要手动建立。

读取配置文件的顺序的是:

 1 Ansible的一些的设置可以通过配置文件完成.在大多数场景下默认的配置就能满足大多数用户的需求,在一些特殊场景下,用户还是需要自行修改这些配置文件
 2 
 3 用户可以修改一下配置文件来修改设置,他们的被读取的顺序如下:
 4 
 5 * ANSIBLE_CONFIG (一个环境变量)
 6 * ansible.cfg (位于当前目录中)
 7 * .ansible.cfg (位于家目录中)
 8 * /etc/ansible/ansible.cfg
 9 
10 版本1.5之前的读取顺序如下:
11 
12 * ansible.cfg (位于当前目录)
13 * ANSIBLE_CONFIG (一个环境变量)
14 * .ansible.cfg (位于家目录下)
15 * /etc/ansible/ansible.cfg

所以在当前文件建立配置文件。

默认配置:

1 [defaults]
2 host_key_checking = False

 pattern:

常用模块区别:

 1 ansbile自身已经自带了很多模块,可以通过ansible-doc -l 进行查看。这里就结合command、shell、raw、script模块了解下其用法。
 2 上面四个模块都属于commands 类。
 3 
 4 command模块,该模块通过-a跟上要执行的命令可以直接执行,不过命令里如果有带有如下字符部分则执行不成功 “ so variables like $HOME and operations like "<", ">", "|", and "&" will not work (use the shell module if you need these features).”;
 5 shell 模块,用法其本和command一样,不过的是其是通过/bin/sh进行执行,所以shell 模块可以执行任何命令,就像在本机执行一样,“ It is almost exactly like the command module but runs the command through a shell (/bin/sh) on the remote node.”;
 6 raw模块,用法和shell 模块一样 ,其也可以执行任意命令,就像在本机执行一样,“Executes a low-down and dirty SSH command, not going through the module subsystem. There is no change handler support for this module. This module does not require python on the remote system”
 7 script模块,其是将管理端的shell 在被管理主机上执行,其原理是先将shell 复制到远程主机,再在远程主机上执行,原理类似于raw模块,“This module does not require python on the remote system, much like the raw module.” 。
 8 
 9 注:raw模块和comand、shell 模块不同的是其没有chdir、creates、removes参数,chdir参数的作用就是先切到chdir指定的目录后,再执行后面的命令,这在后面很多模块里都会有该参数 。
10 command模块包含如下选项: 
11 
12 creates:一个文件名,当该文件存在,则该命令不执行 
13 free_form:要执行的linux指令 
14 chdir:在执行指令之前,先切换到该指定的目录 
15 removes:一个文件名,当该文件不存在,则该选项不执行
16 executable:切换shell来执行指令,该执行路径必须是一个绝对路径
  • 注意的是:command模块不识别元字符和变量等。
  • raw可以、shell可以。
  • command 模块不支持 shell 变量,也不支持管道等 shell 相关的东西.如果你想使用 shell相关的这些东西, 请使用’shell’ 模块.

在使用shell模块的时候,需要注意的是:单引号和双引号的区别:

1 $ ansible raleigh -m shell -a 'echo $TERM' 

使用 Ansible ad hoc 命令行接口时(与使用 Playbooks 的情况相反),尤其注意 shell 引号的规则. 比如在上面的例子中,如果使用双引号”echo $TERM”,会求出TERM变量在当前系统的值,而我们实际希望的是把这个命令传递 到其它机器执行.

注意单双引号的区别使用!!尤其是在读取变量的时候。

 

posted @ 2016-10-26 06:41  evil_liu  阅读(138)  评论(0)    收藏  举报