亚洲熟女综合色一区二区三区,亚洲精品中文字幕无码蜜桃,亚洲va欧美va日韩va成人网,亚洲av无码国产一区二区三区,亚洲精品无码久久久久久久

Linux基礎(chǔ)教程之Ansible安裝部署及常用模塊詳解

ansible安裝方式

ansible安裝常用兩種方式,yum安裝和pip程序安裝

這里提供二種安裝方式,任選一種即可:

1、使用yum安裝

yum install epel-release -y
yum install ansible –y

2、 使用pip(Python的包管理模塊)安裝

pip install ansible

#如果沒pip,需先安裝pip.yum可直接安裝:
yum install Python-pip
pip install ansible

ansible程序結(jié)構(gòu)

安裝目錄

通過使用rpm -ql ansible指令可以查看ansible安裝的所有文件位置!

配置文件目錄:/etc/ansible/
執(zhí)行文件目錄:/usr/bin/
Lib庫依賴目錄:/usr/lib/PythonX.X/site-packages/ansible/
Help文檔目錄:/usr/share/doc/ansible-X.X.X/
Man文檔目錄:/usr/share/man/man1/

ansible配置文件的查找順序

(1)、檢查環(huán)境變量ANSIBLE_CONFIG指向的路徑文件(export ANSIBLE_CONFIG=/etc/ansible.cfg )
(2)、~/.ansible.cfg,檢查當(dāng)前目錄(/etc/ansible)下的ansible.cfg配置文件
(3)、/etc/ansible.cfg 檢查etc目錄的配置文件

ansible配置文件

設(shè)置/etc/ansible/ansible.cfg配置參數(shù),ansible有許多參數(shù),下面列出常用的參數(shù):

inventory:#這個參數(shù)表示資源清單inventory文件的位置,資源清單就是一些ansible需要連接管理的主 機(jī)列表。這個參數(shù)的配置實例如下:

inventory = /etc/ansible/hosts

library:?ansible的操作動作,無論是本地或遠(yuǎn)程,都使用一小段代碼來執(zhí)行,這小段代碼稱為模塊,這個library參數(shù)就是指向存放ansible模塊的目錄。配置實例如下:

library = /usr/share/ansible

ansible支持多個目錄方式,只要用冒號“ : ”隔開就可以,同時也會檢查當(dāng)前執(zhí)行playbook位置下的./library目錄。

forks:設(shè)置默認(rèn)情況下ansible最多能有多少個進(jìn)程同時工作, 從ansible 1.3開始,fork數(shù)量默認(rèn)自動設(shè)置為主機(jī)數(shù)量或者潛在的主機(jī)數(shù)量,默認(rèn)設(shè)置最多5個進(jìn)程并行處理。具體需要設(shè)置多少個,可以根據(jù)控制主機(jī)的性能和被管節(jié)點的數(shù)量來確定,可能是 50或100。默認(rèn)值5是非常保守的值,配置實例如下:

forks = 5

sudo_user:這是設(shè)置默認(rèn)執(zhí)行命令的用戶,也可以在playbook中重新設(shè)置這個參數(shù)。配置實例如下:

sudo_user = root

remote_port:這是指定連接被管節(jié)點的管理端口,默認(rèn)是22。除非設(shè)置了特殊的SSH端口,不然這個參數(shù)一般是不需要修改的。
配置實例如下:

remote_port = 22

host_key_checking:這是設(shè)置是否檢查SSH主機(jī)的密鑰??梢栽O(shè)置為True或False,關(guān)閉后第一次連接沒有提示配置實例

host_key_checking = False

timeout:這是設(shè)置SSH連接的超時間隔,單位是秒。配置實例如下:

timeout = 60

**log_path:**ansible系統(tǒng)默認(rèn)是不記錄日志的,如果想把a(bǔ)nsible系統(tǒng)的輸出記錄到日志文件中,需要設(shè)置log_path來指定一個存儲ansible日志的文件。配置實例如下:

log_path = /var/log/ansible.log

