《Ansible权威指南》笔记(1)——安装,ssh密钥登陆,命令

来源:互联网 发布:ubuntu apt jdk1.8 编辑:程序博客网 时间:2024/05/23 12:20


读这本《Ansible权威指南》学习ansible,根据本书内容和网上的各种文档,以及经过自己测试,写出以下笔记。另,这本书内容很好,但印刷错误比较多,作者说第二版会改进,还没买的小伙伴们可以买第二版。

 

一、安装
1、安装要求:
控制服务器:需要安装Python2.6/2.7
被管理服务器:需要安装Python2.4 以上版本,若低于Python2.5 需要安装pythonsimplejson;若启用了selinux,则需要安装libselinux-python

2、yum安装
yum install ansible -y
安装方式很多,个人推荐使用epel源安装,epel源安装方式不再赘述。截至目前,epel源安装的是2.2版。

3、简单配置
vim /etc/ansible/ansible.cfg    默认配置文件,读取配置文件的顺序是当前目录——当前用户家目录——/etc/ansible/ansible.cfg(该顺序未验证,建议在统一的地方配置,以免混乱)
#remote_user = root    默认使用的远程连接用户
vim /etc/ansible/hosts    Inventory文件的默认位置,指定所要管理的主机安装

 

二、ssh密钥登陆
ansilbe采用ssh的方式管理节点,为了方便管理,使用密钥方式面密码登陆被管理节点。
1、生成rsa格式密钥
ssh-keygen -t rsa
2、把公钥写入到远端主机的~/.ssh/authorized_keys
ssh-copy-id username@192.168.1.50
3、管理机设置默认远程用户
vim /etc/ansible/ansible.cfg
remote_user = username

 

 

三、ansible命令
1、ansible    临时的一次性操作
Usage: ansible <host-pattern> [options]
host-pattern可以是域名如http://www.pan8.net/,IP,也可以在/etc/ansible/hosts指定
options
    -m    后接模块
    -a    后接模块参数
    -u    指定用户名
    -f    启动的并发线程数
例:
ansible all -m ping
ansible all -m copy -a "src=/etc/fstab dest=/tmp/fatab owner=root group=root mode=644 backup=yes"

报错:
The authenticity of host '[192.168.99.43]:22051 ([192.168.99.43]:22)' can't be established.
RSA key fingerprint is 5e:9d:5c:4c:e8:cd:6e:78:70:a2:04:1c:5f:6f:3a:1e.
Are you sure you want to continue connecting (yes/no)? The authenticity of host '[192.168.99.248]:22051 ([192.168.99.248]:22)' can't be established.
RSA key fingerprint is 64:d9:ef:67:6a:d5:37:ff:70:2f:06:d2:35:d1:6b:a2.
Are you sure you want to continue connecting (yes/no)? The authenticity of host '[192.168.99.247]:22051 ([192.168.99.247]:22)' can't be established.
解决方法:
sed -i 's/^#host_key_checking = False/host_key_checking = False/' /etc/ansible/ansible.cfg    

2、ansible-doc    查看模块文档
Usage: ansible-doc [options] [module...]
Options:
  -h, --help            show this help message and exit
  -l, --list            List available modules
  -M MODULE_PATH, --module-path=MODULE_PATH
                        specify path(s) to module library (default=None)
  -s, --snippet         Show playbook snippet for specified module(s)
  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable
                        connection debugging)
  --version             show program's version number and exit
ansible-doc 模块名    模块说明
ansible-doc -s 模块名    简要说明  

ansible-doc -l    报错:
[DEPRECATION WARNING]: docker is kept for backwards compatibility but usage is discouraged. The module documentation details page may explain more about this rationale..
This feature will be removed in
a future release. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
[ERROR]: unable to parse /usr/lib/python2.6/site-packages/ansible/modules/extras/cloud/misc/rhevm.py
ERROR! module rhevm has a documentation error formatting or is missing documentation
解决方法:
sed -i 's/^#deprecation_warnings = True/deprecation_warnings = False/' /etc/ansible/ansible.cfg
rm -f /usr/lib/python2.6/site-packages/ansible/modules/extras/cloud/misc/rhevm.py

3、ansible-playbook    读取事先写好的playbook,可以理解为按一定条件组成的ansible任务集
Usage: ansible-playbook playbook.yml

4、ansible-galaxy    上传下载Roles,Roles地址https://galaxy.ansible.com/
Usage: ansible-galaxy [delete|import|info|init|install|list|login|remove|search|setup] [--help] [options] ...

5、ansible-pull    pull模式(ansible默认push模式),用于数量巨大的机器配置,以及没有网络连接的主机上运行ansible。
Usage: ansible-pull -U <repository> [options]
    -U    URL of the playbook repository

6、ansible-vault    用于配置文件加密
ansible-vault [create|decrypt|edit|encrypt|rekey|view] [--help] [options] vaultfile.yml
例:
ansible-vault encrypt a.yml    加密并设置解密密码,加密后打开为乱码
ansible-vault decrypt a.yml    解密

7、ansible-console    进入类似于shell的交互模式

0 0
原创粉丝点击