ansible笔记
ansible安装
yum -y install ansible
git clone https://github.com/ansible/ansible.git
2
3
# 1.预先安装python3.6版本
./configure --prefix=/usr/local/python3
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.6 /usr/local/bin/pip
2
3
# 2.安装virtualenv
pip install virtualenv
ln -s /usr/local/python3/bin/virtualenv /usr/local/bin/virtualenv
2
# 3.创建ansible账户并安装python3.6版本virtualenv实例
useradd deploy && su - deploy
virtualenv -p /usr/local/bin/python3 .py3-a2.5-env
2
# 4.git源代码安装ansible2.5
cd /home/deploy/.py3-a2.5-env
git clone https://github.com/ansible/ansible.git
cd ansible && git checkout stable-2.5
2
3
# 5.加载python3.6 virtualenv环境
source /home/deploy/.py3-a2.5-env/bin/activate
# 6.安装ansible依赖包
pip install paramiko PyYAML jinja2
# 7.在python3.6虚拟环境下加载ansible2.5
source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q
# 8.验证ansible
ansible --version
# 9、ssh免密码秘钥认证
ssh-keygen -t rsa
ssh-copy-id -i /home/deploy/.ssh/id_rsa.pub root@192.168.200.235
for IP in {210..234};do ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.200.$IP;done
2
3
4
# 10、ansible命令
ansible-playbook -i inventory/testenv ./deploy.yml
ansible all -m ping
ansible 192.168.200.235 -m ping
2
3
4
#以wang sudo至root用户执行ls
ansible all -m command -u wang -a 'ls /root' -b --become-user=root -k -K
ansible all -m command -a 'useradd wang'
ansible all -m shell -a "echo $HOSTNAME"
ansible all -m copy -a "src=/opt/scripts/sshcopy.sh dest=/tmp/sshcopy.sh mode=600"
ansible all -m copy -a "content='he there\n' dest=/tmp/hi.txt owner=mygrp group=mygrp"
ansible all -m fetch -a 'src=/etc/hosts dest=/opt/test'
2
3
4
5
6
7
8
9
10
11
# 11、ansible变量
# 11.1 全局变量
cat >a.json<<EOF {"name":"qfedu","type":"school"} EOF
ansible all -i localhost, -m debug -a "msg='name is , type is '" -e @a.json
# 11.2 剧本变量
和playbook有关,定义在playbook中
- name: test play vars hosts: all vars: user: lilei home: /home/lilei
通过PLAY属性vars_files定义
在playbook中使用变量 ""
# 11.3 主机变量
[webservers] 192.168.200.224 user=xlqywk port=3309
# 11.4 主机组变量
[webservers] 192.168.200.224
[webservers:vars] /home/xlqywk user=xlqywk
主机变量优先级更高
# 11.5 内置变量
ansible_ssh_port ssh端口号不是默认的时候 ansible_ssh_user 默认的ssh用户名 ansible_ssh_pass ansible_sudo_pass
主机指定端口连接ssh [db] 192.168.200.222 ansible_ssh_port=2222 ansible_ssh_user=xlqywk
facts变量 仅获取服务器的内存情况信息 ansible all -i localhost, -m setup -a "filter=memory" -c local 仅获取磁盘信息 ansible all -i localhost, -m setup -a "filter=mount" -c local
关闭facts变量 在yaml文件中加入以下字段 gather_facts: no
注册变量 register
# 12、playbooks框架与格式
inventory/ server详细清单目录 testenv 具体清单与变量声明文件 roles/ roles任务列表 testbox/ testbos详细任务 tasks/ main.yml testbox主任务文件 deploy.yml playbook任务入口文件
主任务文件main.yml 任务名称 执行的任务
任务入口文件deploy.yml
- hosts: "testservers" server列表
gather_facts: true 获取server基本信息
remote_user: root 目标服务器系统用户指定
roles:
- testbox 进入roles/testbox任务目录
---
- hosts: all
remote_user: root
tasks:
- name: "安装apache"
yum: name=httpd
- name: "复制配置文件"
copy: src=/etc/httpd/conf/httpd.conf dest=/tmp/
- name: "复制配置文件"
copy: src=/etc/httpd/conf.d/welcome.conf dest=/tmp/
- name: "启动apache,并设置开机启动"
service: name=httpd state=started enabled=yes
2
3
4
5
6
7
8
9
10
11
12
创建用户和创建文件
- name: create user foo
user: name=foo
- name: create a file
file: 'path=/tmp/foo.txt state=touch mode=0755 owner=foo'
2
3
4
# 13、host配置文件
详细目录testenv [testservers] server组列表 test.example.com 目标服务器
[testservers:vars] server组列表参数 server_name=testexample.com user=root output=/root/test.txt 目标主机key/value参数