首页
学习
活动
专区
圈层
工具
发布

Ansible 搭建与配置(Ⅱ)

一、Ansible 安装包和管理服务

安装httpd服务:

代码语言:javascript
复制
ansible testhost -m yum -a "name=httpd"

卸载httpd服务( 在name后面还可以加上state=installed/removed ):

代码语言:javascript
复制
ansible testhost -m yum -a "name=httpd state=removed"

这里的name是centos系统里的服务名,可以通过chkconfig –list查到。

代码语言:javascript
复制
ansible testhost -m service -a "name=httpd state=started enabled=yes"
Ansible文档的使用
代码语言:javascript
复制
ansible-doc -l   列出所有的模块
ansible-doc cron   查看指定模块的文档

出现的问题:
代码语言:javascript
复制
[[email protected] sbin]# ansible testhost -m service -a "name=httpd state=started enabled=yes"
192.168.59.138 | FAILED! => {
    "changed": false, 
    "failed": true, 
    "msg": "Unable to start service httpd: Job for httpd.service failed because the control process exited with error code. See \"systemctl status httpd.service\" and \"journalctl -xe\" for details.\n"
}
192.168.59.139 | FAILED! => {
    "changed": false, 
    "failed": true, 
    "msg": "Unable to start service httpd: Job for httpd.service failed because the control process exited with error code. See \"systemctl status httpd.service\" and \"journalctl -xe\" for details.\n"
}

去客户端查找错误:

代码语言:javascript
复制
[[email protected] conf.d]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 三 2017-11-08 16:09:04 CST; 1min 23s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 2194 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
  Process: 2193 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
 Main PID: 2193 (code=exited, status=1/FAILURE)

11月 08 16:09:04 zhdy02 httpd[2193]: (98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
11月 08 16:09:04 zhdy02 httpd[2193]: (98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80
11月 08 16:09:04 zhdy02 httpd[2193]: no listening sockets available, shutting down
11月 08 16:09:04 zhdy02 httpd[2193]: AH00015: Unable to open logs
11月 08 16:09:04 zhdy02 systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE
11月 08 16:09:04 zhdy02 kill[2194]: kill: cannot find process ""
11月 08 16:09:04 zhdy02 systemd[1]: httpd.service: control process exited, code=exited status=1
11月 08 16:09:04 zhdy02 systemd[1]: Failed to start The Apache HTTP Server.
11月 08 16:09:04 zhdy02 systemd[1]: Unit httpd.service entered failed state.
11月 08 16:09:04 zhdy02 systemd[1]: httpd.service failed.

这边显示没有颜色,重点不是太清晰。慢慢分析也能分析的出来。

代码语言:javascript
复制
11月 08 16:09:04 zhdy02 httpd[2193]: (98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
11月 08 16:09:04 zhdy02 httpd[2193]: (98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80

意思是 80端口已经在使用,咋整 去查看端口谁用的呗,之前安装git-lab会使用到80, nginx会使用到80,jumpserver也会使用到80。得了 直接关掉,禁掉开机启动。

再次启动远程安装,搞定!

二、ansible playbook

playbook 和之前saltstack讲的playbook是一个原理,在说白了和shell脚本一样,相当于把模块写入到配置文件里面去执行!

代码语言:javascript
复制
vi /etc/ansible/test.yml //加入如下内容
---
- hosts: testhost
  remote_user: root
  tasks:
    - name: test_playbook
      shell: touch /tmp/zhdy.txt

说明:第一行需要有三个杠,hosts参数指定了对哪些主机进行参作,如果是多台机器可以用逗号作为分隔,也可以使用主机组,在/etc/ansible/hosts里定义;

user参数指定了使用什么用户登录远程主机操作;

tasks指定了一个任务,其下面的name参数同样是对任务的描述,在执行过程中会打印出来,shell是ansible模块名字

执行

代码语言:javascript
复制
ansible-playbook test.yml

如上的粉红色:告诉我们 “你要是只是创建一个文件,那不如使用 file modul” -m file

客户端检查:

代码语言:javascript
复制
[[email protected] conf.d]# ls -l /tmp/zhdy.txt 
-rw-r--r-- 1 root root 0 11月  8 16:32 /tmp/zhdy.txt
[[email protected] conf.d]# date 
2017年 11月 08日 星期三 16:32:35 CST
2.1 ansible playbook 变量详解

再来一个创建用户的例子:

代码语言:javascript
复制
vi /etc/ansible/create_user.yml //加入如下内容
---
- name: create_user
  hosts: testhost
  user: root
  gather_facts: false
  vars:
    - user: "test"
  tasks:
    - name: create user
      user: name="{{ user }}"

说明

  • name参数:对该playbook实现的功能做一个概述,后面执行过程中,会打印 name变量的值 ,可以省略。
  • gather_facts参数:指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到。
  • vars参数:指定了变量,这里指字一个user变量,其值为test ,需要注意的是,变量值一定要用引号引住。
  • user:提定了调用user模块,name是user模块里的一个参数,而增加的用户名字调用了上面user变量的值。
客户端检测:
代码语言:javascript
复制
[[email protected] conf.d]# id test
uid=1002(test) gid=1002(test) 组=1002(test)
2.2 playbook 循环

前提需要在客户端先创建如下文件:

代码语言:javascript
复制
ansible testhost -m shell -a "touch /tmp/{1,2,3}.txt"

或者 如下我直接加入(state=touch):

代码语言:javascript
复制
vi /etc/ansible/while.yml //加入如下内容
---
- hosts: testhost
  user: root
  tasks:
    - name: change mode for files
      file: path=/tmp/{{ item }}  state=touch mode=600
      with_items:
        - 1.txt
        - 2.txt
        - 3.txt

说明: with_items为循环的对象
执行 ansible-playbook while.yml

客户端测试:

代码语言:javascript
复制
-rw------- 1 root root    0 11月  8 17:10 1.txt
-rw------- 1 root root    0 11月  8 17:10 2.txt
-rw------- 1 root root    0 11月  8 17:10 3.txt
2.3 playbook中的条件判断

判断,类似于shell中的if语句,当满足情况才回去执行,不满足就输出报错,playbook也是这个道理。

代码语言:javascript
复制
vi /etc/ansible/when.yml //加入如下内容
---
- hosts: testhost
  user: root
  gather_facts: True
  tasks:
    - name: use when
      shell: touch /tmp/when.txt
      when: ansible_ens33.ipv4.address == "192.168.59.138"
    
//以ansible_ens33.ipv4.address为判断对象(ansible_ens33是最上面一级,ipv4是第二级),且值为 "192.168.59.138" 的机器筛选出来。
说明:ansible zhdy01 -m setup 可以查看到所有的facter信息。
代码语言:javascript
复制
-rw-r--r-- 1 root root    0 11月  8 17:54 when.txt

如上一般用在的场合是:目录和文件是否在某台机器上面。

2.4 playbook中的handlers

执行task之后,服务器发生变化之后要执行的一些操作,比如我们修改了配置文件后,需要重启一下服务:

代码语言:javascript
复制
vi /etc/ansible/handlers.yml//加入如下内容
---
- name: handlers test
  hosts: testhost
  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
      
说明,只有copy模块真正执行后,才会去调用下面的handlers相关的操作。
也就是说如果说假如/etc/passwd这个文件不存在 copy执行后是错误的,然后并不会去执行handlers里面的shell相关命令。

这种比较适合配置文件发生更改后(如果配置成功),就会启动重启服务的操作。

下一篇
举报
领券