centos 7使用tar包安装ansible

由于我的yum源没有ansible软件包,防止以后内网服务器也没有ansible的yum源,干脆一点,直接tar包安装!!!!

下载软件包

- # 1.python安装
# python2.7安装
[root@VM-0-17-centos ~]# wget https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz
[root@VM-0-17-centos ~]# tar xvzf Python-2.7.8.tgz
[root@VM-0-17-centos ~]# cd Python-2.7.8
[root@VM-0-17-centos Python-2.7.8]# ./configure --prefix=/usr/local
[root@VM-0-17-centos Python-2.7.8]# make  &&  make install

# 将python头文件拷贝到标准目录,以避免编译ansible时,找不到所需的头文件
[root@VM-0-17-centos ~]# cd /usr/local/include/python2.7
[root@VM-0-17-centos ~]# cp -a ./* /usr/local/include/

# 备份旧版本的python,并且符号链接新版本的python
[root@VM-0-17-centos ~]# cd /usr/bin
[root@VM-0-17-centos ~]# mv python python.old
[root@VM-0-17-centos ~]# ln -s /usr/local/bin/python2.7 /usr/local/bin/python
[root@VM-0-17-centos ~]# rm -f /usr/bin/python && cp /usr/local/bin/python2.7 /usr/bin/python

# 修改yum脚本,使其指向旧版本的python,已避免其无法运行
[root@VM-0-17-centos ~]# vim /usr/bin/yum
#!/usr/bin/python --> #!/usr/bin/python2.4

# python-devel安装
Tips :若python版本已经为2.6或以上,则不需要再重装python,只是还需要安装python开发包:python-dev(有的操作系统下为python-devel)
[root@VM-0-17-centos ~]# yum -y install python-devel


- # 2.setuptools模块安装
[root@VM-0-17-centos ~]# wget https://pypi.python.org/packages/source/s/setuptools/setuptools-7.0.tar.gz
[root@VM-0-17-centos ~]# tar -xf setuptools-7.0.tar.gz 
[root@VM-0-17-centos ~]# cd setuptools-7.0/
[root@VM-0-17-centos setuptools-7.0]# python setup.py  install

- # 3.pyYaml安装
[root@VM-0-17-centos ~]# wget http://pyyaml.org/download/libyaml/yaml-0.1.5.tar.gz
[root@VM-0-17-centos ~]# tar -xf yaml-0.1.5.tar.gz 
[root@VM-0-17-centos ~]# cd yaml-0.1.5/
[root@VM-0-17-centos yaml-0.1.5]# ./configure --prefix=/usr/local
[root@VM-0-17-centos yaml-0.1.5]# make --jobs=`grep processor /proc/cpuinfo | wc -l`
[root@VM-0-17-centos yaml-0.1.5]# make install

[root@VM-0-17-centos ~]# wget https://pypi.python.org/packages/source/P/PyYAML/PyYAML-3.11.tar.gz
[root@VM-0-17-centos ~]# tar -xf PyYAML-3.11.tar.gz 
[root@VM-0-17-centos ~]# cd PyYAML-3.11/
[root@VM-0-17-centos PyYAML-3.11]# python setup.py install

- # 4.Jinjia2模块安装
[root@VM-0-17-centos ~]# tar -xf MarkupSafe-0.9.3.tar.gz 
[root@VM-0-17-centos ~]# cd MarkupSafe-0.9.3/
[root@VM-0-17-centos MarkupSafe-0.9.3]# python setup.py install

[root@VM-0-17-centos ~]# wget https://pypi.python.org/packages/source/J/Jinja2/Jinja2-2.7.3.tar.gz
[root@VM-0-17-centos ~]# tar -xf Jinja2-2.7.3.tar.gz 
[root@VM-0-17-centos ~]# cd Jinja2-2.7.3/
[root@VM-0-17-centos Jinja2-2.7.3]# python setup.py install

- # 5.paramiko模块安装
[root@VM-0-17-centos ~]# wget https://pypi.python.org/packages/source/e/ecdsa/ecdsa-0.11.tar.gz
[root@VM-0-17-centos ~]# tar -xf ecdsa-0.11.tar.gz 
[root@VM-0-17-centos ~]# cd ecdsa-0.11/
[root@VM-0-17-centos ecdsa-0.11]# python setup.py install

[root@VM-0-17-centos ~]# wget https://pypi.python.org/packages/source/p/paramiko/paramiko-1.15.1.tar.gz
[root@VM-0-17-centos ~]# tar -xf paramiko-1.15.1.tar.gz 
[root@VM-0-17-centos ~]# cd paramiko-1.15.1/
[root@VM-0-17-centos paramiko-1.15.1]# python setup.py install

- # 7.simplejson模块安装
[root@VM-0-17-centos ~]# wget https://pypi.python.org/packages/source/s/simplejson/simplejson-3.6.5.tar.gz
[root@VM-0-17-centos ~]# tar -xf simplejson-3.6.5.tar.gz 
[root@VM-0-17-centos ~]# cd simplejson-3.6.5/
[root@VM-0-17-centos simplejson-3.6.5]# python setup.py  install

