歡迎關(guān)注個人公眾號 DailyJobOps
原文地址: Ansible系列-基礎(chǔ)篇-Ansible 常見模塊的使用
→ 上一篇中簡單嘗鮮了幾個模塊,本篇整理下實際中用到的模塊及其用法Demo佳谦,總計有19個模塊,分別為
ping、setup稍味、debug隧熙、user婉弹、group睬魂、authorized_key、shell镀赌、script氯哮、command、service商佛、systemd蛙粘、copy、template威彰、synchronize、file穴肘、lineinfile歇盼、yum、cron
基本模塊
ping
ping
模塊主要是驗證管理節(jié)點
和目標節(jié)點
之間的連通性评抚,是否正常配置好了對應(yīng)賬號的ssh免密登錄
(kfz-ansible) [james@devops-jumpserver-vm ]$ ansible devops-gitlab-vpc -m ping
devops-gitlab-vpc | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
setup
上一篇中配置的時候提到ansible facts 收集目標主機信息豹缀,也可以使用 setup
模塊伯复。
(kfz-ansible) [james@devops-jumpserver-vm]$ ansible devops-gitlab-vpc -m setup
devops-gitlab-vpc | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.17.115.136"
],
"ansible_all_ipv6_addresses": [],
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
"ansible_bios_date": "04/01/2014",
"ansible_bios_vendor": "SeaBIOS",
"ansible_bios_version": "8c24b4c",
"ansible_board_asset_tag": "NA",
... ...
"ansible_virtualization_type": "kvm",
"gather_subset": [
"all"
],
"module_setup": true
},
"changed": false
}
這個模塊的好處就是讓了解到 Ansible 都有哪些內(nèi)置的變量,這些變量在我們后續(xù)寫playbook的時候邢笙,在role templates 中配置很有幫助啸如,
舉個列子,Ansible 批量部署 zabbix agent氮惯,每個agent的配置文件中的 ListenIP
和 Hostname
我們就可以使用facts中的變量 ansible_default_ipv4.address
和 ansible_hostname
另外一旦知道知道都有哪些變量的時候叮雳,下次我們就可以再收集信息展示的時候,加參數(shù)通過filter來過濾我們想看的變量就可妇汗。比如
# 這里只看 ansible_default_ipv4 變量
(kfz-ansible) [james@devops-jumpserver-vm]$ ansible devops-gitlab-vpc -m setup -a 'filter=ansible_default_ipv4'
devops-gitlab-vpc | SUCCESS => {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "172.17.115.136",
"alias": "eth0",
"broadcast": "172.17.255.255",
"gateway": "172.17.255.253",
"interface": "eth0",
"macaddress": "00:16:3e:2e:6e:cf",
"mtu": 1500,
"netmask": "255.255.0.0",
"network": "172.17.0.0",
"type": "ether"
}
},
"changed": false
}
debug
顧名思義帘不,就是我們想調(diào)試輸出一些結(jié)果的時候,比如上面提到的我想知道目標主機的IP地址
這里有兩種用法杨箭,一種是msg
輸出寞焙,需要帶{{ variable-name }}
, 另外一種是 var
用法互婿,直接寫變量名即可捣郊,不用添加 {{ }}
(kfz-ansible) [james@devops-jumpserver-vm]$ ansible devops-gitlab-vpc -m debug -a 'msg={{ ansible_default_ipv4.address }}'
devops-gitlab-vpc | SUCCESS => {
"msg": "172.17.115.136"
}
(kfz-ansible) [james@devops-jumpserver-vm ]$ ansible devops-gitlab-vpc -m debug -a 'var=ansible_default_ipv4.address'
devops-gitlab-vpc | SUCCESS => {
"ansible_default_ipv4.address": "172.17.115.136"
}
用戶相關(guān)
user/group
遠程管理用戶/用戶組
# 添加組
ansible devops-demo-vpc -m group -a 'name=demogroup'
# 添加用戶
ansible devops-demo-vpc -m user -a 'name=demouser group=demogroup shell=/bin/bash password=newpasswd'
authorized_key
主要用來給目標主機用戶配置公鑰,默認到目標用戶家目錄的.ssh目錄的authorized_keys文件 沒有則創(chuàng)建authorized_keys文件
#
- name: deliver authorized_keys
authorized_key:
user: james
key: "{{ lookup('file', '/etc/ansible/roles/authorized_keys') }}" # 使用 lookup從本地authorized_keys文件讀取公鑰內(nèi)容
state: present # absent刪除key
shell/script/command/raw
這里是把 shell 和 script慈参、command放到一起做對比呛牲,其實還有個raw
其中 command 執(zhí)行單一命令不能使用管道符、重定向符等懂牧,raw 類型command侈净,可以使用管道符等;
shell 和 script 類似僧凤,都可以執(zhí)行腳本畜侦,卻別在于script執(zhí)行的腳本在ansible管理機上,而shell執(zhí)行的腳本必須先放到目標節(jié)點上去躯保,才能執(zhí)行旋膳;
另外shell執(zhí)行可以使用環(huán)境變量,bash等途事,但是script只是執(zhí)行腳本验懊,不能帶 bash
# check remote host load
ansible devops-demo-vpc -m command -a 'uptime'
# check remote host data disk
ansible devops-demo-vpc -m raw -a 'df -h |grep data'
# execute bash script
# check if exist t.sh on remote host, not exist on remote ,exist on local
(kfz-ansible) [root@devops-ansible /data/temp]# ansible devops-demo-vpc -m shell -a ' ls -l /tmp/t.sh'
devops-demo-vpc | FAILED | rc=2 >>
ls: cannot access /tmp/t.sh: No such file or directorynon-zero return code
# if use base when script, error here
(kfz-ansible) [root@devops-ansible /data/temp4]# ansible devops-demo-vpc -m script -a 'bash /data/temp/t.sh'
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: If you are using a module and expect the file to exist on the remote, see the remote_src option
devops-demo-vpc | FAILED! => {
"changed": false,
"msg": "Could not find or access 'bash'\nSearched in:\n\t/data/temp/files/bash\n\t/data/temp/bash\n\t./files/bash\n\t./bash on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"
}
# script on local and use script module to execute on remote host
(kfz-ansible) [root@devops-ansible /data/temp]# ansible devops-demo-vpc -m script -a ' /data/temp/t.sh |grep result'
devops-demo-vpc | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to test-liuchao-01-vm closed.\r\n",
"stderr_lines": [
"Shared connection to test-liuchao-01-vm closed."
],
"stdout": "result: I am on host [test-liuchao-01-vm]\r\n",
"stdout_lines": [
"result: I am on host [test-liuchao-01-vm]"
]
}
(kfz-ansible) [root@devops-ansible /data/temp]# ansible devops-demo-vpc -m copy -a 'src=t.sh dest=/tmp/t-remote.sh mode=0755'
devops-demo-vpc | CHANGED => {
"changed": true,
"checksum": "14d91fbe7abd4e406124460149048fd7d88d2216",
"dest": "/tmp/t-remote.sh",
"gid": 0,
"group": "root",
"md5sum": "916c5f68555199e2019030dd0b3cdc62",
"mode": "0755",
"owner": "root",
"size": 84,
"src": "/root/.ansible/tmp/ansible-tmp-1637916752.9830978-13704-278698223191801/source",
"state": "file",
"uid": 0
}
(kfz-ansible) [root@devops-jumpserver-vm /data/temp Fri Nov 26 16:52:34]# ansible devops-demo-vpc -m shell -a 'bash /tmp/t-remote.sh'
devops-demo-vpc | CHANGED | rc=0 >>
hello Ansible
result: I am on host [devops-demo-vpc]
服務(wù)管理
service / systemd
都是用來管理服務(wù)器上的服務(wù),區(qū)別在于Service服務(wù)管理用于centos6及以前的系統(tǒng)尸变,而systemd命令應(yīng)用于centos7系統(tǒng)
核心參數(shù) name\state\enabled
# enable and start nginx
ansible devops-demo-vpc -m service -a 'name=nginx enabled=true state=started'
# reload
ansible devops-demo-vpc -m service -a 'name=nginx state=reloaded'
文件管理
copy
把管理節(jié)點的文件copy到目標節(jié)點义图,并配置相關(guān)屬性
# 常規(guī)copy文件到目標主機
ansible devops-demo-vpc -m copy -a 'scr=t.sh dest=/tmp/t-remote.sh mode=0755 owner=james group=james'
# 有時候不是copy文件,而是直接指定內(nèi)容
ansible devops-demo-vpc -m copy -a 'content="hello world, hello Ansible" dest=/tmp/t-remote.txt'
# force=yes 遠程存在同名文件則強制覆蓋
ansible devops-demo-vpc -m copy -a 'scr=t.sh dest=/tmp/t-remote.txt force=yes'
# backup是遠程存在同名文件則先備份在覆蓋
ansible devops-demo-vpc -m copy -a 'scr=t.sh dest=/tmp/t-remote.txt backup=yes'
template
template 的作用和copy一樣召烂,區(qū)別在于源文件是jinja2格式碱工,文件中可以配置 Ansible變量,然后在目標節(jié)點上替換成對應(yīng)的目標值
(kfz-ansible) [james@devops-jumpserver-vm]$ ansible devops-baseimage-vpc -m template -a 'src=/tmp/ansible-template-jinja2.j2 dest=/tmp/ansible-template-jinja2.txt mode=0644 '
devops-baseimage-vpc | CHANGED => {
"changed": true,
"checksum": "60fcb9b1049408735a56fd7282254abf52fd6125",
"dest": "/tmp/ansible-template-jinja2.txt",
"gid": 0,
"group": "root",
"md5sum": "ef974d5fd45dddce50b765d6b20d6abe",
"mode": "0644",
"owner": "root",
"size": 61,
"src": "/home/james/.ansible/tmp/ansible-tmp-1637489176.9135442-5845-46738029442091/source",
"state": "file",
"uid": 0
}
(kfz-ansible) [james@devops-jumpserver-vm]$ ansible devops-baseimage-vpc -m shell -a 'cat /tmp/ansible-template-jinja2.txt'
devops-baseimage-vpc | CHANGED | rc=0 >>
Hostname is: devops-baseimage-vpc
Host IP is: 172.17.115.134
另外一個需要住的就是在roles中,copy默認是從files目錄獲取文件怕篷,template默認是 templates 文件夾獲取模板文件
synchronize
主要用于目錄历筝、文件的同步,基于 rsync實現(xiàn)廊谓,主要是有push
和 pull
兩種方式梳猪, 如果是push 推送,則src是管理節(jié)點蒸痹,dest是目標節(jié)點春弥;如果是pull拉取,則src是目標節(jié)點电抚,dest是管理節(jié)點
# 推送至遠程目標節(jié)點
ansible devops-demo-vpc -m synchronize -a 'mode=push src=/opt/scripts dest=/opt/target/scripts recursive=yes archive=yes
# 從遠程目標節(jié)點獲取到本地
ansible devops-demo-vpc -m synchronize -a 'mode=pull src=/opt/target/scripts dest=/opt/scripts recursive=yes archive=yes
file
在目標節(jié)點創(chuàng)建文件或目錄惕稻,刪除文件或目錄,修改文件或目錄的權(quán)限等蝙叛;核心參數(shù)有:path俺祠、state、owner借帘、group蜘渣、mode、recurse
# 創(chuàng)建目錄
ansible devops-demo-vpc -m file -a 'path=/opt/script state=directory'
# 創(chuàng)建文件并設(shè)置屬主肺然、屬組及權(quán)限
ansible devops-demo-vpc -m file -a 'path=/opt/script/test.sh owner=test group=test mode=755'
# 刪除文件
ansible devops-demo-vpc -m file -a 'path=/opt/script/test.sh state=absent'
# 遞歸創(chuàng)建目錄
ansible devops-demo-vpc -m file -a 'path=/opt/script/sub1/sub2/sub3 state=directory recurse=true'
lineinfile
在文件中添加蔫缸、修改、刪除一行記錄际起,在實踐中用的很多拾碌,這里做簡單介紹,后續(xù)有單獨文章詳細介紹
# 在文件的匹配行之前插入一行記(在匹配的 Listen 80這行后面插入 server_name www.colinspace.com)
- name: insert after match line demo
lineinfile:
dest: /etc/nginx.conf
insertafter: '^listen 80'
line: 'server_name www.colinspace.com'
# 修改匹配的行
- name: update match line demo
lineinfile:
dest: /etc/nginx.conf
regex: 'server_name www.*'
line: 'server_name blog.colinspace.com'
mode: 0644
Linux系統(tǒng)維護
yum
顧名思義街望,就是我們在Centos下進行yum安裝校翔,核心參數(shù)主要關(guān)注: name 需要安裝的軟件名、state 軟件的狀態(tài)(present灾前、absent防症、removed晴音、latest)和 enablerepo 特殊情況指定yum源
# 安裝Nginx
ansible devops-demo-vpc -m yum -a 'name=nginx state=present'
# 卸載Nginx (absent和removed一樣)
ansible devops-demo-vpc -m yum -a 'name=nginx state=remove'
cron
管理Linux定時任務(wù)羹饰,核心參數(shù)說明
name 定時任務(wù)的名稱、 state 任務(wù)的狀態(tài)驻谆、minute/hour/day/month/weekday 分別設(shè)定任務(wù)執(zhí)行的時間配置炭玫、user指定是哪個用戶配置任務(wù)奈嘿,默認是管理員
# 每天凌晨 01:05 執(zhí)行腳本
ansible devops-demo-vpc -m cron -a 'name="Demo cron" hour=1 minute=05 job="bash /tmp/1.sh" '
# 刪除上述任務(wù)(注意任務(wù)名保持一致)
ansible devops-demo-vpc -m cron -a 'name="Demo cron" state=absent"
#