ansible使用总结

ansible安装和部署


#建议在centos6以上部署ansible,方法如下。
[root@study06 ~]# yum -y install epel-release
[root@study06 ~]# yum -y install ansible

ansible目录结构介绍


/etc/ansible/  			#ansible配置文件目录。
/etc/ansible/ansible.cfg	#ansible主配置文件。
/etc/ansible/hosts		#ansible被管理主机配置文件。

ansible命令使用方法


ansible命令方法适合维护linux系统,如果要维护网络设备最好使用ansible-playbook。

#如下为常用方法,更多请见man ansible
ansible [组名]  -m  [模块]  -a  [命令]
注意:默认ansible使用公钥,如果要使用密码,需要使用-u 和 -k参数。
#举例如下,
[root@study06 ansible]# ansible test -m command -a 'free -h'
192.168.31.14 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.31.14 port 22: No route to host",
    "unreachable": true
}
192.168.31.15 | SUCCESS | rc=0 >>
             total       used       free     shared    buffers     cached
Mem:          238M       234M       3.5M       632K        51M        46M
-/+ buffers/cache:       136M       101M
Swap:         511M         0B       511M

更多用法见:http://www.ansible.com.cn/docs

ansible_hosts配置文件解析


hosts基本用法

[webservers1]		#定义组举例1
foo.example.com
bar.example.com

[webservers2]		#定义组举例2
www[01:50].example.com

[databases]		#定义组举例3
db-[a:f].example.com

[webservers]		#定义组举例4
alpha.example.org
beta.example.org
192.168.1.100:5055	#如果端口号不是默认22,可以指定端口号。
192.168.1.110

hosts指定变量用法

主机变量的用法

[atlanta]
host1 http_port=80 maxRequestsPerChild=808   #主机变量
host2 http_port=303 maxRequestsPerChild=909

组变量的用法

[atlanta]
host1
host2

[atlanta:vars]				#组的变量
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
#父组与子组的用法

[atlanta]			#子组
host1
host2

[raleigh]			#子组
host2
host3

[southeast:children]		#父组
atlanta
raleigh
#父组变量的用法

[atlanta]			#子组
host1
host2

[raleigh]			#子组
host2
host3

[southeast:children]		#父组
atlanta
raleigh

[southeast:vars]		#父组变量的用法
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2
#如果变量特别多还可以将变量单独成目录写入目录的文件中。

#变量组定义的目录为下
/etc/ansible/group_vars/raleigh.yaml
/etc/ansible/group_vars/webservers.yaml
/etc/ansible/host_vars/foosball.yaml

#raleigh.yaml文件的内容如下格式:
---
ntp_server: acme.example.org
database_server: storage.example.org

变量的解释说明

ansible_ssh_host
      将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.

ansible_ssh_port
      ssh端口号.如果不是默认的端口号,通过此变量设置.

ansible_ssh_user
      默认的 ssh 用户名

ansible_ssh_pass
      ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)

ansible_sudo_pass
      sudo 密码(这种方式并不安全,我们强烈建议使用 --ask-sudo-pass)

ansible_sudo_exe (new in version 1.8)
      sudo 命令路径(适用于1.8及以上版本)

ansible_connection
      与主机的连接类型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前默认使用 paramiko.1.2 以后默认使用 'smart','smart' 方式会根据是否支持 ControlPersist, 来判断'ssh' 方式是否可行.

ansible_ssh_private_key_file
      ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况.

ansible_shell_type
      目标系统的shell类型.默认情况下,命令的执行使用 'sh' 语法,可设置为 'csh' 或 'fish'.

ansible_python_interpreter
      目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是"/usr/bin/python",比如  *BSD, 或者 /usr/bin/python
      不是 2.X 版本的 Python.我们不使用 "/usr/bin/env" 机制,因为这要求远程用户的路径设置正确,且要求 "python" 可执行程序名不可为 python以外的名字(实际有可能名为python26).

      与 ansible_python_interpreter 的工作方式相同,可设定如 ruby 或 perl 的路径....

更多请参考:
http://www.ansible.com.cn/docs/intro_getting_started.html#gs-about

原文地址:https://www.cnblogs.com/wangzengyi/p/12558810.html