ansible學習筆記(四):when在include中使用

Ansible中國用戶組QQ羣: 142851673 

簡單測試ansible的wheninclude中使用

1需求shell

用到一個配置需求,對hosts中不一樣類型的主機進行各自的操做。數據庫

1)實驗環境:服務器

客戶端IP
應用類型
Hosts中名稱
192.168.24.10
數據庫
db
192.168.24.11
應用程序
app

2)操做結果:app

192.168.24.10 服務器/tmp下生成db.txt192.168.24.11服務器/tmp下生成app.txt文件ide

2測試結構測試

# tree test/spa

test/orm

├── hostsxml

├── install_client.ymlip

└── roles

    └──install_client

       ├── files

       ├── tasks

          ├── app.yml

          ├── db.yml

          └── main.yml

       ├── templates

1hosts

[db]

192.168.24.10

[app]

192.168.24.11

2install_client.yml

---

###

## laowafang2014-12-1

####

- hosts: '` hosts `'

  user: ansible

  sudo: yes

  sudo_user:root

  roles:

    -install_client

3roles/install_client/tasks/ main.yml       # 本次測試的關鍵地方

---

### agentd install

- include: db.yml

  when: "hosts == 'db'"

- include: app.yml

  when: "hosts == 'app'"

4roles/install_client/tasks/db.yml

---

  - name: Touchdb file

    shell: touch /tmp/db.txt

5roles/install_client/tasks/app.yml

---

  - name: Touchdb file

 shell: touch /tmp/db.txt

3測試playbook

# cd/etc/ansible/test

[ansible@localhosttest]$ ansible-playbook -i ./hosts install_client.yml --extra-vars "hosts=db"

PLAY [db]*********************************************************************

GATHERING FACTS***************************************************************

ok: [192.168.24.10]

TASK: [install_client | Touch db file]****************************************

changed: [192.168.24.10]

TASK: [install_client | Touch app]********************************************

skipping: [192.168.24.10]

PLAY RECAP ********************************************************************

192.168.24.10              : ok=2    changed=1   unreachable=0    failed=0  

[ansible@localhosttest]$ ansible-playbook -i ./hosts install_client.yml --extra-vars "hosts=app"

PLAY [app] ********************************************************************

GATHERING FACTS***************************************************************

ok: [192.168.24.11]

TASK: [install_client | Touch db file]****************************************

skipping: [192.168.24.11]

TASK: [install_client | Touch app]********************************************

changed: [192.168.24.11]

PLAY RECAP********************************************************************

192.168.24.11              : ok=2    changed=1   unreachable=0    failed=0

### 查看對應服務器IP地址分別生成各自文件。

Ansible中國用戶組QQ羣: 142851673

相關文章
相關標籤/搜索