Skip to content

Instantly share code, notes, and snippets.

@chusiang
Last active November 2, 2023 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chusiang/8550bb340c3d03d54f93aa1e8ae52b8c to your computer and use it in GitHub Desktop.
Save chusiang/8550bb340c3d03d54f93aa1e8ae52b8c to your computer and use it in GitHub Desktop.
# 可以設定一些預設行為
# [defaults] General defaults
[defaults]
# hostfile -- 主機 ip 對照
inventory = hosts
# remote_user -- 遠端使用者名稱
remote_user = vagrant
# private_key_file -- SSH privite key path
private_key_file = ~/.ssh/id_rsa
# host_key_checking -- 不詢問加入 ssh 金鑰
host_key_checking = False
# 設定 retry files (*.retry) 存放路徑, 預設放家目錄
# 我自己喜歡指定在目前目錄, 以免作完實驗家目錄一堆 .retry
retry_files_save_path = ./ansible-retry
# 平行處理數量, 預設是 5 個, 應該不一定會用到先記下來
# forks = 20
# 關閉警告訊息
#deprecation_warnings=False
log_path = ./logs/playbooks.log
#!/usr/bin/env ansible-playbook
# ============================================================
# Author: chusiang / chusiang.lai (at) gmail.com
# Blog: http://note.drx.tw
# Filename: hello.yml
# Modified: 2023-11-03 03:30
# Description: A sample code for ansible-playbook.
#
# [ jonny@banshee ~/vcs/sandbox/ansible ] (develop) - 03:36
# (cmd)$ ansible-playbook hello.yml
#
---
- name: say hello
hosts: all
tasks:
- name: run
shell: echo 'Hello, Ansible !' || /bin/true
register: result
- name: print
debug:
msg: "{{ result.stdout }}"
- debug: var=result
post_tasks:
- name: Copy playbook.log
connection: local
command: "mv ./logs/playbooks.log ./logs/ansible.{{ lookup('pipe', 'date +%Y%M%d') }}.log"
run_once: true
# vim: set filetype=yaml.ansible
#!/usr/bin/env ansible-playbook
# vim:ft=ansible :
# ============================================================
# Author: chusiang / chusiang.lai (at) gmail.com
# Blog: http://note.drx.tw
# Filename: hello_penguin.yml
# Modified: 2023-11-03 03:30
# Description: A sample code for ansible-playbook.
#
# [ jonny@banshee ~/vcs/sandbox/ansible ] (develop) - 03:39
# (ins)$ ansible-playbook hello_penguin.yml
---
- name: say hello penguin
hosts: all
tasks:
- name: run
shell: echo 'Hello, Penguin !' || /bin/true
register: result
- name: print
debug:
msg: "{{ result.stdout }}"
- debug: var=result
post_tasks:
- name: Copy playbook.log
connection: local
command: "mv ./logs/playbooks.log ./logs/ansible.{{ lookup('pipe', 'date +%Y%M%d') }}.log"
run_once: true
# vim: set filetype=yaml.ansible
@chusiang
Copy link
Author

chusiang commented Nov 2, 2023

Before

[ jonny@banshee ~/vcs/sandbox/ansible ] (develop) - 03:42
(ins)$ ls -1
...

ansible.cfg
...

hello.yml
hello_penguin.yml
...

hosts
...
logs/
...

Run

[ jonny@banshee ~/vcs/sandbox/ansible ] (develop) - 03:42
(cmd)$ ansible-playbook hello_penguin.yml
 __________________________
< PLAY [say hello penguin] >
 --------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

 ________________________
< TASK [Gathering Facts] >
 ------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

[WARNING]: Platform darwin on host localhost is using the discovered Python interpreter at
/usr/local/bin/python3.11, but future installation of another Python interpreter could change
the meaning of that path. See https://docs.ansible.com/ansible-
core/2.15/reference_appendices/interpreter_discovery.html for more information.
ok: [localhost]
 ____________
< TASK [run] >
 ------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

changed: [localhost]
 ______________
< TASK [print] >
 --------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

ok: [localhost] => {
    "msg": "Hello, Penguin !"
}
 ______________
< TASK [debug] >
 --------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

ok: [localhost] => {
    "result": {
        "changed": true,
        "cmd": "echo 'Hello, Penguin !' || /bin/true",
        "delta": "0:00:00.007412",
        "end": "2023-11-03 03:43:53.567539",
        "failed": false,
        "msg": "",
        "rc": 0,
        "start": "2023-11-03 03:43:53.560127",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "Hello, Penguin !",
        "stdout_lines": [
            "Hello, Penguin !"
        ]
    }
}
 __________________________
< TASK [Copy playbook.log] >
 --------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

changed: [localhost]
 ____________
< PLAY RECAP >
 ------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

localhost                  : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

After

[ jonny@banshee ~/vcs/sandbox/ansible ] (develop) - 03:41
(ins)$ tree logs/
logs/
├── ansible.20233503.log
└── ansible.20233603.log

1 directory, 2 files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment