96.ansible安裝包和管理 playbook相關

24.21 ansible安裝包和管理服務linux

24.22 使用ansible playbooknginx

24.23 playbook裏的變量git

24.24 playbook裏的循環redis

24.25 playbook裏的條件判斷sql

24.26 playbook中的handlersdocker

 

 

 

 

24.21 ansible安裝包和管理服務shell

 

 

 

1.ansible testhost -m yum -a "name=httpd"vim

#yum模塊centos

在name後面還能夠加上state=installed(安裝,能夠不加,默認就是安裝)/removed (卸載)服務器

2.ansible testhost -m service -a "name=httpd state=started enabled=yes"

#運行httpd並開機啓動

這裏的name是centos系統裏的服務名,能夠經過chkconfig --list查到。

3.Ansible文檔的使用 #若是忘記選項能夠查詢

ansible-doc -l   列出全部的模塊

ansible-doc cron  查看指定模塊的文檔

 

 

實例:

[root@axinlinux-01 ~]# ansible axinlinux-02 -m yum -a "name=httpd"

axinlinux-02 | SUCCESS => {

"ansible_facts": {

"pkg_mgr": "yum"

},

"changed": false,

"msg": "",

"rc": 0,

"results": [

"httpd-2.4.6-88.el7.centos.x86_64 providing httpd is already installed"

]

}

[root@axinlinux-01 ~]# ansible axinlinux-02 -m yum -a "name=httpd state=removed" #卸載httpd

[root@axinlinux-02 ~]# rpm -qa httpd #02上看一下就沒有了

[root@axinlinux-01 ~]# ansible axinlinux-02 -m service -a "name=httpd state=started enabled=no" #開啓httpd並不開啓啓動

[root@axinlinux-02 ~]# ps aux |grep httpd #查看是否啓動

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

24.22 使用ansible playbook

 

 

就是把全部的配置搞到一個配置文件裏去,這樣直接執行配置文件就好了。而不是用命令行一行一行的去執行。就像以上幾節,執行命令要敲很長的命令,很麻煩

 

1.至關於把模塊寫入到配置文件裏面,例:

vi  /etc/ansible/test.yml //加入以下內容 #後綴名是.yml的

--- #固定格式,表示開頭(頂格)

- hosts: aming-02 #針對那些機器去操做(也是頂格),也能夠寫主機組,如testhost

remote_user: root #用哪一個用戶的身份去作

tasks: #具體的任務是什麼

- name: test_playbook #注意空格(四個)

shell: touch /tmp/lishiming.txt #用到的shell模塊

說明: 第一行須要有三個槓,hosts參數指定了對哪些主機進行參做,若是是多臺機器能夠用逗號做爲分隔,也可使用主機組,在/etc/ansible/hosts裏定義;

user參數指定了使用什麼用戶登陸遠程主機操做;

tasks指定了一個任務,其下面的name參數一樣是對任務的描述,在執行過程當中會打印出來,shell是ansible模塊名字

2.執行:ansible-playbook test.yml

 

 

 

實例:

[root@axinlinux-01 ~]# cd /etc/ansible/

[root@axinlinux-01 ansible]# vim test.yml

---

- hosts: axinlinux-02

remote_user: root

tasks:

- name: test_playbook

shell: touch /tmp/lishiming.txt

[root@axinlinux-01 ansible]# ansible-playbook test.yml #執行這個文件就能夠了

[root@axinlinux-02 ~]# ls -l /tmp/lishiming.txt #查看一下

-rw-r--r-- 1 root root 0 12月 10 15:04 /tmp/lishiming.txt

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

 

24.23 playbook裏的變量

 

 

 

再來一個建立用戶的例子:

vi /etc/ansible/create_user.yml //加入以下內容

---

- name: create_user

hosts: aming-02

user: root

gather_facts: false #負責收集客戶機的屬性,相似於saltstack的grains(ip、版本等等)

vars: #變量

- user: "test" #user變量的值是test

tasks:

- name: create user

user: name="{{ user }}" #user模塊,至關於直接建立用戶。user等於test,在這引用他

