Ansible配置与测试 -------2,ansible-------2


3.10.1 实训项目

项目一、Ansible配置与测试

1、搭建Ansible环境

一台控制主机:

10.15.15.22

两台node主机:

192.168.97.195

192.168.97.157

2、认识ansible的目录

安装目录

配置文件目录:/etc/ansible/

执行文件目录:/usr/bin/

Lib库依赖目录:/usr/lib/python2.7/site-packages/ansible

Help文档目录:/usr/share/doc/ansible-2.4.2.0/

Man文档目录:/usr/share/man/man1/

3、认识配置文件ansible.cfg,hosts

1)inventory

该参数表示资源清单inventory文件的位置,资源清单就是一些Ansible需要连接管理的主机列表

inventory =/root/ansible/hosts

2)library

Ansible的操作动作,无论是本地或远程,都使用一小段代码来执行,这小段代码称为模块,这个library参数就是指向存放Ansible模块的目录

library =/usr/share/ansible

3)forks

设置默认情况下Ansible最多能有多少个进程同时工作,默认设置最多5个进程并行处理。具体需要设置多少个,可以根据控制主机的性能和被管理节点的数量来确定。

forks = 5

4)sudo_user

这是设置默认执行命令的用户,也可以在playbook中重新设置这个参数

sudo_user =root

5)remote_port

这是指定连接被关节点的管理端口,默认是22,除非设置了特殊的SSH端口,不然这个参数一般是不需要修改的

remote_port =22

6)host_key_checking

这是设置是否检查SSH主机的密钥。可以设置为True或False

host_key_checking= False

7)timeout

这是设置SSH连接的超时间隔,单位是秒。

timeout = 20

8)log_path

Ansible系统默认是不记录日志的,如果想把Ansible系统的输出记录到人i治稳健中,需要设置log_path来指定一个存储Ansible日志的文件

log_path =/var/log/ansible.log

另外需要注意,执行Ansible的用户需要有写入日志的权限,模块将会调用被管节点的syslog来记录,口令是不会出现的日志中的

9)host_key_checking

如果有台被管节点重新安装系统并在known_hosts中有了与之前不同的密钥信息,就会提示一个密钥不匹配的错误信息,直到被纠正为止,在使用Ansible时,如果有台被管理节点没有在known_hosts中被初始化,将会在使用Ansible或定时执行Ansible时提示对key信息的确认。

如果你不想出现这种情况,并且你明白禁用此项行为的含义,只要修改该参数为False即可

host_key_checking= False

4、配置ansible.cfg,demo案例

# egrep -v"(^$|^#)" /etc/ansible/ansible.cfg

[defaults]

inventory      = /etc/ansible/hosts

forks          = 5

poll_interval  = 15

sudo_user      = root

remote_port    = 22

roles_path    =/etc/ansible/roles:/usr/share/ansible/roles

[inventory]

[privilege_escalation]

[paramiko_connection]

[ssh_connection]

[persistent_connection]

[accelerate]

[selinux]

[colors]

[diff]

------------------------------------------------------------------------------------------------

# egrep -v"(^$|^#)" /etc/ansible/hosts

[test_group1]

192.168.97.195ansible_ssh_pass='xxx'

192.168.97.157ansible_ssh_pass=‘xxx’

练习场景一:密码方式登陆,使用ansible查看/tmp下的内容

1、安装ssh_pass包

#yuminstall sshpass

2、配置ansible文件

#egrep -v "(^$|^#)" /etc/ansible/ansible.cfg

[defaults]

inventory= /etc/ansible/hosts

forks= 5

poll_interval= 15

sudo_user= root

remote_port= 22

roles_path= /etc/ansible/roles:/usr/share/ansible/roles

host_key_checking= False

timeout= 10

log_path= /var/log/ansible.log

3、配置hosts文件

#egrep -v "(^$|^#)" /etc/ansible/hosts

[test_group1]

192.168.97.157ansible_ssh_pass='xxx'

192.168.97.195ansible_ssh_pass='xxx'

4、执行ansible命令

#ansibletest_group1 -a 'ls /tmp'

显示success证明成功

 

练习场景二:秘钥登陆

1、生成并配置ssh公钥、私钥,实现免秘钥登陆

#ssh-keygen

如下图所示

ssh-copy-idip

#ssh-copy-id192.168.97.195

#ssh-copy-id192.168.97.197

2、ansible配置文件

#vi/etc/ansible/ansible.cfg

private_key_file= /root/.ssh/id_rsa //指定秘钥存放路径

