01 安装ansible


#################103上安装ansible
yum install -y ansible

rpm -ql ansible | egrep -v "^/usr/(lib|share)"

/etc/ansible/ansible.cfg #ansible服务配置文件
/etc/ansible/hosts #主机清单文件 定义可以管理的主机
/etc/ansible/roles #角色目录


vim /etc/ansible/hosts
...
## db-[99:101]-node.example.com
192.168.0.102
192.168.0.103(本机)

[root@linux-node2 ~]# ssh-keygen -t rsa

[root@linux-node2 ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected]
[root@linux-node2 ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected]
[root@linux-node2 ~]# ansible all -a "hostname"
192.168.0.103 | SUCCESS | rc=0 >>
linux-node2.localdomain

192.168.0.102 | SUCCESS | rc=0 >>
linux-node1.localdomain


Ansible架构图核心组件说明:

Ansible:Ansible的核心程序

Host Lnventory:记录了每一个由Ansible管理的主机信息,信息包括ssh端口,root帐号密码,ip地址等等。可以通过file来加载,可以通过CMDB加载

Playbooks:YAML格式文件,多个任务定义在一个文件中,使用时可以统一调用,“剧本”用来定义那些主机需要调用那些模块来完成的功能.

Core Modules:Ansible执行任何管理任务都不是由Ansible自己完成,而是由核心模块完成;Ansible管理主机之前,先调用core Modules中的模块,然后指明管理Host Lnventory中的主机,就可以完成管理主机。

Custom Modules:自定义模块,完成Ansible核心模块无法完成的功能,此模块支持任何语言编写。

Connection Plugins:连接插件,Ansible和Host通信使用

一、ansible模块

ansible模块应用语法格式:
ansible 主机名称/主机组名称/主机地址信息/all -m(指定应用的模块信息) -a(指定动作信息)

1、command模块(默认模块)
[root@linux-node2 ~]# ansible 192.168.0.102 -m command -a hostname
192.168.0.102 | SUCCESS | rc=0 >>
linux-node1.localdomain

扩展应用:
chdir 切换目录
[root@linux-node2 ~]# ansible 192.168.0.102 -m command -a "chdir=/tmp touch ansible.txt"
[WARNING]: Consider using file module with state=touch rather than running touch

192.168.0.102 | SUCCESS | rc=0 >>

creates 如果指定文件存在,就不执行命令
[root@linux-node2 ~]# ansible 192.168.0.102 -m command -a "creates=/tmp touch ansible.txt"
192.168.0.102 | SUCCESS | rc=0 >>
skipped, since /tmp exists

removes 如果文件存在,就执行
[root@linux-node2 ~]# ansible 192.168.0.102 -m command -a "removes=/tmp touch ansible.txt"
[WARNING]: Consider using file module with state=touch rather than running touch

192.168.0.102 | SUCCESS | rc=0 >>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