說明:name參數對該playbook實現的功能作一個概述,後面執行過程當中,會打印 name變量的值 ,能夠省略;gather_facts參數指定了在如下任務部分執行前,是否先執行setup模塊獲取主機相關信息,這在後面的task會使用到setup獲取的信息時用到;vars參數,指定了變量,這裏指字一個user變量,其值爲test ,須要注意的是,變量值必定要用引號引住;user提定了調用user模塊,name是user模塊裏的一個參數,而增長的用戶名字調用了上面user變量的值。

 

 

 

實例:

[root@axinlinux-01 ansible]# vim create_user.yml

---

- name: create_user

hosts: axinlinux-02

user: root

gather_facts: false

vars:

- user: "test"

tasks:

- name: create user

user: name="{{ user }}"

[root@axinlinux-01 ansible]# ansible-playbook create_user.yml

[root@axinlinux-02 ~]# id test

uid=1003(test) gid=1003(test) 組=1003(test)

[root@axinlinux-01 ansible]# ansible-playbook create_user.yml

axinlinux-02 : ok=1 changed=0 unreachable=0 failed=0 #再次執行就表明有這個用戶了,就會提示changed=0,沒有改變

 

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

24.24 playbook裏的循環

 

 

 

vi /etc/ansible/while.yml //加入以下內容

---

- hosts: testhost

user: root

tasks:

- name: change mode for files

file: path=/tmp/{{ item }} mode=600 #files模塊。變量item,包含一、二、3.txt。至關於循環了三次,把這個三個文件改爲600

with_items: #先寫上面的變量{{ item }} ,再在這些with_items:,也就是這個變量是什麼。注意空格

- 1.txt

- 2.txt

- 3.txt

說明: with_items爲循環的對象

由於他回去收集其餘機器的屬性,若是機器不少的狀況下,能夠加一行gather_facts: false,把它禁掉就能夠了

執行 ansible-playbook while.yml

 

 

實例:

[root@axinlinux-01 ansible]# vim while.yml

---

- hosts: axinlinux-02

user: root

tasks:

- name: change mode for files

file: path=/tmp/{{ item }} state=touch mode=600 #由於02機器上沒有這三個文件,因此加了state=touch建立

with_items:

- 1.txt

- 2.txt

- 3.txt

[root@axinlinux-01 ansible]# ansible-playbook while.yml #執行這個文件就能夠了

[root@axinlinux-02 ~]# ls -l /tmp/*.txt #檢查一下

-rw------- 1 root root 2374 12月 10 16:03 /tmp/1.txt

-rw------- 1 root root 0 12月 10 16:03 /tmp/2.txt

-rw------- 1 root root 0 12月 10 16:03 /tmp/3.txt

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

24.25 playbook裏的條件判斷

 

 

 

當出現什麼狀況的時候,纔會怎麼樣

vi /etc/ansible/when.yml //加入以下內容

---

- hosts: testhost #這裏爲testhost,由於下面判斷語句,要在全部的機器裏去判斷是否符合

user: root

gather_facts: True #True表示要去收集信息。固然默認就是收集,也能夠刪掉這行

tasks:

- name: use when

shell: touch /tmp/when.txt #建立這個文件。也就是當下面一行的條件成立的時候,纔回去建立

when: ansible_ens33.ipv4.address == "192,168,208.130"

#條件判斷。實例中講解

說明:ansible aming-02 -m setup 能夠查看到全部的facter信息 #上面爲True,所收集到的什麼信息。咱們上面用ip做爲判斷,都是在這裏面找的

when判斷不只僅針對gather_facts,也可針對其餘的。好比這個機器上的文件或目錄是否存在、文件屬性等等

 

 

 

實例:

[root@axinlinux-01 ansible]# vim when.yml

---

- hosts: axinlinux-02

user: root

gather_facts: True

tasks:

- name: use when

shell: touch /tmp/when.txt

when: ansible_ens33.ipv4.address == "192,168,208.130"

 

 