另外需要注意,執(zhí)行ansible的用戶需要有寫入日志的權(quán)限,模塊將會調(diào)用被管節(jié)點的syslog來記錄。

ansible主機(jī)清單

ansible主機(jī)清單設(shè)置
編輯/etc/ansible/hosts:

[root@CentOS7-master ~]# vim /etc/ansible/hosts  #定義方式:

###1、直接指明主機(jī)地址或主機(jī)名:
green.example.com
blue.example.com
172.17.250.230
172.17.254.180
172.17.254.165

###2、定義一個主機(jī)組[組名]把地址或主機(jī)名加進(jìn)去
[webservers]
172.17.254.180
172.17.254.165
#組成員可以使用通配符來匹配,如下 172.17.254.[1:6] #表示匹配從172.17.254.1——172.17.254.6的主機(jī)

ansible配置公私鑰

配置ansible使用公鑰驗證

配置這個的原因是為了方便ansible可以實現(xiàn)無秘訪問控制其他機(jī)器,是實現(xiàn)自動化的前提。這一步可以再配置好ansible.cfg文件以及hosts主機(jī)清單后執(zhí)行。

雖然ansible支持其他主機(jī)認(rèn)證方式,但是我們最常用的的還是基于秘鑰的認(rèn)證:
1、首先生成秘鑰

##執(zhí)行下條指令后一路回車即可!
[root@CentOS7-master ~]# ssh-keygen -t rsa

2、然后向主機(jī)分發(fā)秘鑰:

##所有添加到主機(jī)清單中的IP地址或者主機(jī)名,全部都要用下條指令執(zhí)行一遍。
[root@CentOS7-master ~]# ssh-copy-id root@主機(jī)名或IP地址

3、如果出現(xiàn)以下情況:

ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.1.6.72
-bash: ssh-copy-id: command not found

#請嘗試:
yum -y install openssh-clientsansible

ansible常用命令

ansible命令集

/usr/bin/ansible    # Ansibe AD-Hoc 臨時命令執(zhí)行工具,常用于臨時命令的執(zhí)行

/usr/bin/ansible-doc    # ansible 模塊功能查看工具

/usr/bin/ansible-galaxy     # 下載/上傳優(yōu)秀代碼或Roles模塊 的官網(wǎng)平臺,基于網(wǎng)絡(luò)的

/usr/bin/ansible-playbook   # ansible 定制自動化的任務(wù)集編排工具

/usr/bin/ansible-pull # ansible遠(yuǎn)程執(zhí)行命令的工具,拉取配置而非推送配置(使用較少,海量機(jī)器時使用,對運(yùn)維的架構(gòu)能力要求較高)

/usr/bin/ansible-vault # ansible 文件加密工具

/usr/bin/ansible-console # ansible基于Linux Consoble界面可與用戶交互的命令執(zhí)行工具

ansible命令詳解

###命令格式:
ansible <host-pattern> [-f forks] [-m module_name] [-a args]

###我們可以通過 ansible -h查看幫助
###本段中所有以“**”開頭的參數(shù),均表示是重要的
[root@CentOS7-master ~]# ansible -h
Usage: ansible <host-pattern> [options] Options:

** -a MODULE_ARGS, --args=MODULE_ARGS
模塊的參數(shù),如果執(zhí)行默認(rèn)COMMAND的模塊,即是命令參數(shù),如:“date”,“pwd”等等 module arguments 模塊參數(shù)

-k, --ask-pass 
    ask for SSH password 登錄密碼,提示輸入SSH密碼而不是假設(shè)基于密鑰的驗證

--ask-su-pass 
    ask for su password su切換密碼

-K, --ask-sudo-pass 
    ask for sudo password 提示密碼使用sudo,sudo表示提權(quán)操作

--ask-vault-pass 
    ask for vault password

-B SECONDS, --background=SECONDS 
    run asynchronously, failing after X seconds (default=N/A)后臺運(yùn)行超時時間

** -C, --check
    don‘t make any changes; instead, try to predict some of the changes that may occur #只是測試一下會改變什么內(nèi)容,不會真正去執(zhí)行;相反,試圖預(yù)測一些可能發(fā)生的變化