- # 8.ansible安装
[root@VM-0-17-centos ~]# wget https://mirrors.huaweicloud.com/ansible/ansible-1.7.2.tar.gz
[root@VM-0-17-centos ~]# tar -xf ansible-1.7.2.tar.gz 
[root@VM-0-17-centos ~]# cd ansible-1.7.2/
[root@VM-0-17-centos ansible-1.7.2]# python setup.py  install

[root@VM-0-17-centos ~]# ansible --version
ansible 1.7.2

ansible的配置

注:centos7.0安装ansible后发现找不到ansible.cfg,配置文件的路径如下,并将配置文件拷贝过去
[root@VM-0-17-centos ~]# cd ansible-1.7.2/examples/
[root@VM-0-17-centos examples]# ls
ansible.cfg  hosts  playbooks
[root@VM-0-17-centos examples]# mkdir /etc/ansible
[root@VM-0-17-centos examples]# cp hosts ansible.cfg  /etc/ansible
[root@VM-0-17-centos examples]# ll /etc/ansible
total 12
-rw-r--r-- 1 root root 7172 Aug 27 11:30 ansible.cfg
-rw-r--r-- 1 root root  965 Aug 27 11:30 hosts

默认配置文件位置为/etc/ansible/ansilble.cfg,配置文件位置可以修改。

Ansible 按照如下位置和顺序来查找ansible.cfg 文件:
1.ANSIBLE_CONFIG 环境变量所指定的文件。
2../ansible.cfg(当前目录下的ansible.cfg)。
3.~/.ansible.cfg(家目录下的.ansible.cfg)。
4./etc/ansible/ansible.cfg。

配置ssh登录

# 推荐将公钥传到被管理主机上面
[root@VM-0-17-centos ~]# ssh-keygen -t rsa       # 创建公钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Qj0BOMA7w5BT57pyv29IBTpni5rMPXt9RivKSKp8wCA root@VM-0-17-centos
The key's randomart image is:
+---[RSA 2048]----+
| +o......        |
|+ .o+  . .       |
| + o.o. o        |
|E B.o..  .       |
|+ .B o. S        |
| o..o  . .       |
|+o=o .. . .      |
|+=o=+..o +       |
|oo.o**o +        |
+----[SHA256]-----+
[root@VM-0-17-centos ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub  121.37.227.88   # 将公钥传给被管理主机
root@121.37.227.88's password:     # 输入密码
[root@VM-0-17-centos ~]# ssh 121.37.227.88   # 远程登录被管理主机
	
	Welcome to Huawei Cloud Service

[root@ecs-proxy ~]#                    # 成功!



# 也可以选择在本机的配置文件上面添加被管理主机的用户名密码,但是并不安全
[root@VM-0-17-centos ~]# vim /etc/ansible/hosts     # 修改配置文件
[webservers]
121.37.227.88 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123456 

ps:当第一次远程的时候需要确认一下主机之间的公钥连接,我们需要在配置文件中将这个选项修改
[root@VM-0-17-centos ~]# vim /etc/ansible/ansible.cfg
39 host_key_checking = False 



执行测试
[root@VM-0-17-centos ~]# ansible -i /etc/ansible/hosts webservers -m ping
121.37.227.88 | success >> {
    "changed": false, 
    "ping": "pong"                # 成功
}
# 查看服务器的状态
[root@VM-0-17-centos ~]# ansible webservers -m command -a 'w'
121.37.227.88 | success | rc=0 >>
 14:22:03 up 36 days, 20:56,  3 users,  load average: 0.00, 0.03, 0.07
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      21Jul20 36days  0.07s  0.07s -bash
root     pts/0    220.112.46.251   10:05    2:46m  0.06s  0.06s -bash
root     pts/1    106.54.95.242    14:22    1.00s  0.06s  0.01s w

—我遇到的坑坑坑坑们—
执行playbook时报错
pkg_resources.DistributionNotFound: The 'pynacl>=1.0.1' distribution was not found and is required by paramiko

提示pynacl没有安装或者安装版本过低
[root@VM-0-17-centos ~]# pip install pynacl
再报错
paramiko 2.7.1 requires bcrypt>=3.1.3, which is not installed.
提示bcrypt没有安装
[root@VM-0-17-centos ~]# pip install bcrypt

--------------------------------------------------------------------------------

执行ansible时报出紫色的[DEPRECATION WARNING]警告
[DEPRECATION WARNING]: [defaults]hostfile option, The key is misleading as it can also be a list of 
hosts, a directory or a list of paths , use [defaults] inventory=/path/to/file|dir instead. This 
feature will be removed in version 2.8. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.
[DEPRECATION WARNING]: DEFAULT_MODULE_LANG option, Modules are coded to set their own locale if needed 
for screenscraping . This feature will be removed in version 2.9. Deprecation warnings can be disabled 
by setting deprecation_warnings=False in ansible.cfg.
[DEPRECATION WARNING]: DEFAULT_SUDO_EXE option, In favor of Ansible Become, which is a generic 
framework. See become_exe. , use become instead. This feature will be removed in version 2.8. 
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
[DEPRECATION WARNING]: DEFAULT_SUDO_USER option, In favor of Ansible Become, which is a generic 
framework. See become_user. , use become instead. This feature will be removed in version 2.8. 
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

解决办法:
在ansible.cfg文件中根据提示将deprecation_warnings改为FALSE
[root@VM-0-17-centos ~]# vim /etc/ansible/ansible.cfg
103 deprecation_warnings = False 
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值