---
# System prepended metadata

title: 實作playbook(DE sharing Day-3)
tags: [Ansible]

---

---
title: 實作playbook(DE sharing Day-3)
tags: Ansible
---

# 動動手時間:see_no_evil:
- (remote) 若ansible_user不是root，請先確認該帳號已經被加入 `/etc/sudoers`（擁有suders權限），後續比較方便
- (Control) 在 Ansible 2.3.1 以後，通常需要額外安裝 sshpass，才可正常使用。<br>若是不想安裝sshpass，可透過公鑰私鑰機制登入，在Inventory裡面設定`ssh_key=your_path`，或是
```
$ ansible-playbook -k -i hosts playbook.yml 
```

### Step1: 建立 Inventory
```python=
$ mkdir ansible_demo
$ cd ansible_demo
```
```python=
# vi hosts
node1 ansible_host=ip ansible_user=登入名字 ansible_ssh_common_args='-o StrictHostKeyChecking=no'
node2 ansible_host=ip ansible_user=登入名字 ansible_ssh_common_args='-o StrictHostKeyChecking=no'
node3 ansible_host=ip ansible_user=登入名字 ansible_ssh_common_args='-o StrictHostKeyChecking=no'
```
![](https://i.imgur.com/cPHIkDX.png)
<!--### Step2: 建立ansible.cfg
```python=
# vi ansible.cfg
[default]
host_key_checking = false
```
![](https://i.imgur.com/cPHIkDX.png)-->


### Step2: 測試是否連線成功(ad-hoc)
```python=
# -i 後面接 inventory檔案
# 接著使定要測試的節點名稱
# -m 後面接模組，使用ping 來測試是否能連線
$ ansible -k -i hosts node1 -m ping
```

## :small_blue_diamond:練習題一：測試連線
```python=
# vi playbook.yml
- hosts: all
  tasks:
    # task 1
    - name: test connection
      ping:
      register: message

    # task 2
    - name: print debug message
      debug:
        msg: "{{ message }}"
```
注意:heavy_exclamation_mark:在ansible playbook中若是有任何一個任務運行失敗，則會造成整個playbook自動終止，有一些"執行失敗"的module可供使用，後面再介紹．
>若是想要繼續執行，則需要加上參數`ignore_errors: True`
## :small_blue_diamond:練習題二：用 playbook 印出 Hello World
```python=
換你了
# shell: echo "Hello World"
```
## :small_blue_diamond:練習題三：自動安裝套件(透過 `pip` 安裝套件)
>yum install、apt-get intall 方法雷同
```python=
# vi playbook.yml 
- hosts: all
  tasks:
    - name: install beautifulsoup4
      pip:
        name:
          - beautifulsoup4
          - urllib
        
#- name: yum install packages
#  hosts: es
#  become: yes (狀態是否改變)
#  become_user: root （狀態改變成root）
# tasks:
#   - name: install python-pip
#      yum:
#        name:
#          - epel-release
#          - python-pip
#        state: latest/present
```

## :small_blue_diamond:練習題四：讀寫檔案
```python=
$ ansible-playbook -k -i hosts playbook.yml --extra-vars "filename=esb19099"

# vi create.yml
- hosts: all
  tasks:
    - name: create file and add content
      lineinfile:
        path: "/tmp/{{ filename }}.txt"
        line: "{{ item }}"
        create: yes
      with_items:
          - TEST

    - name: change content
      lineinfile:
        path: "/tmp/{{ filename }}.txt" 
        regexp: "{{ item.regexp }}"
        line: "{{ item.line }}"
      with_items:
        - { regexp: TEST, line: test }
    
    - name: display multiple file contents
      shell: "cat /tmp/{{ filename }}.txt"
      register: content

    - debug: msg="{{ content }}"
```

#### :star2:執行指令：`$ ansible-playbook -k -i hosts playbook.yml` 
>$ ansible-playbook -k -i hosts playbook.yml -v #可看到更多連線執行資訊
>(-vvv for more, -vvvv to enable connection debugging)

![](https://i.imgur.com/8b56drt.png)
https://docs.ansible.com/ansible/latest/index.html

:grey_exclamation:更多 ansible 使用的指令
```
$ ansible --help
```