-c CONNECTION, --connection=CONNECTION 
    connection type to use (default=smart) #連接類型使用

** -f FORKS, --forks=FORKS
    specify number of parallel processes to use (default=5) #并行任務(wù)數(shù)。NUM被指定為一個整數(shù),默認(rèn)是5 

-h, --help 
    show this help message and exit 打開幫助文檔API

** -i INVENTORY, --inventory-file=INVENTORY
    specify inventory host file (default=/etc/ansible/hosts) #指定庫存主機(jī)文件的路徑,默認(rèn)為/etc/ansible/hosts

** --list-hosts
    outputs a list of matching hosts; does not execute anything else

** -m MODULE_NAME, --module-name=MODULE_NAME
    module name to execute (default=command) #執(zhí)行模塊的名字,默認(rèn)使用 command 模塊,所以如果是只執(zhí)行單一命令可以不用 -m參數(shù)

-M MODULE_PATH, --module-path=MODULE_PATH   
    specify path(s) to module library (default=/usr/share/ansible/) #要執(zhí)行的模塊的路徑,默認(rèn)為/usr/share/ansible/

-o, --one-line 
    condense output #壓縮輸出,摘要輸出.嘗試一切都在一行上輸出。

-P POLL_INTERVAL, --poll=POLL_INTERVAL  
    set the poll interval if using -B (default=15) #調(diào)查背景工作每隔數(shù)秒。需要- b

--private-key=PRIVATE_KEY_FILE 
    use this file to authenticate the connection #私鑰路徑,使用這個文件來驗證連接

** -S, --su
    run operations with su #用 su 命令

** -R SU_USER, --su-user=SU_USER
    run operations with su as this user (default=root) #指定SU的用戶,默認(rèn)是root用戶

** -s, --sudo
    run operations with sudo (nopasswd)

** -U SUDO_USER, --sudo-user=SUDO_USER
    desired sudo user (default=root)(deprecated, use become) #sudo到哪個用戶,默認(rèn)為 root

** -T TIMEOUT, --timeout=TIMEOUT
    override the SSH timeout in seconds (default=10) #指定SSH默認(rèn)超時時間, 默認(rèn)是10S

-t TREE, --tree=TREE 
    log output to this directory #將日志內(nèi)容保存在該輸出目錄,結(jié)果保存在一個文件中在每臺主機(jī)上。

** -u REMOTE_USER, --user=REMOTE_USER
    connect as this user (default=root) #遠(yuǎn)程用戶, 默認(rèn)是root用戶 

--vault-password-file=VAULT_PASSWORD_FILE 
    vault password file

** -v, --verbose
    verbose mode (-vvv for more, -vvvv to enable 詳細(xì)信息connection debugging)

--version 
    show program's version number and exit  #輸出ansible的版本

ansible-doc命令

ansible-doc指令是用來查看ansible所支持的模塊的文件信息的,并且支持shell!

查看 ansible-doc 的使用說明:

###一般用法:(也是常用的用法)
ansible-doc -l 獲取模塊信息
ansible-doc -s MOD_NAME 獲取指定模塊的使用幫助

###ansible-doc
[root@CentOS7-master ~]# ansible-doc -h 
Usage: ansible-doc [options][module...]

Options:
    -h, --help show this help message and exit # 顯示命令參數(shù)API文檔

    -l, --list List available modules #列出可用的模塊

    -M MODULE_PATH, --module-path=MODULE_PATH #指定模塊的路徑specify path(s) to module library (default=None)

    -s, --snippet Show playbook snippet for specified module(s) #顯示playbook制定模塊的用法

    --version show program's version number and exit # 顯示ansible-doc的版本號查看模塊

實例:

