Ansible 是一个开源的配置管理工具,咱们用于自动化工作、部署应用程序。应用 Ansible,您能够主动执行日常工作,例如更新零碎、装置软件、和配置服务。配置/etc/hosts文件感觉用ip地址麻烦,能够在Master管制节点中的/etc/hosts文件中增加主机名对应着节点名称:[[email protected] ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.43.131 Master
192.168.43.165 node1
192.168.43.166 node2
192.168.43.167 node3
在上一篇文章中,创立了一个名为 Master 的管制节点和四个受控节点(node1、node2、node3 和 node4)。当初,为了让 Ansible 与受控节点通信,须要为Master节点的ansible用户配置免密登录四个受控节点。ansible用户配置ssh免密登录在Master和node1-node4中都须要创立ansible用户:# 在Master节点创立用户,增加到wheel组,并设置ansible用户明码
[[email protected] ~]# useradd ansible
[[email protected] ~]# usermod -aG wheel ansible
[[email protected] ~]# echo '123'|passwd --stdin ansible
Changing password for user ansible.
passwd: all authentication tokens updated successfully.
在node1节点创立用户,增加到wheel组,并设置ansible用户明码
[[email protected] ~]# useradd ansible
[[email protected] ~]# usermod -aG wheel ansible
[[email protected] ~]# echo '123'|passwd --stdin ansible
Changing password for user ansible.
passwd: all authentication tokens updated successfully.
在node2节点创立用户,增加到wheel组,并设置ansible用户明码
[[email protected] ~]# useradd ansible
[[email protected] ~]# usermod -aG wheel ansible
[[email protected] ~]# echo '123'|passwd --stdin ansible
Changing password for user ansible.
passwd: all authentication tokens updated successfully.
在node3节点创立用户,增加到wheel组,并设置ansible用户明码
[[email protected] ~]# useradd ansible
[[email protected] ~]# usermod -aG wheel ansible
[[email protected] ~]# echo '123'|passwd --stdin ansible
Changing password for user ansible.
passwd: all authentication tokens updated successfully.
在Master和node1-node4中执行visudo命令,将%wheel ALL=(ALL) NOPASSWD: ALL后面的正文去掉,这样ansible用户应用sudo时不须要输出明码了:[[email protected] ~]# visudo
在管制节点Master上切换到ansible用户,生成 SSH 密钥,而后将 SSH 公钥复制到所有受控节点。[[email protected] ~]# su - ansible
[[email protected] ~]$ ssh-keygen
当初,将 SSH 公钥复制到所有受控节点,这让 ansible 用户无需输出明码即可登录所有节点主机了:[[email protected] ~]$ ssh-copy-id [email protected]
[[email protected] ~]$ ssh-copy-id [email protected]
[[email protected] ~]$ ssh-copy-id [email protected]
配置文件默认的 Ansible 配置文件位于 /etc/ansible/ansible.cfg 下。Ansible 的大部分设置都能够应用此配置文件进行批改以满足环境需要,上面理解一下 Ansible 在哪里搜寻配置文件,Ansible 按以下顺序搜索配置文件,ansible找到的第一个配置文件,而后疏忽其余文件:$ANSIBLE_CONFIG如果设置了此变量ansible.cfg如果在当前目录中~/.ansible.cfg如果它在用户的主目录中。/etc/ansible/ansible.cfg默认的配置文件默认清单文件位于 /etc/ansible/hosts 中,但能够在配置文件中更改此地位。您还能够通过-i选项指定要应用的清单文件。上面在ansible的家目录创立一个~/.ansible.cfg配置文件,而后创立一个inventory清单文件:[[email protected] ~]$ touch ~/.ansible.cfg
[[email protected] ~]$ touch inventory
在~/.ansible.cfg中制订inventory文件的地位:[[email protected] ~]$ cat ~/.ansible.cfg
[defaults]
inventory = /home/ansible/inventory
上面将主机节点写入到inventory文件中,内容中创立了三个组,nodes、test、prod:[[email protected] ~]$ vim inventory
[nodes]
node1
node2
node3
[test]
node1
[prod]
node2
node3
Ansible ad-hoc命令ad-hoc能够在命令行疾速执行命令,不须要编写playbook。应用ad-hoc查看节点的连通性应用 ping 模块查看与节点主机的连贯。[[email protected] ~]$ ansible all -m ping
在下面的命令中,all 示意让 Ansible 在所有主机上运行此命令。应用ad-hoc治理包应用 Ansible 的ad-hoc命令,还能够将软件包装置到节点主机。上面实例是将httpd装置在[test]组中:[[email protected] ~]$ ansible test -b -m yum -a "name=httpd state=present"
node1 | SUCCESS => {
"ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"msg": "Nothing to do","rc": 0,"results": []
}
应用ad-hoc治理服务因为在上一步中胜利装置了 httpd 服务器,上面应用 Ansible 的 ad-hoc 命令启动和启用 httpd 服务,以便 Web 服务器启动并运行。[[email protected] ~]$ ansible test -b -m service -a "name=httpd enabled=yes state=started"
Playbook与 Ansible ad-hoc命令不同,Ansible 剧本能够保留和重复使用。每个playbook 由一个或多个playbook组成。上面是一个简略的 Ansible 剧本,在不同的节点装置不同的服务。上面实例文件名为httpd.yaml,用于在 prod 组装置 httpd 服务,启动服务,防火墙凋谢80端口。而后再所有节点装置git安装包:# 在vim编辑器中增加这条信息,让缩进更不便一些。
http://www.cctime.com/html/20...
http://www.citnews.com.cn/new...
https://www.csdn.net/article/...
https://www.51cto.com/it/news...
[[email protected] ~]$ echo 'autocmd FileType yaml setlocal ai ts=2 sw=2 et' > .vimrc
[[email protected] ~]$ vim httpd.yml
name: Install httpd on prod group.
hosts: prod
become: yes
tasks:- name: Install httpd
yum:
name: httpd
state: latest - name: enable httpd service
service:
name: httpd
enabled: yes
notify: restart httpd
handlers: - name: restart httpd
service:
name: httpd
state: restarted
- name: Install httpd
name: Install git on all hosts
hosts: all
become: yes
tasks:- name: Install Git
yum:
name: git
state: latest
编写实现playbook时,能够试运行一下,而后再真正的运行:[[email protected] ~]$ ansible-playbook httpd.yml -C
[[email protected] ~]$ ansible-playbook httpd.yml
- name: Install Git
总结Ansible 简略、易于设置且功能强大。Ansible 是无代理的,这使系统管理员能够轻松开始自动化并破费更少的工夫进行配置。