你将学到什么
- 如何使用playbook
- 如何编写playbook
- 如何使用roles
PlayBook使用
基础环境
### 64 位 Ubuntu 16.04 LTS,创建CentOS LXC容器web模拟托管节点 # ssh-keygen -t rsa # apt-get install lxc # apt-get install yum # lxc-create -n centos -t centos -- -R 7 ### 修改centos模板root密码 # chroot /var/lib/lxc/centos/rootfs passwd # lxc-copy -n centos -N web -B aufs -s # lxc-start -n web -d ### 进入容器 # lxc-console -n web ### 下面命令都在容器中执行,修改IP地址为10.0.3.200 # vi ifcfg-eth0 DEVICE=eth0 BOOTPROTO=static ONBOOT=yes HOSTNAME=centos NM_CONTROLLED=no TYPE=Ethernet NAME=eth0 IPADDR=10.0.3.200 NETMASK=255.255.255.0 GATEWAY=10.0.3.1 DNS1=114.114.114.114
简单的playbook
# mkdir playbook
# cd playbook
# vim hosts
[web]
192.168.124.240
# vim site.yml
- name: Sample
hosts: web
# 收集host facts信息
gather_facts: True
tasks:
# 在ansible托管节点上生成sample.txt文件
- name: Web
command: /bin/sh -c "echo 'web' > ~/sample.txt"
# 在ansible控制主机上生成sample.txt文件
- name: Local Web
local_action: command /bin/sh -c "echo 'local web' > ~/sample.txt"
执行playbook
# ansible-playbook -i hosts site.yml
样例playbook
下载样例
### 在主机中下在ansible样例 $ git clone https://github.com/ansible/ansible-examples.git
修改样例配置文件
$ cd ansible-examples/tomcat-standalone $ vim hosts [tomcat-servers] 10.0.3.200 ### 配置ssh登入密码 $ vim group_vars/tomcat-servers # Here are variables related to the Tomcat installation http_port: 8080 https_port: 8443 # This will configure a default manager-gui user: admin_username: admin admin_password: 123456 ansible_ssh_pass: 123456
执行playbook
### 出错就反复执行,不过要加上出错提示中的--limit @/home/ubuntu/ansible-examples/tomcat-standalone/site.retry参数 # ansible-playbook -i hosts site.yml
出错处理
- 问题1
TASK [selinux : Install libselinux-python] *************************************
fatal: [10.0.3.200]: FAILED! => {"changed": false, "failed": true, "msg": "Failure talking to yum: Cannot retrieve metalink for repository: epel/x86_64. Please verify its path and try again"}
to retry, use: --limit @/home/ubuntu/ansible-examples/tomcat-standalone/site.retry
解决办法
### 在容器中执行一遍yum update更新下源,就是更新下缓存,不需要安装软件
- 问题2
TASK [tomcat : insert firewalld rule for tomcat http port] *********************
fatal: [10.0.3.200]: FAILED! => {"changed": false, "failed": true, "msg": "firewalld and its python 2 module are required for this module"}
RUNNING HANDLER [tomcat : restart tomcat] **************************************
to retry, use: --limit @/home/ubuntu/ansible-examples/tomcat-standalone/site.retry
解决办法
### 为容器安装firewalld # yum search firewalld |grep python python-firewall.noarch : Python2 bindings for firewalld # yum install python-firewall.noarch # systemctl enable firewalld # systemctl start firewalld
roles使用
roles标准结构
# tree ansible-sshd/
ansible-sshd/
├── CHANGELOG
├── defaults
│ └── main.yml
├── handlers
│ └── main.yml
├── LICENSE
├── meta
│ ├── 10_top.j2
│ ├── 20_middle.j2
│ ├── 30_bottom.j2
│ ├── main.yml
│ ├── make_option_list
│ ├── options_body
│ └── options_match
├── README.md
├── tasks
│ └── main.yml
├── templates
│ └── sshd_config.j2
├── tests
│ ├── inventory
│ ├── roles
│ │ └── ansible-sshd -> ../../.
│ └── test.yml
├── Vagrantfile
└── vars
├── Amazon.yml
├── Archlinux.yml
├── Debian_8.yml
├── Debian.yml
├── default.yml
├── Fedora.yml
├── FreeBSD.yml
├── OpenBSD.yml
├── RedHat_6.yml
├── RedHat_7.yml
├── Suse.yml
├── Ubuntu_12.yml
├── Ubuntu_14.yml
└── Ubuntu_16.yml
| 目录名 | 说明 |
|---|---|
| defaults | 为当前角色设定默认变量时使用此目录,应当包含一个main.yml文件 |
| handlers | 此目录中应当包含一个main.yml文件,用于定义此角色用到的各handler,在handler中使用include包含的其它的handler文件也应该位于此目录中 |
| meta | 应当包含一个main.yml文件,用于定义此角色的特殊设定及其依赖关系 |
| tasks | 至少应该包含一个名为main.yml的文件,其定义了此角色的任务列表,此文件可以使用include包含其它的位于此目录中的task文件 |
| templates | template模块会自动在此目录中寻找Jinja2模板文件 |
| vars | 定义当前角色使用的变量 |
| files | 存放由copy或script等模块调用的文件 |
| tests | 在playbook中角色的使用样例 |
roles使用
# cat ansible-sshd/tests/test.yml --- - hosts: localhost become: true roles: - ansible-sshd # cd ansible-sshd/tests/ # ansible-playbook test.yml
roles的任务执行顺序
### 首先执行meta下的main.yml文件内容 ### 然后执行tasks下的main.yml文件内容
来源:https://www.cnblogs.com/silvermagic/p/7666264.html