[root@CentOS7-master ~]# ansible-doc -l|grep mysql
mysql_db                           Add or remove MySQL databases from a remote host.   
mysql_replication                  Manage MySQL replication                            
mysql_user                         Adds or removes a user from a MySQL database.       
mysql_variables                    Manage MySQL global variables
[root@CentOS7-master ~]# 
[root@CentOS7-master ~]# ansible-doc -l mysql_replication 
a10_server                         Manage A10 Networks AX/SoftAX/Thunder/vThunder devic...
a10_service_group                  Manage A10 Networks devices' service groups         
......
......
......
quantum_router_interface           Attach/Detach a subnet's interface to a router      
quantum_subnet                     Add/remove subnet from a network                    
(END)

ansible常用模塊

1、主機(jī)連通性測試:

ansible all -m ping

執(zhí)行效果如下:

[root@CentOS7-master ~]# ansible all -m ping
172.17.254.165 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
172.17.254.180 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
172.17.250.230 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

若出現(xiàn)以下結(jié)果,則說明未配置主機(jī)列表,則需要在/etc/ansible/hosts?中添加主機(jī)列表:

[root@CentOS7-master ~]# ansible all -m ping
 [WARNING]: provided hosts list is empty, only localhost is available

 [WARNING]: No hosts matched, nothing to do

2、command:在遠(yuǎn)程主機(jī)執(zhí)行命令;不支持|管道命令

命令模塊接受命令名稱,后面是空格分隔的列表參數(shù)。給定的命令將在所有選定的節(jié)點上執(zhí)行。它不會通過shell進(jìn)行處理,比如$HOME和操作如”小于”<“,”>”, “|”, “;”,”&”‘ 工作(需要使用(shell)模塊實現(xiàn)這些功能)。

action: command

chdir       #在執(zhí)行命令之前,先切換到該目錄

creates     #一個文件名,當(dāng)這個文件存在,則該命令不執(zhí)行,可以用來做判斷

executable  #切換shell來執(zhí)行命令,需要使用命令的絕對路徑

free_form   #要執(zhí)行的Linux指令,一般使用ansible的-a參數(shù)代替。

