自动化运维--ansible剧本操作

news/2024/8/30 20:42:33 标签: ansible

目录

  • 一、简介
  • 二、什么是YAML
  • 三、ansible-playbook介绍
  • 四、ansible-playbook使用教程
      • 4.1、给node1标签下的主机部署httpd服务
      • 4.2、ignore_errors忽略错误
      • 4.3、yaml文件执行报错不在继续执行
      • 4.4、多hosts操作
      • 4.2、yaml文件内定义变量
      • 4.6、命令行输入变量
      • 4.7、hosts文件定义变量
      • 4.8、单条件判断
      • 4.9、多条件判断
      • 4.10、条件组
      • 4.11、迭代

一、简介

Playbooks(剧本)是Ansible的配置、部署语言,由它对描述你想要远程机器执行的策略或步骤,使用YAML编写。playbooks是由一个或多个“play”(task)组成的列表。从根本上讲task就是调用ansible的一个模块(module)。将多个play组织在一个playbook中,运行时就会根据自上而下的顺序依次执行。

二、什么是YAML

  1. YAML:是一种非标记语言。是用来写配置文件的语言,非常简洁和强大。
  2. YAML语法和其他语言类似,也可以表达散列表、标量等数据结构。
  3. 结构通过空格来展示;序列里配置项通过-来代表;Map里键值用:来分隔;YAML的扩展名为yaml

语法规则

  1. 对大小写敏感
  2. 使用缩进表示层级关系
  3. 缩进时不允许使用tab键,只允许使用空格
  4. 缩进的空格数目不重要,只要相同层级的元素左侧对其即可

YAML支持的数据结构

  1. 键值对
  2. 数组
  3. 纯量

ansibleplaybook_20">三、ansible-playbook介绍

通过task调用ansible的模板将多个play组织在一个playbook中运行。playbooks本身由以下各部分组成:

  1. Tasks:任务,即调用模块完成的某操作;
  2. Variables:变量
  3. Templates:模板
  4. Handlers:处理器,当某条件满足时,触发执行的操作;
  5. Roles:角色。

ansibleplaybook_28">四、ansible-playbook使用教程

ansible安装教程:https://blog.csdn.net/weixin_49226813/article/details/112466490

基本环境

名称主机名P地址
ansible服务器ansible20.0.0.10
节点1server120.0.0.20
节点2server220.0.0.30
# 在之前的部署基础上只修改了hosts文件
[root@ansible ~]# vim /etc/ansible/hosts
[node1]
20.0.0.20
[node2]
20.0.0.30
创建一个存放yaml文件存放的目录
[root@ansible ~]# mkdir ansible

4.1、给node1标签下的主机部署httpd服务

1、创建http.yaml文件

[root@ansible ansible]# vim http.yaml
- hosts: node1					#node1主机标签
  remote_user: root				#root执行用户
  tasks:								
   - name: yum httpd
     yum: name=httpd			#安装httpd服务
   - name: start httpd
     service: name=httpd state=started		#开启httpd服务器
   - name: stop firewalld
     service: name=firewalld state=stopped		#关闭firewalld防火墙
   - name: index
     copy: content=<h1>abc123</h1> dest=/var/www/html/index.html	#设置一个主页

2、检查语法

ansible-playbook 文件名称 --syntax-check

[root@ansible ansible]# ansible-playbook http.yaml --syntax-check

playbook: http.yaml

3、执行http.yaml 文件

[root@ansible ansible]# ansible-playbook http.yaml

PLAY [node1] ******************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [20.0.0.20]

TASK [yum httpd] **************************************************************************************************
changed: [20.0.0.20]

TASK [start httpd] ************************************************************************************************
changed: [20.0.0.20]

TASK [stop firewalld] *********************************************************************************************
ok: [20.0.0.20]

TASK [index] ******************************************************************************************************
changed: [20.0.0.20]

PLAY RECAP ********************************************************************************************************
20.0.0.20                  : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

4、验证
4.1、去节点1上面验证

