1.ansible實作管理的方式
Ad-Hoc ##利用ansible指令直接完成管理,主要用于臨時指令使用場景
playbook ##ansible腳本,主要用于大型項目場景,需要前期的規劃
2.Ad-Hoc執行方式中如何獲得幫助
ansible-doc ##顯示子產品幫助的指令
2.1 格式
ansible-doc [參數] [子產品...]
2.2 常用參數
-l ##列出可用子產品
-s ##顯示指定子產品的playbook片段
[[email protected] ~]# ansible-doc -l | wc -l
3387
[[email protected] ~]# ansible-doc -s shell
- name: Execute shell commands on targets
shell:
chdir: # Change into this directory before running the command.
cmd: # The command to run followed by optional arguments.
creates: # A filename, when it already exists, this step will *not* be run.
executable: # Change the shell used to execute the command. This expects an absolute path to the executable.
free_form: # The shell module takes a free form command to run, as a string. There is no actual parameter named 'free form'. See
the examples on how to use this module.
removes: # A filename, when it does not exist, this step will *not* be run.
stdin: # Set the stdin of the command directly to the specified value.
stdin_add_newline: # Whether to append a newline to stdin data.
warn: # Whether to enable task warnings.
[[email protected] ~]#
3.ansible指令運作方式及常用參數
3.1 格式:
ansible 清單 -m 子產品 -a 子產品參數
3.2 常用參數
--version ##顯示版本
-m module ##指定子產品,預設為command子產品
--list ##顯示主機清單,也可以用--list-hosts
-v ##詳細過程 -vv -vvv更詳細過程
-k ##提示輸入ssh連接配接密碼,預設key認證
-C ##預執行檢測
-T ##執行指令的逾時時間,預設10s#
-u ##指定遠端執行的使用者
-b ##執行sudo切換身份操作
-become-user=USERNAME ##指定sudo的使用者
-K ##提示輸入sudo密碼
4.ansible的基本顔色代表信
綠色 ##執行成功但為對遠端主機做任何改變
黃色 ##執行成功并對遠端主機做改變
紅色 ##執行失敗
5.ansible中的常用子產品
5.1 command
注意:Linux中的很多通配符在command子產品中不支援
功能: 在遠端主機執行指令,此子產品為預設子產品
常用參數:
chdir | ##執行指令前先進入到指定目錄 |
cmd | ##運作指令指定 |
creates | ##如果檔案存在将不運作 |
removes | ##如果檔案存在将運作 |
free_form | ##在遠端主機中執行的指令,此參數不需要加 |
在westos清單主機中建立使用者lee
[[email protected] .ansible]$ ansible westos -m command -a "useradd lee" -u root -k
SSH password:
172.25.32.12 | CHANGED | rc=0 >>
172.25.32.11 | CHANGED | rc=0 >>
在westos清單主機中删除使用者lee
[[email protected] .ansible]$ ansible westos -m command -a "userdel lee" -u root -k
SSH password:
172.25.32.12 | CHANGED | rc=0 >>
172.25.32.11 | CHANGED | rc=0 >>
檢視westos清單主機中/etc/passwd/的最後一行
[[email protected] .ansible]$ ansible westos -m command -a "chdir=/etc tail -n1 passwd" -u root -k
SSH password:
172.25.32.12 | CHANGED | rc=0 >>
admin:x:1000:1000::/home/admin:/bin/bash
172.25.32.11 | CHANGED | rc=0 >>
admin:x:1000:1000::/home/admin:/bin/bash
在westos清單主機中如果/etc/passwd存在的話就不運作tail指令,如果不檔案存在就運作tail
[[email protected] .ansible]$ ansible westos -m command -a "chdir=/etc creates=/etc/passwd tail -n1 passwd" -u root -k
SSH password:
172.25.32.12 | SUCCESS | rc=0 >>
skipped, since /etc/passwd exists
172.25.32.11 | SUCCESS | rc=0 >>
skipped, since /etc/passwd exists
在westos清單主機中如果/etc/passwd存在的話就運作tail指令,如果檔案不存在就不運作tail
[[email protected] .ansible]$ ansible westos -m command -a "chdir=/etc removes=/etc/passwd tail -n1 passwd" -u root -k
SSH password:
172.25.32.12 | CHANGED | rc=0 >>
admin:x:1000:1000::/home/admin:/bin/bash
172.25.32.11 | CHANGED | rc=0 >>
admin:x:1000:1000::/home/admin:/bin/bash
5.2 shell
功能: 和command功能類似
常用參數:
chdir | ##執行指令前先進入到指定目錄 |
cmd | ##運作指令指定 |
creates | ##如果檔案存在将不運作 |
removes | ##如果檔案存在在将運作 |
free_form | ##在遠端主機中執行的指令,此參數不需要加 |
executable | ##指定執行環境,預設為sh |
指定執行環境為/bin/bash,預設為sh
[[email protected] .ansible]$ ansible westos -m shell -a "executable=sh ps ax | grep $$ " -k
SSH password:
172.25.32.11 | CHANGED | rc=0 >>
4628 pts/1 S+ 0:00 sh -c ps ax | grep 3496
4630 pts/1 S+ 0:00 grep 3496
172.25.32.12 | CHANGED | rc=0 >>
4656 pts/1 S+ 0:00 sh -c ps ax | grep 3496
4658 pts/1 S+ 0:00 grep 3496
檢視目前目錄所在的程序
[[email protected] .ansible]$ ansible westos -m shell -a ' ps ax | grep $$'
172.25.32.12 | CHANGED | rc=0 >>
4765 pts/1 S+ 0:00 /bin/sh -c ps ax | grep $$
4767 pts/1 S+ 0:00 grep 4765
172.25.32.11 | CHANGED | rc=0 >>
4737 pts/1 S+ 0:00 /bin/sh -c ps ax | grep $$
4739 pts/1 S+ 0:00 grep 4737
檢視目前正在運作的程序
[[email protected] .ansible]$ ansible westos -m shell -a 'ps'
172.25.32.12 | CHANGED | rc=0 >>
PID TTY TIME CMD
4864 pts/1 00:00:00 sudo
4865 pts/1 00:00:00 sh
4866 pts/1 00:00:00 python
4867 pts/1 00:00:00 ps
172.25.32.11 | CHANGED | rc=0 >>
PID TTY TIME CMD
4835 pts/1 00:00:00 sudo
4836 pts/1 00:00:00 sh
4837 pts/1 00:00:00 python
4838 pts/1 00:00:00 ps
5.3 script
功能: 在ansible主機中寫好的腳本在受控主機中執行
[[email protected] .ansible]$ exit ##回到超級使用者中
logout
[[email protected] ~]# vim /mnt/westos.sh
[[email protected] ~]# cat /mnt/westos.sh
#!/bin/bash
echo $HOSTNAME
[[email protected] ~]# ansible westos -m script -a "/mnt/westos.sh" -k
SSH password:
172.25.32.12 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 172.25.32.12 closed.\r\n",
"stderr_lines": [
"Shared connection to 172.25.32.12 closed."
],
"stdout": "node2\r\n",
"stdout_lines": [
"node2"
]
}
172.25.32.11 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 172.25.32.11 closed.\r\n",
"stderr_lines": [
"Shared connection to 172.25.32.11 closed."
],
"stdout": "node1\r\n",
"stdout_lines": [
"node1"
]
}
5.4 copy
功能:從ansible主機複制檔案到受控主機
常用參數
src | ##源檔案 |
dest | ##目的地檔案 |
owner/group | ##指定目的地檔案所有人 |
mode | ##指定目的地檔案權限 |
backup=yes | ##當受控主機中存在檔案時備份原檔案 |
content | ##指定文本内容直接在受控主機中生成檔案 |
将/mnt/westos.sh/複制到westos清單被控主機的/mnt/中,當被控主機中存在westos.sh時備份原檔案,檔案所有人為admin,權限為777
[[email protected] .ansible]$ ansible westos -m copy -a "src=/mnt/westos.sh dest=/mnt/westos.sh owner=admin mode=777 backup=yes"
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "25a5e82036293f48d4a117c91855a16c2d36e0de",
"dest": "/mnt/westos.sh",
"gid": 0,
"group": "root",
"md5sum": "2b9854338cd858ad0f86eb55423c3f03",
"mode": "0777",
"owner": "admin",
"size": 27,
"src": "/home/admin/.ansible/tmp/ansible-tmp-1659037428.57-4624-78249132552260/source",
"state": "file",
"uid": 1000
}
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "25a5e82036293f48d4a117c91855a16c2d36e0de",
"dest": "/mnt/westos.sh",
"gid": 0,
"group": "root",
"md5sum": "2b9854338cd858ad0f86eb55423c3f03",
"mode": "0777",
"owner": "admin",
"size": 27,
"src": "/home/admin/.ansible/tmp/ansible-tmp-1659037428.58-4626-250519756246854/source",
"state": "file",
"uid": 1000
}
在westos清單被控主機的/mnt/目錄下生成檔案westosfile1,檔案内容為hello westos/hello linux,檔案所有人為admin,權限為777
[[email protected] .ansible]$ ansible westos -m copy -a "content='hello westos\nhello linux\n' dest=/mnt/westosfile1 owner=admin mode=600"
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "7edbc023b406807d55423480b2bfd908870d5919",
"dest": "/mnt/westosfile1",
"gid": 0,
"group": "root",
"mode": "0600",
"owner": "admin",
"path": "/mnt/westosfile1",
"size": 25,
"state": "file",
"uid": 1000
}
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "7edbc023b406807d55423480b2bfd908870d5919",
"dest": "/mnt/westosfile1",
"gid": 0,
"group": "root",
"mode": "0600",
"owner": "admin",
"path": "/mnt/westosfile1",
"size": 25,
"state": "file",
"uid": 1000
}
檢視westos清單被控主機的/mnt/目錄
[[email protected] .ansible]$ ansible westos -m shell -a "ls /mnt"
172.25.32.11 | CHANGED | rc=0 >>
westos.sh
westosfile1
172.25.32.12 | CHANGED | rc=0 >>
westos.sh
westosfile1
[[email protected] .ansible]$ ansible westos -m shell -a "cat /mnt/westosfile1"
172.25.32.11 | CHANGED | rc=0 >>
hello westos
hello linux
172.25.32.12 | CHANGED | rc=0 >>
hello westos
hello linux
5.5 fetch
功能: 從受控主機把檔案複制到ansible主機,但不支援目錄
常用參數
src | ##受控主機的源檔案 |
dest | ##本機目錄 |
flat | ##基本名稱功能 |
将受控主機/mnt/westosfile1複制到主機的/mnt/目錄下
[[email protected] mnt]# ansible 172.25.32.11 -m fetch -a "src=/mnt/westosfile1 dest=/mnt" -k
SSH password:
172.25.32.11 | CHANGED => {
"changed": true,
"checksum": "7edbc023b406807d55423480b2bfd908870d5919",
"dest": "/mnt/172.25.32.11/mnt/westosfile1",
"md5sum": "e79f6eb05e162f95e496e8d4d8a24275",
"remote_checksum": "7edbc023b406807d55423480b2bfd908870d5919",
"remote_md5sum": null
}
将受控主機複制到主機檔案名字改為file
[[email protected] mnt]# ansible 172.25.32.11 -m fetch -a "src=/mnt/westosfile1 dest=/mnt/file flat=yes" -k
SSH password:
172.25.32.11 | CHANGED => {
"changed": true,
"checksum": "7edbc023b406807d55423480b2bfd908870d5919",
"dest": "/mnt/file",
"md5sum": "e79f6eb05e162f95e496e8d4d8a24275",
"remote_checksum": "7edbc023b406807d55423480b2bfd908870d5919",
"remote_md5sum": null
}
5.6 file
功能: 設定檔案的屬性
常用參數
path | 指定檔案名稱 |
state | 指定操作狀态 |
touch absent directory link hard | 建立 删除 遞歸 建立軟連結 建立硬連接配接 |
mode | 設定權限 |
group/owner | 設定檔案組/設定檔案使用者 |
src | 源檔案 |
dest | 目标檔案 |
recurse=yes | 遞歸更改 |
建立檔案
[[email protected] .ansible]$ ansible westos -m file -a 'path=/mnt/test.sh state=touch'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/mnt/test.sh",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
删除檔案
[[email protected] .ansible]$ ansible westos -m file -a 'path=/mnt/test.sh state=absent'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/mnt/test.sh",
"state": "absent"
}
建立目錄
[[email protected] .ansible]$ ansible westos -m file -a 'path=/mnt/westos state=directory'
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/mnt/westos",
"size": 6,
"state": "directory",
"uid": 0
}
遞歸修改目錄權限
[[email protected] .ansible]$ ansible westos -m file -a 'path=/mnt/westos state=directory mode=777 recurse=yes'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"path": "/mnt/westos",
"size": 6,
"state": "directory",
"uid": 0
}
生成軟連結
[[email protected] .ansible]$ ansible westos -m file -a 'src=/mnt/westosfile1 dest=/mnt/westos state=link'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/mnt/westos",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 16,
"src": "/mnt/westosfile1",
"state": "link",
"uid": 0
}
生成硬連接配接
[[email protected] .ansible]$ ansible westos -m file -a 'src=/mnt/westosfile1 dest=/mnt/westos1 state=hard'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/mnt/westos1",
"gid": 0,
"group": "root",
"mode": "0600",
"owner": "admin",
"size": 25,
"src": "/mnt/westosfile1",
"state": "hard",
"uid": 1000
}
建立檔案時設定權限及所有人,所有組
[[email protected] .ansible]$ ansible westos -m file -a 'path=/mnt/file state=touch owner=admin group=admin mode=777'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/mnt/file",
"gid": 1000,
"group": "admin",
"mode": "0777",
"owner": "admin",
"size": 0,
"state": "file",
"uid": 1000
}
5.7 archive
作用: 壓縮
常用參數
path | 打包目錄名稱 |
path | 聲稱打封包件名稱 |
format | 打包格式 |
owner | 指定檔案所屬人 |
mode | 指定檔案權限 |
[[email protected] .ansible]$ ansible all -m archive -a 'path=/etc dest=/opt/etc.tar.gz format=gz owner=admin mode=700' -k
SSH password:
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"archived": [
"/etc/fstab",
"/etc/crypttab",
"/etc/mtab",
"/etc/resolv.conf",
"/etc/my.cnf",
"/etc/issue",
"/etc/issue.net",
"/etc/libuser.conf",
........
5.8 unarchive
功能:解壓縮
常用參數
copy | 預設為yes 從ansible主機複制檔案到受控主機 設定為no 從受控主機中尋找src源檔案 |
remote_src | 功能同copy且相反 設定為yes 表示包在受控主機 設定為no表示包在ansible主機 |
src | 包路徑,可以使ansible主機也可以使受控主機 |
dest | 受控主機目錄 |
mode | 加壓後檔案權限 <copy=yes> |
ansible westos -m unarchive -a 'src=/opt/etc.tar.gz dest=/mnt owner=admin' #把主要機中/opt/etc.tar.gz解壓到受控機/mnt裡,解壓後所有人是admin
把受控機中/opt/etc.tar.gz解壓到受控機/mnt裡,copy=no等同于remote_src=yes
[[email protected] .ansible]$ ansible westos -m unarchive -a "src=/opt/etc.tar.gz dest=/mnt copy=no"
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/mnt",
"extract_results": {
"cmd": [
"/bin/gtar",
"--extract",
"-C",
"/mnt",
"-z",
"-f",
"/opt/etc.tar.gz"
],
"err": "",
"out": "",
"rc": 0
},
"gid": 0,
"group": "root",
"handler": "TgzArchive",
"mode": "0755",
"owner": "root",
"size": 112,
"src": "/opt/etc.tar.gz",
"state": "directory",
"uid": 0
}
5.9 hostname
作用: 管理主機名稱
常用參數:name ##指定主機名稱
[[email protected] .ansible]$ ansible 172.25.32.11 -m hostname -a 'name=www.westos.org'
172.25.32.11 | CHANGED => {
"ansible_facts": {
"ansible_domain": "westos.org",
"ansible_fqdn": "www.westos.org",
"ansible_hostname": "www",
"ansible_nodename": "www.westos.org",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "www.westos.org"
}
5.10 cron
作用:計劃任務
常用參數
minute | ##分鐘 |
hour | ##小時 |
day | ##天 |
month | ##月 |
weekday | ##周 |
name | ##任務名稱 |
job | ##任務腳本或指令 |
disabled | ##yes 禁用計劃任務 ##no 啟動計劃任務 |
state | ##absent 删除計劃任務 |
在11:11分的時候在/mnt目錄建立linux檔案
[[email protected] .ansible]$ ansible westos -m cron -a 'job="touch /mnt/linux" name=test minute=11 hour=11 '
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"test"
]
}
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"test"
]
}
禁止執行這個11:11分的時候在/mnt目錄建立linux檔案的任務
[[email protected] .ansible]$ ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test minute=11 hour=11 disabled=yes'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"test"
]
}
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
删除這個11:11分的時候在/mnt目錄建立linux檔案的任務
[[email protected] .ansible]$ ansible westos -m cron -a 'job="touch /mnt/linux" name=test minute=11 hour=11 state=absent'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": []
}
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": []
}
"changed": true,
"envs": [],
"jobs": [
"test"
]
}
5.11 yum_repository
作用:配置系統軟體倉庫源檔案
name | ##指定倉庫名稱 |
baseurl | ##指定源路徑 |
description | ##指定倉庫描述 |
file | ##指定倉庫檔案名稱 |
enabled | ##倉庫是否啟用 |
gpgcheck | ##倉庫是否檢測gpgkey |
state | ##預設值present建立/#absent 為删除 |
建立軟體倉庫源
[[email protected] .ansible]$ ansible westos -m yum_repository -a "name=AppStream baseurl=http://172.25.32.250/rhel7.6/AppStream description=AppStream gpgcheck=no file=westos" -k
SSH password:
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"repo": "AppStream",
"state": "present"
}
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"repo": "AppStream",
"state": "present"
}
删除建立的軟體倉庫源
[[email protected] .ansible]$ ansible westos -m yum_repository -a "name=AppStream file=westos_test state=absent" -k
SSH password:
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"repo": "AppStream",
"state": "absent"
}
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"repo": "AppStream",
"state": "absent"
}
5.11 yum
作用: 管理系統中的dnf倉庫及管理軟體
name | ##指定包 |
state | ##指定動作 #present 安裝 #latest 更新 #absent 删除 |
list | ##列出指定資訊 |
disable_gpg_check | #禁用gpgkey檢測 |
enablerepo | ##指定安裝包來源 |
disablerepo | ##禁用安裝包來源 |
給被控機安裝httpd服務
[[email protected] .ansible]$ ansible westos -m yum -a "name=httpd state=present"
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"httpd"
]
},
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: product-id, search-disabled-repos, subscription-manager\nThis system is not registered with an entitlement server. You can use subscription-manager to register.\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-88.el7 will be installed\n--> Processing Dependency: httpd-tools = 2.4.6-88.el7 for package: httpd-2.4.6-88.el7.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-88.el7.x86_64\n--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-88.el7.x86_64\n--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-88.el7.x86_64\n--> Running transaction check\n---> Package apr.x86_64 0:1.4.8-3.el7_4.1 will be installed\n---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed\n---> Package httpd-tools.x86_64 0:2.4.6-88.el7 will be installed\n---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.6-88.el7 AppStream 1.2 M\nInstalling for dependencies:\n apr x86_64 1.4.8-3.el7_4.1 AppStream 103 k\n apr-util x86_64 1.5.2-6.el7 AppStream 92 k\n httpd-tools x86_64 2.4.6-88.el7 AppStream 90 k\n mailcap noarch 2.1.41-2.el7 AppStream 31 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package (+4 Dependent packages)\n\nTotal download size: 1.5 M\nInstalled size: 4.3 M\nDownloading packages:\n--------------------------------------------------------------------------------\nTotal 33 MB/s | 1.5 MB 00:00 \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : apr-1.4.8-3.el7_4.1.x86_64 1/5 \n Installing : apr-util-1.5.2-6.el7.x86_64 2/5 \n Installing : httpd-tools-2.4.6-88.el7.x86_64 3/5 \n Installing : mailcap-2.1.41-2.el7.noarch 4/5 \n Installing : httpd-2.4.6-88.el7.x86_64 5/5 \n Verifying : mailcap-2.1.41-2.el7.noarch 1/5 \n Verifying : httpd-2.4.6-88.el7.x86_64 2/5 \n Verifying : apr-1.4.8-3.el7_4.1.x86_64 3/5 \n Verifying : apr-util-1.5.2-6.el7.x86_64 4/5 \n Verifying : httpd-tools-2.4.6-88.el7.x86_64 5/5 \n\nInstalled:\n httpd.x86_64 0:2.4.6-88.el7 \n\nDependency Installed:\n apr.x86_64 0:1.4.8-3.el7_4.1 apr-util.x86_64 0:1.5.2-6.el7 \n httpd-tools.x86_64 0:2.4.6-88.el7 mailcap.noarch 0:2.1.41-2.el7 \n\nComplete!\n"
]
}
解除安裝httpd服務,但不删除依賴關系
[[email protected] .ansible]$ ansible westos -m yum -a 'name=httpd state=absent autoremove=no'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"removed": [
"httpd"
]
},
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: product-id, search-disabled-repos, subscription-manager\nThis system is not registered with an entitlement server. You can use subscription-manager to register.\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-88.el7 will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nRemoving:\n httpd x86_64 2.4.6-88.el7 @AppStream 3.7 M\n\nTransaction Summary\n================================================================================\nRemove 1 Package\n\nInstalled size: 3.7 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Erasing : httpd-2.4.6-88.el7.x86_64 1/1 \n Verifying : httpd-2.4.6-88.el7.x86_64 1/1 \n\nRemoved:\n httpd.x86_64 0:2.4.6-88.el7 \n\nComplete!\n"
]
}
解除安裝httpd服務,也解除安裝依賴關系
[[email protected] .ansible]$ ansible westos -m yum -a 'name=httpd state=absent autoremove=yes'
172.25.32.12 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"msg": "",
"rc": 0,
"results": [
"httpd is not installed"
]
}
指定下載下傳的源(通過AppStream來安裝)
[[email protected] .ansible]$ ansible westos -m yum -a 'name=httpd state=present enablerepo=AppStream'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"httpd"
]
},
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: product-id, search-disabled-repos, subscription-manager\nThis system is not registered with an entitlement server. You can use subscription-manager to register.\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-88.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.6-88.el7 AppStream 1.2 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 1.2 M\nInstalled size: 3.7 M\nDownloading packages:\nDelta RPMs disabled because /usr/bin/applydeltarpm not installed.\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : httpd-2.4.6-88.el7.x86_64 1/1 \n Verifying : httpd-2.4.6-88.el7.x86_64 1/1 \n\nInstalled:\n httpd.x86_64 0:2.4.6-88.el7 \n\nComplete!\n"
]
}
列出httpd的相關資訊
[[email protected] .ansible]$ ansible westos -m yum -a 'name=httpd state=absent autoremove=yes'
172.25.32.12 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"msg": "",
"rc": 0,
"results": [
"httpd is not installed"
]
}
更新httpd服務
[[email protected] .ansible]$ ansible westos -m yum -a 'name="httpd" state=latest'
172.25.32.11 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"changes": {
"installed": [],
"updated": []
},
"msg": "",
"rc": 0,
"results": [
"All packages providing httpd are up to date",
""
]
}
5.13 service
作用: 管理系統服務狀态
常用參數
name | ##指定服務名稱 |
state | ##指定對服務的動作 #started #stoped #restarted #reloaded |
enabled | ##設定服務開機是否啟動 #yes開啟啟動 #no開機不啟動 |
開啟httpd服務,并指定開機啟動
[[email protected] .ansible]$ ansible westos -m service -a "name=httpd state=started enabled=yes"
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "inactive",
"After": "-.mount network.target basic.target system.slice remote-fs.target tmp.mount systemd-journald.socket nss-lookup.target",
"AllowIsolate": "no",
"AmbientCapabilities": "0",
........
重新開機httpd服務
[[email protected] .ansible]$ ansible westos -m service -a "name=httpd state=restarted enabled=yes"
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveEnterTimestamp": "Fri 2022-07-29 07:34:50 UTC",
"ActiveEnterTimestampMonotonic": "4964679459",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "active",
"After": "-.mount systemd-journald.socket network.target nss-lookup.target basic.target tmp.mount remote-fs.target system.slice",
"AllowIsolate": "no",
"AmbientCapabilities": "0",
5.14 firewalld
常用參數
zone | ##火牆的域 |
service | ##服務名稱 |
permanent | ##永久生效 |
state | ##允許 enabled ##拒絕 disabled |
immediate | ##立即生效 |
開啟火牆并永久指定火牆的域為public 且立即生效,
[[email protected] .ansible]$ ansible westos -m firewalld -a 'zone=public service=http permanent=yes state=enabled immediate=yes'
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"msg": "Permanent and Non-Permanent(immediate) operation, Changed service http to enabled"
}
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"msg": "Permanent and Non-Permanent(immediate) operation, Changed service http to enabled"
}
在被控主機中:
[[email protected] ~]# firewall-cmd --list-all
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh dhcpv6-client http
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
5.15 user
作用: 子產品可以幫助我們管理遠端主機上的使用者,比如建立使用者、修改使用者、删除使用者、為使用者建立密鑰對等操作
name | ##必須參數,用于指定要操作的使用者名稱。 |
group | ##指定使用者所在的基本組。 |
gourps | ##指定使用者所在的附加組。 |
append | ##指定添加附加組預設值為no |
shell | ##指定使用者的預設shell。 |
uid | ##指定使用者的uid号。 |
comment | ##指定使用者的注釋資訊。 |
state | ##用于指定使用者是否存在于遠端主機 #present 建立 #absent 删除 |
remove | ##當删除使用者是删除使用者家目錄,預設值為no |
password | ##此參數用于指定使用者的密碼。但密碼為明文, ##可以用openssl password -6 '密碼'生成加密字元 |
generate_ssh_key | ##生成sshkey |
建立lee使用者
[[email protected] .ansible]$ ansible westos -m user -a 'name=lee'
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1001,
"home": "/home/lee",
"name": "lee",
"shell": "/bin/bash",
"state": "present",
"stderr": "useradd: warning: the home directory already exists.\nNot copying any file from skel directory into it.\nCreating mailbox file: File exists\n",
"stderr_lines": [
"useradd: warning: the home directory already exists.",
"Not copying any file from skel directory into it.",
"Creating mailbox file: File exists"
],
"system": false,
"uid": 1001
}
删除lee使用者
[[email protected] .ansible]$ ansible westos -m user -a 'name=lee state=absent'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": false,
"name": "lee",
"remove": false,
"state": "absent"
}
指定lee使用者的uid為6666
[[email protected] .ansible]$ ansible westos -m user -a 'name=lee uid=6666'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 6666,
"home": "/home/lee",
"name": "lee",
"shell": "/bin/bash",
"state": "present",
"stderr": "useradd: warning: the home directory already exists.\nNot copying any file from skel directory into it.\nCreating mailbox file: File exists\n",
"stderr_lines": [
"useradd: warning: the home directory already exists.",
"Not copying any file from skel directory into it.",
"Creating mailbox file: File exists"
],
"system": false,
"uid": 6666
}
指定lee使用者所在的組為admin
[[email protected] .ansible]$ ansible westos -m user -a 'name=lee group=admin'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"append": false,
"changed": true,
"comment": "",
"group": 1000,
"home": "/home/lee",
"move_home": false,
"name": "lee",
"shell": "/bin/bash",
"state": "present",
"uid": 6666
}
指定使用者所在的附加組為admin
[[email protected] .ansible]$ ansible westos -m user -a 'name=lee groups=admin'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"append": false,
"changed": true,
"comment": "",
"group": 1000,
"groups": "admin",
"home": "/home/lee",
"move_home": false,
"name": "lee",
"shell": "/bin/bash",
"state": "present",
"uid": 6666
}
生成加密字元【$符是特殊字元,所有要用轉譯字元】
[[email protected] .ansible]$ openssl passwd -1 'westos' #設定密碼
$1$oD/nYgUs$ztibP8DFmgBBgAxM4r6i/.
[[email protected] .ansible]$ ansible westos -m user -a 'name=lee password="$1$oD/nYgUs$ztibP8DFmgBBgAxM4r6i/."'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 100,
"home": "/home/lee",
"name": "lee",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"stderr": "useradd: warning: the home directory already exists.\nNot copying any file from skel directory into it.\nCreating mailbox file: File exists\n",
"stderr_lines": [
"useradd: warning: the home directory already exists.",
"Not copying any file from skel directory into it.",
"Creating mailbox file: File exists"
],
"system": false,
"uid": 1001
}
生成密鑰
[[email protected] .ansible]$ ansible westos -m user -a 'name=lee generate_ssh_key=yes'
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"append": false,
"changed": true,
"comment": "",
"group": 100,
"home": "/home/lee",
"move_home": false,
"name": "lee",
"shell": "/bin/bash",
"ssh_fingerprint": "2048 SHA256:5w9/Fcx+J8KIFc1HtjAyMXi/aB76xdPUewv/aGZSG6M ansible-generated on www.westos.org (RSA)",
"ssh_key_file": "/home/lee/.ssh/id_rsa",
"ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCx53ERXMA8HSsuLcqyoKcwynKuE2Iirn5zOD+6rHMHh+grpJZ/KvrxhMOOyrAMXS81Lm7+qksct2522bnsY7ARB4g6vANtkdM3GrYqffy1/tCAwO4X6HOrPrS3WuX3Fc7M++plvrxt6ze5RSxnRIcDUwRRKeeKmwsHCcpHKNdVYrM/BlBuKfj7ecwMOYZEWGCm2/yeoParqK5d5psy/58yiGclQvMUEl1/8Atguwxsh/T2Ta2pALMLWcWUDYsYaDxl8pKrwnXK0IntPF+b2eGa5Z9HoBS1H32ZBEjb/xGb9WAy0mn8ip4/xEW9qN6PE1RXvAl8ihSvTJw8zNMqcsWF ansible-generated on www.westos.org",
"state": "present",
"uid": 1001
}
5.16 group
作用: group 子產品可以幫助我們管理遠端主機上的組。
常用參數
name | ##用于指定要操作的組名稱。 |
state | ##用于指定組的狀态 #present 建立 #absent 删除 |
gid | ##用于指定組的gid。 |
添加組westoslee
[[email protected] .ansible]$ ansible westos -m group -a 'name=westoslee'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 6667,
"name": "westoslee",
"state": "present",
"system": false
}
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 6667,
"name": "westoslee",
"state": "present",
"system": false
}
指定westoslee組的gid為8888
[[email protected] .ansible]$ ansible westos -m group -a 'name=westoslee gid=8888'
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 8888,
"name": "westoslee",
"state": "present",
"system": false
}
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 8888,
"name": "westoslee",
"state": "present",
"system": false
}
删除westoslee組
[[email protected] .ansible]$ ansible westos -m group -a 'name=westoslee state=absent'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "westoslee",
"state": "absent"
}
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "westoslee",
"state": "absent"
}
5.17 lineinfile
path | ##指定要操作的檔案。 |
line | ##指定文本内容。 "|+" 表示格式化輸入 |
regexp | ##使用正規表達式比對對應的行當替換文本時 ##如果有多行文本都能被比對 ##則隻有最後面被比對到的那行文本才會被替換 ##當删除文本時,如果有多行文本都能被比對 ##這麼這些行都會被删除。 |
state | ##當想要删除對應的文本時需要将state參數的值設定為absent #state的預設值為present。 |
backrefs | ##當内容無比對規則時不對檔案做任何更改,預設值為no ##向後引用regexp變量資訊 |
insertafter | ##借助insertafter參數可以将文本插入到“指定的行”之後 ##insertafter參數的值可以設定為EOF或者正規表達式 |
insertbefore | ##借助insertbefore參數可以将文本插入到“指定的行”之前 #insertbefore參數的值可以設定為BOF或者正規表達式 |
backup | ##是否在修改檔案之前對檔案進行備份。 |
create | ##當要操作的檔案并不存在時,是否建立對應的檔案。 |
給被控機建立/mnt下的westos檔案,并編寫内容\n表示換行
[[email protected] .ansible]$ ansible westos -m copy -a 'content="hello westos\nhello test\nhello linux\n" dest=/mnt/westos '
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "868d8bfc146c9de5569f3fca88677b0f35abf30e",
"dest": "/mnt/westos",
"gid": 0,
"group": "root",
"md5sum": "b035847bf1e123742bd8e58647178d0d",
"mode": "0644",
"owner": "root",
"size": 36,
"src": "/home/admin/.ansible/tmp/ansible-tmp-1659096272.79-5119-87992278885050/source",
"state": "file",
"uid": 0
}
在已經存在的文本(/mnt/westos)中寫入nihao
[[email protected] .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="nihao"'
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
把以hello開頭的行替替換成hello westos,比對到多行的替換隻能替換最後一行,其他行不進行替換
[[email protected] .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos regexp="^hello" line="hello westos" '
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
把以hello開頭的行全部删除 ,比對到多行的删除會全部删除
[[email protected] .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos regexp="^hello" line="hello westos" '
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
将westos檔案中滿足條件【h後邊的任意四個字元,中間任意字元,w後任意五個字元】的行替換為字元\1 {因為 backrefs=no就不向後引用regexp}
[[email protected] .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}.*(w.{5}))" line="\1" backrefs=no'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
将westos檔案中滿足條件【h後邊的任意四個字元,中間任意字元,w後任意五個字元】的行替換為regexp的第一部分條件
[[email protected] .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\1" backrefs=yes'
172.25.32.12 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup": "",
"changed": false,
"msg": ""
}
在檔案中最後一行後添加#######ok##########
[[email protected] .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="#######ok##########" insertafter=EOF'
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
在hello字元前添加#######ok##########;比對到多行,則在最後一行有hello字元前添加
[[email protected] .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="#######ok##########" insertbefore=BOF'
172.25.32.11 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup": "",
"changed": false,
"msg": ""
}
在第一行前添加#######ok##########
[[email protected] .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="#######ok##########" insertbefore=BOF'
172.25.32.11 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup": "",
"changed": false,
"msg": ""
}
在test字元前一行添加
[[email protected] .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="#######ok##########" insertbefore=test'
172.25.32.12 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup": "",
"changed": false,
"msg": ""
}
5.18 line |+
[[email protected] .ansible]$ cat westos.yml
- name: test
hosts: westos
tasks:
- lineinfile:
path: /mnt/westos
line: |+
westos
linux
lee
[[email protected] .ansible]$ ansible-playbook westos.yml
PLAY [test] ***********************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************
ok: [172.25.32.12]
ok: [172.25.32.11]
TASK [lineinfile] *****************************************************************************************************************
changed: [172.25.32.12]
changed: [172.25.32.11]
PLAY RECAP ************************************************************************************************************************
172.25.32.11 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.25.32.12 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
5.19 replace
作用 : 子產品可以根據我們指定的正規表達式替換檔案中的字元串,檔案中所有被比對到的字元串都會被替換
常用參數
path | ##指定要操作的檔案 |
regexp | ##指定一個正規表達式 #檔案中與正則比對的字元串将會被替換。 |
replace | ##指定最終要替換成的字元串。 |
backup | ##是否在修改檔案之前對檔案進行備份,最好設定為yes。 |
把帶有westos字元的全部替換成lee,并且備份westos原檔案
[[email protected] .ansible]$ ansible westos -m replace -a 'path=/mnt/westos regexp="westos" replace="lee" backup=yes'
172.25.32.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup_file": "/mnt/[email protected]:35:34~",
"changed": true,
"msg": "1 replacements made"
}
172.25.32.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup_file": "/mnt/[email protected]:35:33~",
"changed": true,
"msg": "1 replacements made"
}
5.20 setup
作用: setup子產品用于收集遠端主機的一些基本資訊
常用參數: filter ##用于進行條件過濾。如果設定,僅傳回比對過濾條件的資訊。
顯示被控機的主機名
[[email protected] .ansible]$ ansible westos -m setup -a "filter='ansible_fqdn'"
172.25.32.12 | SUCCESS => {
"ansible_facts": {
"ansible_fqdn": "lb-182-230.above.com",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
172.25.32.11 | SUCCESS => {
"ansible_facts": {
"ansible_fqdn": "www.westos.org",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
顯示被控機的ip位址
[[email protected] .ansible]$ ansible westos -m setup -a "filter='ansible_all_ipv4_addresses'"
172.25.32.12 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.25.32.12"
],
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
172.25.32.11 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.25.32.11"
],
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
5.21 debug
作用:調試子產品,用于在調試中輸出資訊
常用參數
msg: | ##調試輸出的消息 |
var: | ##将某個任務執行的輸出作為變量傳遞給debug子產品 ##debug會直接将其列印輸出 |
verbosity: | ##debug的級别(預設是0級,全部顯示) |
輸出hello
[[email protected] .ansible]$ ansible westos -m debug -a 'msg=hello'
172.25.32.11 | SUCCESS => {
"msg": "hello"
}
172.25.32.12 | SUCCESS => {
"msg": "hello"
}
輸出被控機的主機名【不能用ansible指令,因為看不到結果,但是可以在playbook中看到效果】
[[email protected] .ansible]$ cat test.yml
- name: test
hosts: westos
tasks:
- name: debug
debug:
var: ansible_facts['fqdn']
[[email protected] .ansible]$ ansible-playbook test.yml
PLAY [test] ***********************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************
ok: [172.25.32.12]
ok: [172.25.32.11]
TASK [debug] **********************************************************************************************************************
ok: [172.25.32.11] => {
"ansible_facts['fqdn']": "www.westos.org"
}
ok: [172.25.32.12] => {
"ansible_facts['fqdn']": "lb-182-230.above.com"
}
PLAY RECAP ************************************************************************************************************************
172.25.32.11 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.25.32.12 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0