[root@axinlinux-01 ansible]# ansible -m setup axinlinux-02 #查看02機器的gather_facts內容,由於咱們的判斷條件就在這裏面判斷的。用到了setup模塊

"ansible_ens33": { #判斷語句的最上一級

"active": true,

"device": "ens33",

"features": {

!!!中間的數據省略了!!!

},

"hw_timestamp_filters": [],

"ipv4": { #判斷語句的下一級

"address": "192.168.208.130", #判斷語句的在下一級。只能這樣一級一級的寫才能得出這個ip

"broadcast": "192.168.208.255",

"netmask": "255.255.255.0",

"network": "192.168.208.0"

},

條件判斷中,when: ansible_ens33.ipv4.address == "192,168,208.130" ,ansible_ens33爲一級。然後面的ipv4爲一級,address爲一級。也就是ansible_ens33的下面是是ipv4,在下面是address。因此咱們想要得出這個ip,就要這麼寫。這是分級的,用 點 (.)來分隔。

由於咱們使用這個ip來做爲判斷的。而後用其餘的做爲判斷,好比"active": true, 就得這樣寫when:ansible_ens33.active。判斷他是否是分級的,看他的空格是否是在上一級的後面!!

[root@axinlinux-01 ansible]# ansible-playbook when.yml #運行

[root@axinlinux-02 ~]# ls -l /tmp/*.txt

-rw-r--r-- 1 root root 0 12月 10 16:52 /tmp/when.txt

 

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

 

24.26 playbook中的handlers

 

 

 

 

至關於shell裏面的&&。執行成功之後在執行下一個命令

應用場景:可使用在修改nginx等配置文件,而後重啓或加載的時候

如下就是,當tasks執行成功之後再去執行handlers。用notify,將他們關聯起來!!!

執行task以後,服務器發生變化以後要執行的一些操做,好比咱們修改了配置文件後,須要重啓一下服務 vi /etc/ansible/handlers.yml//加入以下內容

---

- name: handlers test

hosts: aming-02

user: root

tasks:

- name: copy file

copy: src=/etc/passwd dest=/tmp/aaa.txt #用到的copy模塊。來源地址和目標地址

notify: test handlers #就是copy模塊執行成功之後,我要在運行一個handlers,能夠多個handlers

handlers:

- name: test handlers #這裏的名字就是上面notify定義的名字

shell: echo "111111" >> /tmp/aaa.txt #用到的shell模塊

說明,只有copy模塊真正執行後,纔會去調用下面的handlers相關的操做。也就是說若是1.txt和2.txt內容是同樣的,並不會去執行handlers裏面的shell相關命令。 這種比較適合配置文件發生更改後,重啓服務的操做。

 

 

 

 

實例:

[root@axinlinux-01 ansible]# vim handlers.yml

---

- name: handlers test

hosts: axinlinux-02

user: root

tasks:

- name: copy file

copy: src=/etc/passwd dest=/tmp/aaa.txt

notify: test handlers

handlers:

- name: test handlers

shell: echo "111111" >> /tmp/aaa.txt

[root@axinlinux-01 ansible]# ansible-playbook handlers.yml

[root@axinlinux-02 ~]# tail /tmp/aaa.txt

mongod:x:993:989:mongod:/var/lib/mongo:/bin/false

gitlab-www:x:992:988::/var/opt/gitlab/nginx:/bin/false

git:x:991:987::/var/opt/gitlab:/bin/sh

gitlab-redis:x:990:986::/var/opt/gitlab/redis:/bin/false

gitlab-psql:x:989:985::/var/opt/gitlab/postgresql:/bin/sh

gitlab-prometheus:x:988:984::/var/opt/gitlab/prometheus:/bin/sh

zhangsan:x:1011:1011::/home/jail/./home/zhangsan:/usr/sbin/jk_chrootsh

redis:x:987:983:Redis Database Server:/var/lib/redis:/sbin/nologin

dockerroot:x:986:982:Docker User:/var/lib/docker:/sbin/nologin

111111

相關文章
相關標籤/搜索