3、执行测试

#ansibleall -a 'ls /tmp '

若显示success证明成功

项目二、Ansible的ad-hoc模式

1、模块的使用

ansible 默认提供了很多模块来供我们使用。在 Linux 中,我们可以通过 ansible-doc -l 命令查看到当前 ansible 都支持哪些模块,通过 ansible-doc -s 模块名又可以查看该模块有哪些参数可以使用。

2、Ansible常用模块

l  ping模块

l  raw模块

l  yum模块

l  apt模块

l  pip模块

l  synchronize模块

l  template模块

l  copy模块

l  user 模块与group模块

l  service 模块

l  get_url 模块

l  fetch模块

l  file模块

l  unarchive模块

l  command 模块和shell

接下来的命令中,test_group1 是一个组,这个组里面有很多服务器,引号里面””命令会在test_group1组下 的所有机器上执行.

练习场景一:模块ping

检查指定节点机器是否还能连通,用法很简单,不涉及参数,主机如果在线,则回复pong

  #ansible test_group1 -m ping

练习场景二:拷贝模块

使用拷贝模块,将本机/etc/hosts拷贝到其它或指定机器上

#ansible test_group1 -mcopy -a "src=/etc/hosts dest=/opt" -f 5

练习场景三:获取test_group1组里所有主机的系统信息

#ansible test_group1 -msetup

练习场景四:使用yum安装方式安装vsftpd软件,并启动服务

#ansible 192.168.97.195 -myum -a "name=vsftpd state=present" -f 5

#ansible 192.168.97.195 -mservice -a "name=vsftpd state=started" -f 5

 

项目三、Ansible playbook模式变量的定义

Playbooks 与 adhoc 相比,是一种完全不同的运用 ansible 的方式,是非常之强大的.简单来说,playbooks 是一种简单的配置管理系统与多机器部署系统的基础.与现有的其他系统有不同之处,且非常适合于复杂应用的部署.Playbooks 的格式是YAML

这里有一个 playbook,其中仅包含一个 play:

---

- hosts: webservers

  remote_user: root

  vars:

    http_port: 80

    max_clients: 200

  remote_user: root

  tasks:

  - name: ensure apache is at the latestversion

    yum: pkg=httpd state=latest

  - name: write the apache config file

    template: src=/srv/httpd.j2dest=/etc/httpd.conf

    notify:

    - restart apache

  - name: ensure apache is running

    service: name=httpd state=started

 

在下面,我们将分别讲解 playbook 语言的多个特性.

a、hosts 行的内容是一个或多个组或主机的 patterns,remote_user 就是账户名

b、在每一个 task 中,可以定义自己的远程用户

c、vars定义的是变量

1、playbook模式的基本使用

ansible-playbookplaybook.yml [option]

Options:

  -u REMOTE_USER, --user=REMOTE_USER          ssh连接的用户名,ansible.cfg中可以配置

  -k, --ask-pass                                  提示输入ssh登录密码,当使用密码验证登录的时候用

  -s, --sudo                                          sudo运行

  -U SUDO_USER, --sudo-user=SUDO_USER          sudo到哪个用户,默认为root

  -K, --ask-sudo-pass                             提示输入sudo密码,当不是NOPASSWD模式时使用

  -T TIMEOUT, --timeout=TIMEOUT                           ssh连接超时时间,默认10秒

  -C, --check                                                                指定该参数后,执行playbook文件不会真正去执行,而是模拟执行一遍,然后输出本次执行会对远程主机造成的修改

  -c CONNECTION                                    连接类型(default=smart)

  -D, --diff                                           如果file和template模块改变,会显示改变的内容,应该和--check一起

  -e EXTRA_VARS, --extra-vars=EXTRA_VARS         设置额外的变量如:key=value or YAML/JSON,以空格分隔变量,或用多个-e

  -f FORKS, --forks=FORKS                                         fork多少个进程并发处理,默认5

  -i INVENTORY, --inventory-file=INVENTORY             指定hosts文件路径,默认default=/etc/ansible/hosts

  -l SUBSET, --limit=SUBSET                                        指定一个pattern,对-hosts:匹配到的主机再过滤一次

  --list-hosts                                          只打印有哪些主机会执行这个playbook文件,不是实际执行该playbook

  --list-tasks                                          列出该playbook中会被执行的task

  -M MODULE_PATH,--module-path=MODULE_PATH   模块所在路径,默认/usr/share/ansible/

  --private-key=PRIVATE_KEY_FILE                  私钥路径

  --start-at-task=START_AT                            startthe playbook at the task matching this name

  --step                                              同一时间只执行一个task,每个task执行前都会提示确认一遍

  --syntax-check                                  只检测playbook文件语法是否有问题,不会执行该playbook

  -t TAGS, --tags=TAGS                        当play和task的tag为该参数指定的值时才执行,多个tag以逗号分隔

  --skip-tags=SKIP_TAGS                       当play和task的tag不匹配该参数指定的值时,才执行

  -v, --verbose                                   verbosemode (-vvv for more, -vvvv to enable connection debugging)