[root@server1 ~]# rpm -q httpd
httpd-2.4.6-67.el7.centos.x86_64

4.2、使用主机访问
在这里插入图片描述

4.2、ignore_errors忽略错误

1、创建http01.yaml文件

[root@ansible ansible]# vim http01.yaml
- hosts: node2
  remote_user: root
  tasks:
   - name: yum httpd
     yum: name=httpd
   - name: start httpd
     service: name=httpd state=started
   - name: stop firewalld
     service: name=firewalld state=stopped
   - name: setenforce
     command: '/sbin/setenforce 0'
     ignore_errors: True			#忽略错误(就是忽略setenforce 0执行报错)
   - name: index
     copy: content=<h1>abc123</h1> dest=/var/www/html/index.html

2、检查语法

[root@ansible ansible]# ansible-playbook http01.yaml --syntax-check

playbook: http01.yaml

3、执行http.yaml 文件

[root@ansible ansible]# ansible-playbook http01.yaml 

PLAY [node2] ******************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [20.0.0.30]

TASK [yum httpd] **************************************************************************************************
changed: [20.0.0.30]

TASK [start httpd] ************************************************************************************************
changed: [20.0.0.30]

TASK [stop firewalld] *********************************************************************************************
ok: [20.0.0.30]

TASK [setenforce] *************************************************************************************************
fatal: [20.0.0.30]: FAILED! => {"changed": true, "cmd": ["/sbin/setenforce", "0"], "delta": "0:00:00.002361", "end": "2021-01-13 14:44:51.071972", "msg": "non-zero return code", "rc": 1, "start": "2021-01-13 14:44:51.069611", "stderr": "/sbin/setenforce: SELinux is disabled", "stderr_lines": ["/sbin/setenforce: SELinux is disabled"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [index] ******************************************************************************************************
changed: [20.0.0.30]

PLAY RECAP ********************************************************************************************************
20.0.0.30                  : ok=6    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1   
出现了报错,是否忽略保持去验证

4、验证
去节点2上面验证

[root@server2 ~]# rpm -q httpd
httpd-2.4.6-67.el7.centos.x86_64

访问
在这里插入图片描述
表明执行 /sbin/setenforce 0报错自动忽略了

4.3、yaml文件执行报错不在继续执行

当执行yaml文件有一步错误后错误下的命令就不在执行
1、创建http02.yaml文件

- hosts: node1
  remote_user: root
  tasks:
   - name: yum httpd
     yum: name=httpd
   - name: start httpd
     service: name=httpd state=started
   - name: stop firewalld
     service: name=firewalld state=stopped
   - name: setenforce
     command: '/sbin/setenforce 0'
   - name: index
     copy: content=<h1>abc123</h1> dest=/var/www/html/index.html

2、检查语法

[root@ansible ansible]# ansible-playbook http02.yaml --syntax-check

playbook: http02.yaml

3、清除httpd

[root@server1 ~]# systemctl stop httpd
[root@server1 ~]# yum remove httpd -y
[root@server1 ~]# rm -fr /var/www/html/*

4、执行http02.yaml 文件

[root@ansible ansible]# ansible-playbook http02.yaml 

PLAY [node1] ******************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [20.0.0.20]

TASK [yum httpd] **************************************************************************************************
changed: [20.0.0.20]

TASK [start httpd] ************************************************************************************************
changed: [20.0.0.20]

TASK [stop firewalld] *********************************************************************************************
ok: [20.0.0.20]

TASK [setenforce] *************************************************************************************************
fatal: [20.0.0.20]: FAILED! => {"changed": true, "cmd": ["/sbin/setenforce", "0"], "delta": "0:00:00.002681", "end": "2021-01-13 14:59:54.032128", "msg": "non-zero return code", "rc": 1, "start": "2021-01-13 14:59:54.029447", "stderr": "/sbin/setenforce: SELinux is disabled", "stderr_lines": ["/sbin/setenforce: SELinux is disabled"], "stdout": "", "stdout_lines": []}

PLAY RECAP ********************************************************************************************************
20.0.0.20                  : ok=4    changed=2    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

5、测试
在执行/sbin/setenforce 0时出现错误,在此错误下面的的创建主页命令是否执行

节点1验证

[root@server1 ~]# ls /var/www/html/
#发现目录为空没有主页

访问
在这里插入图片描述

4.4、多hosts操作

1、创建name.yaml文件

[root@ansible ansible]# vim name.yaml
- hosts: node1
  remote_user: root
  tasks:
   - name: nginx group
     group: name=nginx system=yes gid=1033
   - name: nginx user
     user: name=nginx uid=1033 group=nginx system=yes
- hosts: node2
  remote_user: root
  tasks:
    - name: copy file
      copy: src=/etc/inittab dest=/opt/inittab

2、语法检查

[root@ansible ansible]# ansible-playbook name.yaml --syntax-check

playbook: name.yaml

3、执行yaml文件

[root@ansible ansible]# ansible-playbook name.yaml 

PLAY [node1] ******************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [20.0.0.20]

TASK [nginx group] ************************************************************************************************
changed: [20.0.0.20]

TASK [nginx user] *************************************************************************************************
changed: [20.0.0.20]

PLAY [node2] ******************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [20.0.0.30]

TASK [copy file] **************************************************************************************************
changed: [20.0.0.30]

PLAY RECAP ********************************************************************************************************
20.0.0.20                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
20.0.0.30                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@ansible ansible]# 

4、测试
节点1查看是否有nginx用户

[root@server1 ~]# id nginx
uid=1033(nginx) gid=1033(nginx) 组=1033(nginx)

节点2测试是否有

[root@server2 opt]# ll
总用量 4
-rw-r--r--  1 root root 511 1月  13 15:36 inittab
drwxr-xr-x. 2 root root   6 3月  26 2015 rh

4.2、yaml文件内定义变量

1、清除node1httpd环境

[root@server1 ~]# rpm -q httpd
httpd-2.4.6-67.el7.centos.x86_64
[root@server1 ~]# yum remove httpd -y

2、创建一个测试文件

[root@ansible ansible]# vim cese.yaml
- hosts: node1
  remote_user: root
  vars:
  - a1: httpd
  tasks:
   - name: yum httpd
     yum: name={{a1}}
     notify:
      - abc
   - name: start httpd
     service: enabled=true name={{a1}} state=started
  handlers:
   - name: abc
     service: name={{a1}} state=restarted

3、检查语法

[root@ansible ansible]# ansible-playbook cese.yaml --syntax-check

playbook: cese.yaml

4、执行yaml文件

[root@ansible ansible]# ansible-playbook cese.yaml 

PLAY [node1] ******************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [20.0.0.20]

TASK [yum httpd] **************************************************************************************************
changed: [20.0.0.20]

TASK [start httpd] ************************************************************************************************
changed: [20.0.0.20]

RUNNING HANDLER [abc] *********************************************************************************************
changed: [20.0.0.20]

PLAY RECAP ********************************************************************************************************
20.0.0.20                  : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@ansible ansible]#

