# ad-hoc篇
阿新 • • 發佈:2020-11-30
1、--list-hosts
# 檢視所有配置主機
ansible all --list-hosts
#檢視tt主機組主機
ansible tt --list-hosts
2、shell模組
- 語法: ansible [主機組] -m shell -a '[引數]'
- 注:複雜命令會有出錯得可能,解決方法:寫道指令碼,copy到遠端,執行,將結果拉回執行命令的伺服器
- 示例:
[root@node110 ~]# ansible tt -m shell -a 'getent passwd nginx' 192.168.1.103 | CHANGED | rc=0 >> nginx:x:502:502::/home/nginx:/bin/nologin 192.168.1.104 | CHANGED | rc=0 >> nginx:x:995:993:nginx user:/var/cache/nginx:/sbin/nologin
3、copy模組
- 語法一:ansible [主機組] -m copy -a 'src=[原始檔|路徑] dest=[目標檔案|路徑] owner=[使用者,非必填] group=[屬組,非必填] mode=[許可權,非必填] backup=[yes|no,預設為no,非必填]'
語法二:ansible [主機組] -m copy -a "content='[文字]' dest='[目標檔案|路徑]'" - 示例:
ansible 192.168.1.103 -m copy -a 'src=/usr/local/src/1.sh dest=/usr/local/src/1.sh owner=node103 \ group=node103 mode=777 backup=yes'
ansible 192.168.1.104 -m copy -a "content='Hello World\n' dest='/tmp/1.log'"
4、file模組
- 語法:ansible [主機組] -m file -a 'name=[檔案|路徑] state=[動作] owner=[屬主] mode=[許可權,如755]'
- 示例:
#建立f3檔案 ansible tt -m file -a 'name=/tmp/f3 state=touch' #刪除f3檔案 ansible tt -m file -a 'name=/tmp/f3 state=absent' #建立dir1目錄 ansible tt -m file -a 'name=/tmp/dir1 state=directory' #刪除dir1目錄 ansible tt -m file -a 'name=/tmp/dir1 state=absent' #建立連結 ansible tt -m file -a 'src=/etc/fstab name=/tmp/fstab.link state=link' #刪除連結 ansible tt -m file -a 'name=/tmp/fstab.link state=absent' #刪除tmp目錄 ansible tt -m file -a 'name=/tmp/ state=absent'
5、hostname模組
- 語法:ansible [主機組] -m hostname -a 'name=[主機名]'
- 注:修改主機名,c6系列配置放在/etc/sysconfig/network,c7系列放在/etc/hostname,同時修改/etc/hosts
- 示例:
ansible 192.168.1.103 -m hostname -a 'name=node103'
6、cron模組
- 示例:
# 設定名為warningcron的計劃任務,執行時間每週一、三、五的12:00,執行指令碼/usr/bin/wall FBI warning
ansible all -m cron -a 'minute=00 hour=12 weekday=1,3,5 job="/usr/bin/wall FBI warning" name=warningcron'
# 名為warningcron的計劃任務置為無效
ansible all -m cron -a 'disabled=yes job="/usr/bin/wall FBI warning" name=warningcron'
# 恢復名為warningcron的計劃任務
ansible all -m cron -a 'disabled=no job="/usr/bin/wall FBI warning" name=warningcron'
# 刪除名為warningcron的計劃任務
ansible all -m cron -a 'job="/usr/bin/wall FBI warning" name=warningcron state=absent'
7、script模組
- 示例:
ansible tt -m script -a '/usr/local/src/1.sh'
8、debug模組
- debug模組的msg或var一般配合register輸出資訊
# 示例:register結合msg
[root@node110 yml]# cat 1.yml
---
- hosts: test
remote_user: root
tasks:
- name: install pkg
shell: df -h
register: df_out
when: ansible_default_ipv4.address == "192.168.1.103"
- name: print df_out
debug: msg={{ df_out.stdout_lines }}
[root@node110 yml]# ansible-playbook 1.yml
PLAY [test] *********************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************
ok: [192.168.1.103]
ok: [192.168.1.104]
TASK [install pkg] **************************************************************************************************
skipping: [192.168.1.104]
changed: [192.168.1.103]
TASK [print df_out] *************************************************************************************************
ok: [192.168.1.103] => {
"msg": [
"Filesystem Size Used Avail Use% Mounted on",
"/dev/mapper/VolGroup-lv_root 18G 2.1G 15G 13% /",
"tmpfs 491M 0 491M 0% /dev/shm",
"/dev/sda1 485M 34M 426M 8% /boot"
]
}
fatal: [192.168.1.104]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout_lines'\n\nThe error appears to be in '/usr/local/src/ansible/yml/1.yml': line 9, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n when: ansible_default_ipv4.address == \"192.168.1.103\"\n - name: print df_out\n ^ here\n"}
PLAY RECAP **********************************************************************************************************
192.168.1.103 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.1.104 : ok=1 changed=0 unreachable=0 failed=1 skipped=1 rescued=0 ignored=0
# 示例:register結合var
[root@node110 yml]# cat 4.yml
---
- hosts: test
remote_user: root
tasks:
- name: install pkg
shell: df -h
register: df_out
when: ansible_default_ipv4.address == "192.168.1.103"
- name: print df_out
debug: var=df_out.stdout_lines
[root@node110 yml]# ansible-playbook 4.yml
PLAY [test] *********************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************
ok: [192.168.1.103]
ok: [192.168.1.104]
TASK [install pkg] **************************************************************************************************
skipping: [192.168.1.104]
changed: [192.168.1.103]
TASK [print df_out] *************************************************************************************************
ok: [192.168.1.103] => {
"df_out.stdout_lines": [
"Filesystem Size Used Avail Use% Mounted on",
"/dev/mapper/VolGroup-lv_root 18G 2.1G 15G 13% /",
"tmpfs 491M 0 491M 0% /dev/shm",
"/dev/sda1 485M 34M 426M 8% /boot"
]
}
ok: [192.168.1.104] => {
"df_out.stdout_lines": "VARIABLE IS NOT DEFINED!"
}
PLAY RECAP **********************************************************************************************************
192.168.1.103 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.1.104 : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0