ansible(2)--ansible的安装与配置文件管理
1 ansible的安装
1.1 yum安装
使用epel
源安装:
[root@xuzhichao ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@xuzhichao ~]# yum install ansible
Installed:
ansible.noarch 0:2.9.23-1.el7
Dependency Installed:
PyYAML.x86_64 0:3.10-11.el7 libyaml.x86_64 0:0.1.4-11.el7_0 python-babel.noarch 0:0.9.6-8.el7
python-backports.x86_64 0:1.0-8.el7 python-backports-ssl_match_hostname.noarch 0:3.5.0.1-1.el7 python-cffi.x86_64 0:1.6.0-5.el7
python-enum34.noarch 0:1.0.4-1.el7 python-idna.noarch 0:2.4-1.el7 python-ipaddress.noarch 0:1.0.16-2.el7
python-jinja2.noarch 0:2.7.2-4.el7 python-markupsafe.x86_64 0:0.11-10.el7 python-paramiko.noarch 0:2.1.1-9.el7
python-ply.noarch 0:3.4-11.el7 python-pycparser.noarch 0:2.14-1.el7 python-setuptools.noarch 0:0.9.8-7.el7
python-six.noarch 0:1.9.0-2.el7 python2-cryptography.x86_64 0:1.7.2-2.el7 python2-httplib2.noarch 0:0.18.1-3.el7
python2-jmespath.noarch 0:0.9.4-2.el7 python2-pyasn1.noarch 0:0.1.9-7.el7
Complete!
1.2 pip安装
pip
是安装python
的工具。
[root@manager ~]# yum install python3 python3-devel python3-pip - y
[root@manager ~]# pip3 install --upgrade pip -i https://pypi.douban.com/simple/
[root@manager ~]# pip3 install ansible -i https://pypi.douban.com/simple/
[root@manager ~]# /usr/local/bin/ansible --version
查看ansible
的安装信息,包括版本以及各种文件的路径:
[root@xuzhichao ~]# ansible --version
ansible 2.9.23
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.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 2 2020, 13:16:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
2 ansible相关文件
/etc/ansible/ansible.cfg
:主配置文件,配置 ansible 工作特性/etc/ansible/hosts
:配置主机清单文件/etc/ansible/roles/
:存放 ansible 角色的目录
2.1 ansible配置文件
ansible
的主配置文件存在 /etc/anible/ansible.cfg
,其中大部分的配置内容无需进行修改;
[defaults]
#inventory = /etc/ansible/hosts
`主机清单配置文件`
#library = /usr/share/my_modules/
`库文件存放目录,存放模块`
#module_utils = /usr/share/my_module_utils/
`用到的utils工具存放目录`
#remote_tmp = $HOME/.ansible/tmp
`临时py命令文件存放在远程主机目录,先把ansible执行的指令,先生成py程序,然后放在本地的目录里,然后再用ssh协议,将生成的py程序复制到被管理的机器remote_tmp目录下,复制过去后,拿出来执行,执行完毕后会将该程序删除`
#local_tmp = $HOME/.ansible/tmp
`本机的临时命令执行目录`
#forks = 5 #
`默认并发数`
#poll_interval = 15
`每隔15秒去查看对方的状态`
#sudo_user = root
`默认sudo 用户 `
#ask_sudo_pass = True
#ask_pass = True
`每次执行ansible命令是否询问ssh密码`
#remote_port = 22
`客户端的ssh端口`
#host_key_checking = False
`检查对应服务器的host_key,建议取消注释,用于取消第一次连接问yes|no,若设置为true,则没有输入过yes|no的主机不能使用ansible管理。
#log_path=/var/log/ansible.log
`日志文件,建议取消注释,可以记录日志`
[privilege_escalation] #如果是普通用户则需要配置提权
#become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False
2.2 ansible配置文件的优先级
Ansible
的配置文件可以存放在任何位置,但配置文件有读取顺序,查找顺序如下:-
- 最先查找
$ANSIBLE_CONFIG
变量;
- 最先查找
-
- 其次查找当前项目目录下
ansible.cfg
;
- 其次查找当前项目目录下
-
- 然后查找用户家目录下的
.ansible.cfg
;
- 然后查找用户家目录下的
-
- 最后查找
/etc/ansible/ansible.cfg
;
- 最后查找
-
验证配置文件的优先级:
-
变量
$ANSIBLE_CONFIG
设置的配置文件优先级最高;[root@xuzhichao ~]# touch /tmp/ansible.cfg [root@xuzhichao ~]# ANSIBLE_CONFIG=/tmp/ansible.cfg
-
在项目目录中的优先级第二高;
[root@xuzhichao ~]# mkdir project1 [root@xuzhichao ~]# cd project1 [root@xuzhichao project1]# touch ansible.cfg [root@xuzhichao project1]# ansible --version ansible 2.9.23 config file = /root/project1/ansible.cfg <==当前生效的配置文件 configured module search path = [u'/root/.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 2 2020, 13:16:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
-
在当前用户家目录中的配置文件优先级:
[root@xuzhichao ~]# touch .ansible.cfg [root@xuzhichao ~]# ansible --version ansible 2.9.23 config file = /root/.ansible.cfg <==当前生效的配置文件 configured module search path = [u'/root/.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 2 2020, 13:16:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
-
最后是
/etc/ansible/ansible.cfg
2.3 ansible的主机清单Inventory
ansible
的主要功用在于批量主机操作,想要完成主机管理,第一步要编辑主机清单(inventory)
,Inventory
文件主要用来填写被管理主机以及主机组信息;(逻辑上定义);默认 Inventory
文件为/etc/ansible/hosts
;
当然也可以自定义一个文件,当执行 ansible
命令时使用 -i
选项指定 Inventory
文件位置;
2.3.1 Inventory文件格式
为了便捷地使用其中的部分主机,可以在inventory file
中将其分组命名。
-
分组命名遵循INI文件风格,也就是中括号中的字符为组名,如
[web]
:[web] 192.168.169.128 192.168.169.129
-
可以将同一个主机同时归并到多个不同的组中,如
192.168.169.129
同时存在于[web]与[app]中:[web] 192.168.169.128 192.168.169.129 [app] 192.168.169.130 192.168.169.129
-
支持主机名写法,并且支持通配符:
www[01:50].example.com <==支持通配符匹配www01 www02 ...www50
-
当如若目标主机使用了非默认的
SSH
端口,还可以在主机名称之后使用冒号加端口号来标明:www.example.com:2222
-
可以使用基于用户名密码的方式连接被控主机,可以在主机清单中定义被控主机的用户名密码:
[webservers] 10.0.0.31 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456' 10.0.0.41 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'
2.3.2 使用普通用户管理被控端
ansible
使用普通用户xu
去管理被控端。
-
首先控制端,被控端都需要有
xu
用户;#ansible主机创建用户 [root@xuzhichao etc]# useradd xu [root@xuzhichao etc]# echo "123456" | passwd --stdin xu #被控端主机创建用户 [root@nginx02 ~]# useradd xu [root@nginx02 ~]# echo "123456" | passwd --stdin xu
-
实现普通用户的秘钥认证:
[xu@xuzhichao ~]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/xu/.ssh/id_rsa): Created directory '/home/xu/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/xu/.ssh/id_rsa. Your public key has been saved in /home/xu/.ssh/id_rsa.pub. The key fingerprint is: SHA256:xnPoL029TxN0PH+55LssaS9NtX2DYtkUKwKB3zo4Z7M xu@xuzhichao The key's randomart image is: +---[RSA 2048]----+ | ... | | . . . . | | . o +.o| | o + . + .=| | . S o.= oo*| | o O o.+.oo==| | + =o. ..*oo| | E... .*.o.| | .. ..==.| +----[SHA256]-----+ [xu@xuzhichao ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub xu@192.168.20.22 /bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/xu/.ssh/id_rsa.pub" The authenticity of host '192.168.20.22 (192.168.20.22)' can't be established. ECDSA key fingerprint is SHA256:G8+byxRD1GdKHww8nN1ZbyiAKEcMtVhaPOTTxt0Aldc. ECDSA key fingerprint is MD5:fa:e1:df:9f:ae:c2:3d:f3:67:65:c0:12:3a:e1:ce:cc. Are you sure you want to continue connecting (yes/no)? yes /bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys xu@192.168.20.22's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'xu@192.168.20.22'" and check to make sure that only the key(s) you wanted were added.
-
在被控端主机上配置
visodu
提权,需要配置免passwd
方式提权:[root@nginx02 ~]# visudo xu ALL=(ALL) NOPASSWD:ALL
-
修改控制端
/etc/ansible/ansible.cfg
主配置文件,配置普通用户提权:[root@xuzhichao ~]# vim /etc/ansible/ansible.cfg [privilege_escalation] become=True become_method=sudo become_user=root become_ask_pass=False
-
定义主机清单:
[root@xuzhichao ~]# vim /etc/ansible/hosts [nginx-web] 192.168.20.22 192.168.20.23
-
切换到
xu
用户测试管理被控端主机:[root@xuzhichao ~]# su - xu [xu@xuzhichao ~]$ ansible 192.168.20.22 -m ping 192.168.20.22 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" }