5、验证
节点1验证

[root@server1 ~]# rpm -q httpd
httpd-2.4.6-67.el7.centos.x86_64

访问
在这里插入图片描述

4.6、命令行输入变量

1、创建一个测试文件

[root@ansible ansible]# vim abc.yaml
- hosts: node2
  remote_user: root
  vars:
   - useradd:
  tasks:
  - name: create user
    user: name={{useradd}}

2、检查语法

[root@ansible ansible]# ansible-playbook abc.yaml --syntax-check

playbook: abc.yaml

3、执行yaml文件

[root@ansible ansible]# ansible-playbook abc.yaml -e "useradd=lisi"

PLAY [node2] ******************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [20.0.0.30]

TASK [create user] ************************************************************************************************
changed: [20.0.0.30]

PLAY RECAP ********************************************************************************************************
20.0.0.30                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

4、节点2验证

[root@server2 ~]# id lisi
uid=1001(lisi) gid=1001(lisi) 组=1001(lisi)

4.7、hosts文件定义变量

1、修改hosts文件

[root@ansible ansible]# vim /etc/ansible/hosts
[node1]
20.0.0.20 add="123456"		#增加一个add="123456"

2、创建测试文件

[root@ansible ansible]# vim hosts.yaml
- hosts: node1
  remote_user: root
  tasks:
   - name: copy file
     copy: content="{{add}}" dest=/opt/add.txt

3、检查语法

[root@ansible ansible]# ansible-playbook hosts.yaml --syntax-check

playbook: hosts.yaml

4、测试
去节点1测试

[root@server1 opt]# ll
总用量 4
-rw-r--r--  1 root root 6 1月  13 16:36 add.txt
drwxr-xr-x. 2 root root 6 3月  26 2015 rh
[root@server1 opt]# cat add.txt 
123456

4.8、单条件判断

1、创建测试文件

[root@ansible ansible]# vim centos.yaml
- hosts: node1
  remote_user: root
  tasks:
  - name: centos 7
    command: /sbin/shutdown -r now		#重启-t是关机
    when:
     - ansible_distribution == "CentOS"

2、检查语法

[root@ansible ansible]# ansible-playbook centos.yaml --syntax-check

playbook: centos.yaml

3、执行yaml文件

 [root@ansible ansible]# ansible-playbook centos.yaml 

PLAY [node1] ************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************
ok: [20.0.0.20]

TASK [centos 7] *********************************************************************************************************
fatal: [20.0.0.20]: FAILED! => {"msg": "Failed to connect to the host via ssh: ssh_exchange_identification: read: Connection reset by peer"}

PLAY RECAP **************************************************************************************************************
20.0.0.20                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

有一个报错说ssh断开,因为服务器重启了

4.9、多条件判断

1、创建测试文件

[root@ansible ansible]# vim centos01.yaml

2、检查语法

[root@ansible ansible]# ansible-playbook centos01.yaml --syntax-check

playbook: centos01.yaml

3、执行yaml文件

[root@ansible ansible]# ansible-playbook centos01.yaml
[root@ansible ansible]# ansible-playbook centos01.yaml 

PLAY [node1] ************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************
ok: [20.0.0.20]

TASK [centos 7] *********************************************************************************************************
fatal: [20.0.0.20]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Shared connection to 20.0.0.20 closed.", "unreachable": true}

PLAY RECAP **************************************************************************************************************
20.0.0.20                  : ok=1    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0   

依然报错说ssh断开,因为服务器重启了

4.10、条件组

1、创建测试文件

[root@ansible ansible]# vim duoif.yaml
- hosts: node1
  remote_user: root
  tasks:
   - name: copy
     copy: content="abc123" dest=/opt/af.txt
     when: (1<2) or (2<1)

2、检查语法

[root@ansible ansible]# ansible-playbook duoif.yaml --syntax-check

playbook: duoif.yaml

3、执行yaml文件

[root@ansible ansible]# ansible-playbook duoif.yaml 

PLAY [node1] ************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************
ok: [20.0.0.20]

TASK [copy] *************************************************************************************************************
changed: [20.0.0.20]

PLAY RECAP **************************************************************************************************************
20.0.0.20                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

4、验证
节点1验证

[root@server1 opt]# ll
总用量 8
-rw-r--r--  1 root root 6 1月  13 16:36 add.txt
-rw-r--r--  1 root root 6 1月  13 18:11 af.txt
drwxr-xr-x. 2 root root 6 3月  26 2015 rh
[root@server1 opt]# cat af.txt 
abc123

4.11、迭代

1、创建测试文件

[root@ansible ansible]# vim dai.yaml
- hosts: node1
  remote_user: root
  tasks:
   - name: yum httpd tomcat
     yum: name={{item}} state=latest
     with_items:
      - httpd
      - tomcat

2、检查语法

[root@ansible ansible]# ansible-playbook dai.yaml --syntax-check

playbook: dai.yaml

3、执行yaml文件

[root@ansible ansible]# ansible-playbook dai.yaml 

PLAY [node1] ************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************
ok: [20.0.0.20]

TASK [yum httpd tomcat] *************************************************************************************************
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a 
loop to supply multiple items and specifying `name: "{{item}}"`, please use `name: ['httpd', 'tomcat']` and remove the 
loop. This feature will be removed in version 2.11. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.
changed: [20.0.0.20] => (item=[u'httpd', u'tomcat'])

PLAY RECAP **************************************************************************************************************
20.0.0.20                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

4、验证
节点1验证

[root@server1 opt]# rpm -q httpd
httpd-2.4.6-67.el7.centos.x86_64
[root@server1 opt]# rpm -q tomcat
tomcat-7.0.76-2.el7.noarch

http://www.niftyadmin.cn/n/569575.html

相关文章

ansible自动化运维两个重要模块

目录template模块安装配置httpd服务器tag标签模块1、执行指定标签对应的命令2、特殊标签template模块安装配置httpd服务器 基本环境 名称主机名P地址ansible服务器ansible20.0.0.10节点1server120.0.0.20节点2server220.0.0.30 # 在之前的部署基础上只修改了hosts文件 [roota…

k8s入门篇

目录简介ks8的主要功能k8s架构图及组件k8s核心概念PodControllersService简介 Kubernetes是Google在2014年开源的一个容器集群管理系统&#xff0c;Kubernetes简称K8S。K8S用于容器化应用程序的部署&#xff0c;扩展和管理。K8S提供了容器编排&#xff0c;资源调度&#xff0c…

k8s二进制单节点部署

目录部署规划一、etcd数据库部署master节点部署node节点部署node节点加入群集二、docker容器引擎部署三、flannel网络组件部署master分配子网写入etcdnode节点安装flannel四、master节点部署五、启动scheduler六、启动controller-manager七、kubelet部署八、kube-proxy部署部署…

k8s多节点部署

目录前提master02部署前端负载nginx搭建测试node IP指向VIP地址测试方法负载前提 多节点在单节点的基础上进行 资源规划 名称主机名IP地址ks8-master01master20.0.0.10/24ks8-master02master0220.0.0.40/24node01node0120.0.0.20/24node02node0220.0.0.30/24nginx-masterngin…

Java运行初始配置

1.下载jdk并安装 2.配置环境变量&#xff08;通常为一系列键值对&#xff09;&#xff1a; path&#xff1a;X:\Program Files\Java\jdk1.8.0_05\bin&#xff08;操作系统外部命令搜索路径&#xff0c;执行命令时进入path下所设置目录进行检索&#xff09; classpath&#xff…

Java变量相关

基本数据类型&#xff1a; 数值型&#xff1a;整数类型&#xff08;byte&#xff0c;short&#xff0c;int&#xff0c;long&#xff09;&#xff1b;浮点类型&#xff08;float&#xff0c;double&#xff09; 字符型&#xff1a;char 布尔型&#xff1a;boolean 驼峰命名法…

“编码GBK的不可映射字符”处理方法

有时候cmd运行java时会出现错误提示&#xff1a;“编码GBK的不可映射字符” 原因&#xff1a; 代码中含有中文字符&#xff0c;系统默认的编码格式是GBK&#xff0c;而包含中文字符的代码一般是Unicode格式&#xff0c;所以直接运行含有中文字符的代码就会出现编码错误 解决办…

算数运算符小记

1. 一系列运算的最后结果的数据类型&#xff0c;取决于运算中范围最大的数据类型。 例如 int i 3 / 2&#xff1b; 此时i的输出类型将为int型&#xff0c;输出结果为“1”。 而如果 int i 3 / 2.0&#xff1b; 这时候i的输出类型将为double型&#xff0c;输出结果为“1…