Ansible Playbooks基本使用


你將學到什么

  • 如何使用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文件內容


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM