Mac 安装 ansible
Mac 安装 ansible
# 这里在虚拟环境下安装ansible
mkvirtualenv -p python3 ansible_env
pip install ansible
ansible --version
# ⚠️
# pip 安装的ansible 默认是没有配置文件的,并且由于不断的更新,
# https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
# 的默认配置文件也是不生效的,
# 所以推荐brew install ansible 之后拷贝配置文件过来。
Ansible 文件路径
# 配置文件路径
/etc/ansible
或者
/private/etc/ansible/
# 执行文件路径
/Users/username/.virtualenvs/ansible_env/bin/ansible
ansible 设置facts缓存
执行playbook的时候, 默认第一个task都是GATHERING FACTS, 这个过程就是Ansible在收集每台主机的facts信息。
方便我们在playbook中直接引用facts里的信息,当然如果你的playbook中不需要facts信息, 可以在playbook中设置"gather_facts: False"来提高playbook效率.
但是如果我们既想在每次执行playbook的时候都能收集facts, 又想加速这个收集过程, 那么就需要配置facts缓存了。
使用json文件配置缓存
$ vim /etc/ansible/ansible.cfg
.........
gathering = smart
fact_caching_timeout = 86400
fact_caching = jsonfile
fact_caching_connection = /dev/shm/ansible_fact_cache
使用redis配置缓存
# 首先需要安装redis 插件
pip install redis
# 配置缓存
$ vim /etc/ansible/ansible.cfg
........
gathering = smart
facts_caching_timeout = 86400 #设置缓存过期时间86400秒
facts_caching = redis # 使用redis或者 (或者使用memcached,即"facts_caching = memcached")
fact_caching_connection = 127.0.0.1:6379
#若redis设置了密码,比如密码为"admin",则配置修改如下:
# fact_caching_connection = localhost:6379:0:admin
# 需要注意:
# 在使用redis缓存后,如果出现异常(若未出现,请忽略):TypeError: the JSON object must be str, not 'bytes'。
解决办法:
$ find / -name ansible
$ vim /usr/lib/python2.7/site-packages/ansible/plugins/cache/redis.py
..........
self._cache[key] = json.loads(value.decode('utf-8')) # 修改为这个
ansible 取消 host key交互
$ vim /etc/ansible/ansible.cfg
........
host_key_checking = False # 打开注释即可
# 取消ssh的yes和no的交互:
$ vim /root/.ssh/config
UserKnownHostsFile /dev/null
ConnectTimeout 15
StrictHostKeyChecking no
或者直接ssh时增加一个参数
$ ssh -o StrictHostKeyChecking=no -p22 root@10.4.7.101
配置ssh的长链接: 开启ControlPersist
其实就是持久化socket,一次验证多次通信.并且只需要修改ssh client就行了.
支持这个特性需要比较新的openssh
# 添加ControlMaster的配置
cat /home/ansible/.ssh/config 或 ~/.ssh/config
Host *
Compression yes
ServerAliveInterval 60
ServerAliveCountMax 5
ControlMaster auto
ControlPath ~/.ssh/sockets/%r@%h-%p
ControlPersist 4h
本文来自博客园, 作者:Star-Hitian, 转载请注明原文链接:https://www.cnblogs.com/Star-Haitian/articles/15237072.html

浙公网安备 33010602011771号