ansible 安装配置测试
ansible --version
Traceback (most recent call last):
File "/usr/bin/ansible", line 44, in <module>
import ansible.constants as C
ImportError: No module named ansible.constants
yum 安装ansible, pip也需要安装ansible…
ansible docker -m command -a 'uptime'
172.17.0.2 | SUCCESS | rc=0 >>
07:50:44 up 203 days, 5:50, 1 user, load average: 0.06, 0.18, 0.13
172.17.0.4 | SUCCESS | rc=0 >>
07:50:54 up 203 days, 5:50, 1 user, load average: 0.05, 0.18, 0.13
ansible的command模块不支持管道…而shell模块可以
ansible模块的存在意义, 有了command/shell模块完全可以替代所有其他模块
unarchive: src=/tmp/redis-3.0.7.tar.gz dest=/usr/local
file or module does not exist
需要再加上copy=no
参考https://github.com/ansible/ansible-modules-core/issues/1443
自动化部署不能完成所有的事情, 它能做的只是把单台服务器上的步骤推送到多台而已. 所以你首先需要把自己要做的功能能用shell脚本抽象出来, 否则还是不够用. 任何自动化工具都帮不了你.
环境搭建还是可以的, 像fastdfs, redis集群搭建, jar包/xml配置文件替换,,,简直痴人说梦
http://sofar.blog.51cto.com/353572/1579894/
http://os.51cto.com/art/201409/451927_all.htm
http://www.ansible.com.cn/docs/playbooks_best_practices.html#id14
关于ansible变量默认值的尝试
common/vars/main.yml中的变量, 可以被common角色本身获取, 也可以在其他角色的meta/main.yml通过角色依赖的方式获取. 而在common/default/main.yml中设置的变量, 可以通过在主调playbook中通过include role
引入, 也可以通过在主调角色的meta/main.yml通过角色依赖方式引入.
需要注意的是, 如果定义了一个字典形式的变量默认值, 比如{service: {name: 'cache', port: 8080}}
, 那么在主调角色中, 变量service
必须同时拥有name
, port
两个键, 同时覆盖, 不能只覆盖service字典中的name
字段(当然, 如果没有service
变量, 就会使用默认值).
另外, 不能通过include语句在tasks/main.yml, vars/main.yml中引入存放默认变量的配置文件, 因为直接引入变量配置文件, 同名的键会引起冲突.
ansible的yml文件key和value不能有中划线, 在此规定, 键名按照驼峰命名法, 键值使用下划线连接
ansible主调文件a.yml中可以使用include包含b.yml,c.yml;
a.yml
- include b.yml
- include c.yml
命令行执行时可以使用--limit
参数指定某个特定的被调文件:ansible-playbook a.yml --limit b
–tag标记对应group_vars
与host_vars
目录中设置的变量
cat group_vars/a
ntp: www.ntp-server.com
然后在(一般是)common
下的tasks/main.yml中为每条ntp相关的task下面都加上tags: ntp
标记
这样, 在ansible-playbook site.yml --tags ntp
就可以单独执行标记为tags: ntp
的task了
remote_user: gluon
指令并不是拥有目标服务器的root的公钥, 执行ansible时就会通过su
切换至gluon
用户, 它没有那么只能, ansible执行端必须拥有目标服务器gluon
的公钥才行.
shell命令虽然可以用bash管道, 但是不能使用&& ||等操作符连续执行命令, 比如
shell: su - general && ls
ls命令列出的依然是remote_user指定用户的家目录内容.
也就是说, shell模块虽然可以执行一连串bash命令, 但这些命令是不相关的, 后者也没办法取到前者的返回值, 它们相互独立.
with_items
指令, 还有with_dic
, 放在一个task中.
- name: add several users
user: name={{ item }} state=present groups=wheel
with_items:
- testuser1
- testuser2
输出debug信息到文件
- hosts: all
tasks:
# emit a debug message containing the content of each file.
- debug:
msg: "{{ item }}"
with_file:
- first_example_file
- second_example_file
如果不在主调yml文件中显示include某个角色, 就没有办法使用它下面的默认变量.
ansible ImportError: No module named yum
ansible问题处理
1.
fatal: [172.17.0.3]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Control socket connect(/root/.ansible/cp/ansible-ssh-172.17.0.3-22-root): Connection refused\r\nFailed to connect to new control master\r\n", "unreachable": true}
问题分析
这种情况一般出现在使用docker作为ansible容器的时候.
ssh客户端可以配置ControlPath
字段启用"缓存", 它会在与远程主机建立连接后生成一个sock
类型的文件, 之后的ssh连接可以通过这个文件与远程主机通过, 具体请参考ssh命令应用. 但是, 存储在docker容器中的sock
文件貌似是无法使用的, 原因应该是docker存储驱动的问题, 将其存储在-v
挂载的主机目录中就不会有这种问题, 出现在docker-1.14-rc2
的源码安装版本.
解决方法
因为ansible在通过ssh工具连接远程主机时, 自行加上了许多参数, 用于重用/加速连接, 只要将这些参数去掉即可. ansible配置文件在/etc/ansible/ansible.cfg
, 把ssh_args
赋值为空就好了.
2.
Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this
详细报错为
192.168.1.1 | FAILED => 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.
192.168.1.2 | FAILED => 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私钥, 而是在inventory清单文件中以变量形式写入密码, 格式如下. 但是在执行playbook时报上述错误
[target]
192.168.1.1 ansible_ssh_user=root ansible_ssh_pass=123456
192.168.1.2 ansible_ssh_user=root ansible_ssh_pass=123456
原因分析
这是因为首次连接远程主机时, ssh会提示将远程主机的fingerprint key串加到本地的~/.ssh/known_hosts
文件中. 就是如下部分这种啦, 眼熟吧?
The authenticity of host '106.75.5.133 (106.75.5.133)' can't be established.
RSA key fingerprint is c1:a1:8b:9f:30:dc:2b:6d:a7:75:e5:67:60:26:ff:d1.
Are you sure you want to continue connecting (yes/no)?
ansible的执行过程会被这种提示打断, 所以提示出错.
解决方法
方法1:
了解到问题原因为, 我们了解到进行ssh连接时, 可以使用-o参数将StrictHostKeyChecking设置为no, 使用ssh连接时避免首次连接时让输入yes/no部分的提示.通过查看ansible.cfg配置文件, 发现如下行:
[ssh_connection]
# ssh arguments to use
# Leaving off ControlPersist will result in poor performance, so use
# paramiko on older platforms rather than removing it
#ssh_args = -o ControlMaster=auto -o ControlPersist=60s
所以这里我们可以启用ssh_args
部分, 使用下面的配置, 避免上面出现的错误:
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no
方法2:
在ansible.cfg
配置文件中, 也会找到如下部分:
# uncomment this to disable SSH key host checking
host_key_checking = False
默认host_key_checking
部分是注释的, 通过找开该行的注释, 同样也可以实现跳过ssh首次连接提示验证部分. 由于配置文件中直接有该选项, 所以推荐用方法2.
Ansible应用场景
1. SSH部分
2. 日志输出
默认ansible 执行的时候,并不会输出日志到文件,不过在ansible.cfg 配置文件中有如下行:
# logging is off by default unless this path is defined
# if so defined, consider logrotate
log_path = /var/log/ansible.log
同样,默认log_path
这行是注释的,打开该行的注释,所有的命令执行后,都会将日志输出到/var/log/ansible.log
文件,便于了解在何时执行了何操作及其结果,如下:
[root@361way.com ansible]# cat /var/log/ansible.log
2015-05-04 01:57:19,758 p=4667 u=root |
2015-05-04 01:57:19,759 p=4667 u=root | /usr/bin/ansible test -a uptime
2015-05-04 01:57:19,759 p=4667 u=root |
2015-05-04 01:57:20,563 p=4667 u=root | 10.212.52.252 | success | rc=0 >>
01:57am up 23 days 11:20, 2 users, load average: 0.38, 0.38, 0.40
2015-05-04 01:57:20,831 p=4667 u=root | 10.212.52.14 | success | rc=0 >>
02:03am up 331 days 8:19, 2 users, load average: 0.08, 0.05, 0.05
2015-05-04 01:57:20,909 p=4667 u=root | 10.212.52.16 | success | rc=0 >>
02:05am up 331 days 8:56, 2 users, load average: 0.00, 0.01, 0.05