--ansible安装
yum -y install ansible
如果无法安装,先安装前置
[root@arthur ~]# yum install epel-release
[root@arthur ~]# yum repolist
[root@arthur ~]# yum -y install ansible
免秘钥登陆
[root@martin-1 ~]# ssh-keygen -t rsa
[root@martin-1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.106.129
[root@martin-1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.106.130
为Ansible定义节点的清单
文件/etc/ansible/hosts 维护着Ansible中服务器的清单。
[root@ansible ~]# vi /etc/ansible/hosts
[vm]
192.168.106.129 name pasword
192.168.106.130
保存并退出文件。
尝试在Ansible服务端运行命令
使用ping检查‘test-servers’或者ansible节点的连通性。
[root@ansible ~]# ansible -m ping 'vm'
执行shell命令
1:检查Ansible节点的运行时间(uptime)
[root@ansible ~]# ansible -m command -a "uptime" 'vm'
2:检查节点的内核版本
[root@ansible ~]# ansible -m command -a "uname -r" 'vm'
打开日志
修改:ansible.cfg文件(默认路路径为/etc/ansible/ansible.cfg)
#log_path = /var/log/ansible.log(取消注释,值为默认日志存放地点)
play_book
创建yaml文件
vim testVault
- hosts: test
vars:
remote_user: root
tasks:
- name: write the apache config file
template: src=/tmp/1.txt dest=/tmp
执行ansible-playbook testVault
Playbook扩展--使用参数和加密
vim testVault
- hosts: test
vars_files:
- vars/files.yml
remote_user: root
tasks:
- name: write the apache config file
template: src=/tmp/{{ file_name }} dest=/tmp
vim files.yml
---
file_name: '2.txt'
ansible-playbook testfile.yml --ask-vault-pass
输入密码即可