学习

  • ansible core : ansible 自身核心模块
  • host inventory: 主机库,定义可管控的主机列表
  • connection plugins: 连接插件,一般默认基于 ssh 协议连接
  • modules:core modules ( 自带模块 ) 、 custom modules ( 自定义模块 )
  • playbooks :剧本,按照所设定编排的顺序执行完成安排任务

安装

install

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# python --version
# Python 2.7.5
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
# 
# DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
####
# get-pip.py options
# do not attempt to install setuptools
--no-setuptools
# do not attempt to install wheel
--no-wheel
# Install to the user site
--user
# install ansible
sudo pip install ansible
# 
ansible --version
ansible 2.7.10
  config file = None
  configured module search path = [u'/home/vagrant/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Apr  9 2019, 14:30:50) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
# 如果用pip安装完ansible之后,config file是None的。也就是说不存在/etc/ansible这个配置文件夹,需要自己创建。

生成密钥

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 创建ssh密钥
mkdir ~/.ssh;
cd ~/.ssh/
# 都按回车
ssh-keygen -t rsa
# 加入授权
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
# 修改文件权限
chmod 600 ~/.ssh/authorized_keys
# 授权登录
ssh-copy-id -i ~/.ssh/id_rsa.pub vagrant@n55
ssh-copy-id -i ~/.ssh/id_rsa.pub vagrant@n56

常用命令

1
2
3
4
5
6
7
8
9
ansible --version
ansible -m
ansible-doc -s command
absible-galaxy
ansible-link
ansible-init playbook.yml
ansible-playbook
ansible-pull
ansible-vault
1
2
3
4
5
tree /etc/ansible/
/etc/ansible/
├── ansible.cfg
├── hosts
└── roles
1
2
3
4
ansible 192.168.33.56 -m ping -vvv
ansible webservers -m ping -vvv
ansible -m command -a '/bin/echo hello ansible!'
ansible 192.168.33.56 -m command -a 'sudo yum install -y bridge-utils bind-utils psmisc'

错误

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
192.168.33.55 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}

192.168.33.55 | FAILED! => {
    "msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"
}

192.168.33.27 | FAILED | rc=1 >>
error: garbage option

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).non-zero return code
## command不支持管道命令

参考