removes     #一個文件名,這個文件不存在,則該命令不執(zhí)行,與creates相反的判斷
[root@CentOS7-master ~]# ansible all -m command -a 'ifconfig'
172.17.250.230 | SUCCESS | rc=0 >>
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.250.230  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::20c:29ff:fefc:13f0  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:fc:13:f0  txqueuelen 1000  (Ethernet)
        RX packets 15924367  bytes 1678705351 (1.5 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8677333  bytes 1589494094 (1.4 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.73.130  netmask 255.255.255.0  broadcast 192.168.73.255
        inet6 fe80::20c:29ff:fefc:13fa  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:fc:13:fa  txqueuelen 1000  (Ethernet)
        RX packets 8003  bytes 1375066 (1.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 78  bytes 5016 (4.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

172.17.254.165 | SUCCESS | rc=0 >>
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.254.165  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::20c:29ff:fe6a:1f0f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:6a:1f:0f  txqueuelen 1000  (Ethernet)
        RX packets 6028867  bytes 489629406 (466.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 322593  bytes 58991897 (56.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.73.137  netmask 255.255.255.0  broadcast 192.168.73.255
        inet6 fe80::20c:29ff:fe6a:1f19  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:6a:1f:19  txqueuelen 1000  (Ethernet)
        RX packets 7859  bytes 1345936 (1.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 20  bytes 1536 (1.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

172.17.254.180 | SUCCESS | rc=0 >>
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.254.180  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::20c:29ff:fedb:9611  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:db:96:11  txqueuelen 1000  (Ethernet)
        RX packets 8231638  bytes 3839144807 (3.5 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2504227  bytes 177542489 (169.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.73.151  netmask 255.255.255.0  broadcast 192.168.73.255
        inet6 fe80::20c:29ff:fedb:961b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:db:96:1b  txqueuelen 1000  (Ethernet)
        RX packets 10665  bytes 1901294 (1.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2600  bytes 521922 (509.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


[root@CentOS7-master ~]# ansible all -m command -a 'chdir=/tmp ls'                    
172.17.254.165 | SUCCESS | rc=0 >>
ansible_nDr_sW
systemd-private-6524f00099a646b28e1484e64e97f459-nginx.service-SDDItJ
systemd-private-6524f00099a646b28e1484e64e97f459-vmtoolsd.service-9zL9Z3

172.17.250.230 | SUCCESS | rc=0 >>
ansible_W9Cey7
systemd-private-cbd6ab787c1047f49bd164e1928bc50c-mariadb.service-ZWF9XI
systemd-private-cbd6ab787c1047f49bd164e1928bc50c-vmtoolsd.service-7NjwSi

172.17.254.180 | SUCCESS | rc=0 >>
ansible_p4DDLJ
hsperfdata_tomcat
systemd-private-e8fcbe84909942deb6ea742ed68dca4e-vmtoolsd.service-7sY3og

3、shell模塊

shell模塊在遠(yuǎn)程主機(jī)上調(diào)用shell解釋器運(yùn)行命令,支持shell的各種功能,例如管道等 :

[root@CentOS7-master ~]# ansible webservers -m shell -a 'cat /etc/passwd |grep "root"'
172.17.254.180 | SUCCESS | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

172.17.254.165 | SUCCESS | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

4、copy:復(fù)制文件到遠(yuǎn)程主機(jī),可以改權(quán)限等

用法:
(1) 復(fù)制文件

-a "src= dest= "

(2) 給定內(nèi)容生成文件

-a "content= dest= "

相關(guān)選項如下:
backup:在覆蓋之前,將源文件備份,備份文件包含時間信息。有兩個選項:yes|no

content:用于替代“src”,可以直接設(shè)定指定文件的值

dest:必選項。要將源文件復(fù)制到的遠(yuǎn)程主機(jī)的絕對路徑,如果源文件是一個目錄,那么該路徑也必須是個目錄

directory_mode:遞歸設(shè)定目錄的權(quán)限,默認(rèn)為系統(tǒng)默認(rèn)權(quán)限

force:如果目標(biāo)主機(jī)包含該文件,但內(nèi)容不同,如果設(shè)置為yes,則強(qiáng)制覆蓋,如果為no,則只有當(dāng)目標(biāo)主機(jī)的目標(biāo)位置不存在該文件時,才復(fù)制。默認(rèn)為yes

others:所有的file模塊里的選項都可以在這里使用

src:被復(fù)制到遠(yuǎn)程主機(jī)的本地文件,可以是絕對路徑,也可以是相對路徑。如果路徑是一個目錄,它將遞歸復(fù)制。在這種情況下,如果路徑使用“/”來結(jié)尾,則只復(fù)制目錄里的內(nèi)容,如果沒有使用“/”來結(jié)尾,則包含目錄在內(nèi)的整個內(nèi)容全部復(fù)制,類似于rsync。
[root@CentOS7-master ~]# ansible webservers -m copy -a 'content="hello\nworld" dest=/tmp/test.ansible mode=666'   
172.17.254.165 | SUCCESS => {
    "changed": true, 
    "checksum": "7db827c10afc1719863502cf95397731b23b8bae", 
    "dest": "/tmp/test.ansible", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "9195d0beb2a889e1be05ed6bb1954837", 
    "mode": "0666", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 11, 
    "src": "/root/.ansible/tmp/ansible-tmp-1512465712.79-212682551827039/source", 
    "state": "file", 
    "uid": 0
}
172.17.254.180 | SUCCESS => {
    "changed": true, 
    "checksum": "7db827c10afc1719863502cf95397731b23b8bae", 
    "dest": "/tmp/test.ansible", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "9195d0beb2a889e1be05ed6bb1954837", 
    "mode": "0666", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 11, 
    "src": "/root/.ansible/tmp/ansible-tmp-1512465712.87-100512759759497/source", 
    "state": "file", 
    "uid": 0
}

5、file 設(shè)置文件屬性:

創(chuàng)建目錄:-a "path= state=directory"
創(chuàng)建鏈接文件:-a "path= src= state=link"
刪除文件:-a "path= state=absent"
force:需要在兩種情況下強(qiáng)制創(chuàng)建軟鏈接,一種是源文件不存在,但之后會建立的情況下;另一種是目標(biāo)軟鏈接已存在,需要先取消之前的軟鏈,然后創(chuàng)建新的軟鏈,有兩個選項:yes|no

group:定義文件/目錄的屬組 mode:定義文件/目錄的權(quán)限

owner:定義文件/目錄的屬主 path:必選項,定義文件/目錄的路徑

recurse:遞歸設(shè)置文件的屬性,只對目錄有效 src:被鏈接的源文件路徑,只應(yīng)用于state=link的情況

dest:被鏈接到的路徑,只應(yīng)用于state=link的情況
    state:
        directory:如果目錄不存在,就創(chuàng)建目錄
        file:即使文件不存在,也不會被創(chuàng)建
        link:創(chuàng)建軟鏈接
        hard:創(chuàng)建硬鏈接
        touch:如果文件不存在,則會創(chuàng)建一個新的文件,如果文件或目錄已存在,則更新其最后修改時間
        absent:刪除目錄、文件或者取消鏈接文件
[root@CentOS7-master ~]# ansible webservers -m file -a 'path=/tmp/test state=directory' 
172.17.254.165 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/test", 
    "secontext": "unconfined_u:object_r:user_tmp_t:s0", 
    "size": 6, 
    "state": "directory", 
    "uid": 0
}
172.17.254.180 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/test", 
    "secontext": "unconfined_u:object_r:user_tmp_t:s0", 
    "size": 6, 
    "state": "directory", 
    "uid": 0
}
[root@CentOS7-master ~]# 
[root@CentOS7-master ~]# 
[root@CentOS7-master ~]# ansible webservers -m file -a 'path=/data/web.sh state=touch'
172.17.254.165 | SUCCESS => {
    "changed": true, 
    "dest": "/data/web.sh", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:default_t:s0", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}
172.17.254.180 | SUCCESS => {
    "changed": true, 
    "dest": "/data/web.sh", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:default_t:s0", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}
[root@CentOS7-master ~]# 
[root@CentOS7-master ~]# ansible webservers -m file -a 'path=/data/web.sh state=file' 
172.17.254.165 | SUCCESS => {
    "changed": false, 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "path": "/data/web.sh", 
    "secontext": "unconfined_u:object_r:default_t:s0", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}
172.17.254.180 | SUCCESS => {
    "changed": false, 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "path": "/data/web.sh", 
    "secontext": "unconfined_u:object_r:default_t:s0", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}

6、fetch從遠(yuǎn)程某主機(jī)獲取文件到本地:

dest:用來存放文件的目錄,例如存放目錄為backup,源文件名稱為/etc/profile在主機(jī)Pythonserver中,那么保存為/backup/Pythonserver/etc/profile;

Src:在遠(yuǎn)程拉取的文件,并且必須是一個file,不能是目錄。

[root@CentOS7-master ~]# ansible webservers -m fetch -a 'src=/var/log/messages dest=/root'
172.17.254.165 | SUCCESS => {
    "changed": true, 
    "checksum": "8065eb47956eaa59d3ca64a2c69a38084ae7505d", 
    "dest": "/root/172.17.254.165/var/log/messages", 
    "md5sum": "c864d77846965436f8d7feb660c103a1", 
    "remote_checksum": "d7f2d4876e65962444963531e4ed10b319e7b277", 
    "remote_md5sum": null
}
172.17.254.180 | SUCCESS => {
    "changed": true, 
    "checksum": "0d0829396b7dfdc1951c3ec35fa266f3195c6763", 
    "dest": "/root/172.17.254.180/var/log/messages", 
    "md5sum": "7a3f75bb76aed49282bc7cbc3142448c", 
    "remote_checksum": "0d0829396b7dfdc1951c3ec35fa266f3195c6763", 
    "remote_md5sum": null
}

7、cron管理cron計劃任務(wù)

ansible all -m cron -a “ ”: 設(shè)置管理節(jié)點生成定時任務(wù)

action: cron backup=    #如果設(shè)置,創(chuàng)建一個crontab備份 [yes|no]
cron_file=  #如果指定, 使用這個文件cron.d,而不是單個用戶

crontab

day=    #日應(yīng)該運(yùn)行的工作( 1-31, *, */2, )

hour=   #小時 ( 0-23, *, */2, )

minute=     #分鐘( 0-59, *, */2, )

month=  #月( 1-12, *, /2, )

weekday     #周 ( 0-6 for Sunday-Saturday,, )

job=    #指明運(yùn)行的命令是什么

name=   #定時任務(wù)描述

reboot  #任務(wù)在重啟時運(yùn)行,不建議使用,建議使用special_time

special_time    #特殊的時間范圍,參數(shù):reboot(重啟時),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小時)

state   #指定狀態(tài),present表示添加定時任務(wù),也是默認(rèn)設(shè)置,absent表示刪除定時任務(wù)

user    #以哪個用戶的身份執(zhí)行
ansible webservers -m cron -a 'name="sync time from ntpserver" minute="*/10" job="/sbin/ntpdate asia.pool.ntp.org &>/dev/null" '

8、yum安裝軟件

conf_file   #設(shè)定遠(yuǎn)程yum安裝時所依賴的配置文件。如配置文件沒有在默認(rèn)的位置。
disable_gpg_check   #是否禁止GPG checking,只用于‘present’ or `latest’。
disablerepo     #臨時禁止使用yum庫。 只用于安裝或更新時。
enablerepo  #臨時使用的yum庫。只用于安裝或更新時。

#下面帶“**”的都是重點的
** name=    #所安裝的包的名稱
** state=present|latest|absent  #present安裝,latest安裝最新的,absent 卸載軟件。
** update_cache     #強(qiáng)制更新yum的緩存。
[root@CentOS7-master ~]# ansible 172.17.254.165 -m yum -a 'name=htop state=present'
172.17.254.165 | SUCCESS => {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package htop.x86_64 0:2.0.2-1.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package         Arch              Version                Repository       Size\n================================================================================\nInstalling:\n htop            x86_64            2.0.2-1.el7            epel             98 k\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal download size: 98 k\nInstalled size: 207 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : htop-2.0.2-1.el7.x86_64                                      1/1 \n  Verifying  : htop-2.0.2-1.el7.x86_64                                      1/1 \n\nInstalled:\n  htop.x86_64 0:2.0.2-1.el7                                                     \n\nComplete!\n"
    ]
}

9、service: 服務(wù)程序管理

arguments   #命令行提供額外的參數(shù)
enabled=true|false  #設(shè)置開機(jī)啟動。
name=   #服務(wù)名稱
runlevel    #開機(jī)啟動的級別,一般不用指定。
sleep   #在重啟服務(wù)的過程中,是否等待。如在服務(wù)關(guān)閉以后等待2秒再啟動。
state=  #started啟動服務(wù), stopped停止服務(wù), restarted重啟服務(wù), reloaded重載配置
ansible 172.17.254.165 -m service -a 'name=nginx state=stopped enabled=true'

10、user模塊管理

用戶模塊,管理用戶帳號 action: user

comment     #用戶的描述信息
createhome  #是否創(chuàng)建家目錄
force       #在使用state=absent是, 行為與userdel –force一致.
group       #指定基本組
groups      #指定附加組,如果指定為(groups=)表示刪除所有組
home        #指定用戶家目錄
move_home   #如果設(shè)置為home=時, 試圖將用戶主目錄移動到指定的目錄
name        #指定用戶名
non_unique  #該選項允許改變非唯一的用戶ID值
password    #指定用戶密碼
remove      #在使用state=absent時, 行為是與userdel –remove一致
shell       #指定默認(rèn)shell
state       #設(shè)置帳號狀態(tài),不指定為創(chuàng)建,指定值為absent表示刪除
system      #當(dāng)創(chuàng)建一個用戶,設(shè)置這個用戶是系統(tǒng)用戶。這個設(shè)置不能更改現(xiàn)有用戶
uid         #指定用戶的uid
update_password     #更新用戶密碼
#創(chuàng)建一個name為tom,uid為1006,shell為、bin/zshell,home為/home/tomhome的用戶
[root@CentOS7-master ~]# ansible webservers -m user -a 'name=tom comment="tom is tom" uid=1066 shell=/bin/zshell home=/home/tomhome'           
172.17.254.165 | SUCCESS => {
    "changed": true, 
    "comment": "tom is tom", 
    "createhome": true, 
    "group": 1066, 
    "home": "/home/tomhome", 
    "name": "tom", 
    "shell": "/bin/zshell", 
    "state": "present", 
    "system": false, 
    "uid": 1066
}
172.17.254.180 | SUCCESS => {
    "changed": true, 
    "comment": "tom is tom", 
    "createhome": true, 
    "group": 1066, 
    "home": "/home/tomhome", 
    "name": "tom", 
    "shell": "/bin/zshell", 
    "state": "present", 
    "system": false, 
    "uid": 1066
}

11、group

用戶組模塊,添加或刪除組:action: group

gid # 設(shè)置組的GID號
name= # 管理組的名稱
state # 指定組狀態(tài),默認(rèn)為創(chuàng)建,設(shè)置值為absent為刪除
system # 設(shè)置值為yes,表示為創(chuàng)建系統(tǒng)組
[root@CentOS7-master ~]# ansible webservers -m group -a 'name=tom state=present'
172.17.254.165 | SUCCESS => {
    "changed": false, 
    "gid": 1066, 
    "name": "tom", 
    "state": "present", 
    "system": false
}
172.17.254.180 | SUCCESS => {
    "changed": false, 
    "gid": 1066, 
    "name": "tom", 
    "state": "present", 
    "system": false
}

12、script在指定節(jié)點運(yùn)行服務(wù)端的腳本

[root@CentOS7-master ~]# vim test.sh
#/bin/bash

#創(chuàng)建/tmp/test.sh.log
touch /tmp/test.sh.log

#將date命令結(jié)果輸出到/tmp/test.sh.log
echo “hello” >> /tmp/test.sh.log

在webservers組中所有主機(jī)執(zhí)行/root/test.sh腳本

[root@CentOS7-master ~]# ansible webservers -m script -a '/root/test.sh'

13、setup模塊

facts組件是ansible用于采集被管機(jī)器設(shè)備信息的一個功能,我們可以使用setup模塊查機(jī)器的所有facts信息,可以使用filter來查看指定信息。整個facts信息被包裝在一個JSON格式的數(shù)據(jù)結(jié)構(gòu)中,ansible_facts是最上層的值。

facts就是變量,內(nèi)建變量 。每個主機(jī)的各種信息,cpu顆數(shù)、內(nèi)存大小等。會存在facts中的某個變量中。調(diào)用后返回很多對應(yīng)主機(jī)的信息,在后面的操作中可以根據(jù)不同的信息來做不同的操作。如redhat系列用yum安裝,而debian系列用apt來安裝軟件。

setup模塊,主要用于獲取主機(jī)信息,在playbooks里經(jīng)常會用到的一個參數(shù)gather_facts就與該模塊相關(guān)。

setup模塊下經(jīng)常使用的一個參數(shù)是filter參數(shù),具體使用示例如下(由于輸出結(jié)果較多,這里只列命令不寫結(jié)果):

ansible 172.17.254.165 -m setup -a 'filter=ansible*mb' //查看主機(jī)內(nèi)存信息
ansible 172.17.254.180 -m setup -a 'filter=ansible_ens3[2-3]' //查看地接口為eth0-2的網(wǎng)卡信息

ansible all -m setup --tree /tmp/facts //將所有主機(jī)的信息輸入到/tmp/facts目錄下,每臺主機(jī)的信息輸入到主機(jī)名文件中(/etc/ansible/hosts里的主機(jī)名)

相關(guān)新聞

歷經(jīng)多年發(fā)展,已成為國內(nèi)好評如潮的Linux云計算運(yùn)維、SRE、Devops、網(wǎng)絡(luò)安全、云原生、Go、Python開發(fā)專業(yè)人才培訓(xùn)機(jī)構(gòu)!