练习场景一:创建文件,在文件中定义变量

1、playbook的配置,创建文件名称为touch.yml

---

- hosts : 192.168.97.195

remote_user : root

vars :

touch_file : cetc_file

tasks :

- name : touch file

shell : "touch/tmp/{{touch_file}}"

2、执行

ansible-playbook./touch.yml

3、执行结果返回

红色:表示有task执行失败

黄色:表示执行了且改变了远程主机状态

绿色:表示执行成功

 

练习场景二:外部变量定义方式,--extra-vars "touch_file=cetc-file1"

1、playbook的配置,文件名称为touch.yml

---

- hosts : 192.168.97.195

remote_user : root

#vars :

#touch_file : cetc_file

tasks :

- name : touch file

shell : "touch/tmp/{{touch_file}}"

2、执行

ansible-playbook touch.yml--extra-vars "touch_file=cetc-file1"

3、验证目标主机是否创建成功

 

练习场景三:文件中定义变量

1、进入ansible的hosts文件清单中,添加下列内容,文件名称为touch.yml

[test_group1:vars]

touch_file=cetc-file2     

2、执行

ansible-playbook./touch.yml

3、验证目标主机是否创建成功

练习场景四:注册定义变量方式

1、playbook文件配置

---

- hosts : 192.168.97.195

remote_user : root

vars:

touch_file: file5

tasks :

- name : get date

command : date

register : date_output

-name : echo date_output                                          

shell: "echo{{date_output.stdout}} >/tmp/{{touch_file}}"

2、执行

ansible-playbook touch.yml

3、查看目标主机文件是否创建成功,文件内容是否是日期

 

项目四、Ansible playbook模式的基本语句

 

基本语句:

1、条件语句    when

2、循环语句

循环类型        关键字

标准循环        with_items

嵌套循环        with_nested

遍历字典         with_dict

并行遍历列表     with_together

遍历列表和索引   with_indexed_items

遍历文件列表的内容 with_file

遍历目录文件     with_filelob

重试循环          until

查找第一个匹配文件  with_first_found

随机选择          with_random_choice

在序列中循环      with_sequence

3、条件循环语句复用

种类一、标准循环

种类二、遍历字典

种类三、遍历目录中的内容

练习场景一:条件语句when

1、条件语句,当系统为RedHat时,重启系统

when语句

---

- hosts :192.168.97.195,192.168.97.157

remote_user : root

tasks:

- name: "rebootCentOS 6 and 7 systems"

command: reboot

when: ansible_os_family =="RedHat"

2、执行

ansible-playbooksystem.yml

3、查看目标主机是否重启

 

练习场景二:循环语句with_items

1、循环语句with_items,创建item.yml文件,使用name和group模块进行添加用户,并将用户添加到指定组中

---

- hosts :192.168.97.195,192.168.97.157

remote_user : root

tasks:

- name: add several users

user: name={{ item.name }}state=present groups={{ item.groups }}

with_items:

- { name: 'test1', groups:'wheel' }

- { name: 'test2', groups:'root' }

2、执行

ansible-playbook item.yml

3、验证目标主机添加用户相关操作是否成功

练习场景三:循环语句with_dict

1、循环语句with_dict,创建dict.yml文件,进行添加用户相关操作

---

- hosts :192.168.97.195,192.168.97.157

remote_user : root

tasks:

- name: add several user

user: name={{ item.key }}state=present groups={{ item.value }}

with_dict:

{ 'test3' : 'wheel','test4' :'root' }

2、执行

ansible-playbook dict.yml

3、验证目标主机添加用户相关操作是否成功

 

练习场景四:遍历目录中文件

1、遍历目录中的文件

--- 

- hosts: all

tasks: 

- file: dest=/etc/fooappstate=directory 

- copy: src={{ item }} dest=/etc/fooapp/ owner=rootmode=600

 with_fileglob:

 -/playbooks/files/fooapp/*

2、运行

3、验证操作是否成功

 

相关内容

    暂无相关文章