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 (卸载)bash

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.tx

---
- hosts: testhost
  user: root
  tasks:
    - name: change mode for files
      file: path=/tmp/{{ item }} mode=600
      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 ~]# 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]# 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
相关文章
相关标签/搜索