Ansible运维自动化3

一.循环

1.简单循环
loop: #赋值列表

  • value1
  • value2

  • {{item}} #迭代变量名称
    实例
[jia@ansible ansible]$ cat test.yaml 
---
- name: create user
  vars_files: ./user.yaml
  hosts: westos
  tasks:
    - name: user westos1
      user:
        name: "{{item}}"
        state: present
      loop:
        - westos1
        - westos2
        - westos3


[jia@ansible ansible]$ cat user.yaml 
---
USERS:
  - westos1
  - westos2
  - westos3
[jia@ansible ansible]$ cat test.yaml 
---
- name: create user
  vars_files: ./user.yaml
  hosts: westos
  tasks:
    - name: user westos1
      user:
        name: "{{item}}"
        state: absent
      loop:
        "{{USERS}}"

2.循环散列或字典列表

---
- name: create file
  hosts: 172.25.0.254
  tasks:
    - name: file module
      service:
        name: "{{ item.name}}"
        state: "{{ item.state }}"
      loop:
        - name: httpd
          state: started
        - name: vsftpd
          state: stopped

二.条件

when:
  - 条件1
  - 条件2

条件判断
在这里插入图片描述
多条条件组合

when:
  条件1 and 条件2
  - 条件1
  - 条件2
when:
  条件1 or 条件2
when: >
  条件1
  or
  条件2
[jia@ansible ansible]$ cat tiaojian.yaml 
---
- name: test
  hosts: all
  tasks:
    - name: test
      shell: test -e /mnt/file
      ignore_errors: yes
      register: OUTPUT

    - name: show messages
      debug:
        msg: /mnt/file is not find!
      when: OUTPUT.rc !=0

    - name: show messages
      debug:
        msg: /mnt/file is exit!
      when: OUTPUT.rc ==0

在这里插入图片描述

[jia@ansible ansible]$ cat tiaojian.yaml 
---
- name: test
  hosts: all
  tasks:
    - name: show messages
      debug:
        msg: vdb not find
      when: ansible_facts['devices']['vdb'] is not defined

    - name: show messages
      debug:
        msg: vdb is exit
      when: ansible_facts['devices']['vdb'] is defined
[jia@ansible ansible]$ cat tiaojian.yaml 
---
- name: test
  hosts: all
  tasks:
    - name: show messages
      debug:
        msg: vdb not find
      when:
      - ansible_facts['devices']['vdb'] is not defined
      - inventory_hostname is in "172.25.51.2"

    - name: show messages
      debug:
        msg: vdb is exit
      when:
      - ansible_facts['devices']['vdb'] is defined
      - inventory_hostname is in "172.25.51.2"

在这里插入图片描述
在这里插入图片描述

三.触发器

notify: 触发器当遇到更改是触发handlers
handlers:触发器触发后执行的动作

---
- name: create virtualhost for web server
hosts: 172.25.0.254
vars_files:
./vhost_list.yml
tasks:
- name: create document
file:
path: "{{web2.document}}"
state: directory
- name: create vhost.conf
copy:
dest: /etc/httpd/conf.d/vhost.conf
content:
"<VirtualHost *:{{web1.port}}>\n\tServerName {{web1.name}}\n\tDocumentRoot
{{web1.document}}\n\tCustomLog logs/{{web1.name}}.log combined\n</
VirtualHost>\n\n<VirtualHost *:{{web2.port}}>\n\tServerName {{web2.name}}
\n\tDocumentRoot {{web2.document}}\n\tCustomLog logs/{{web2.name}}.log combined\n</
VirtualHost>"
notify:
restart apache
handlers:
- name: restart apache
service:
name: httpd
state: restarted

四.处理失败任务

1.ignore_errors
作用:
当play遇到任务失败是会终止
ignore_errors: yes 将会忽略任务失败使下面的任务继续运行

2.force_handlers
作用:
当任务失败后play被终止也会调用触发器进程

3.changed_when
作用:
控制任务在何时报告它已进行更改

4.failed_when
作用:
当符合条件时强制任务失败

5.block
block: 定义要运行的任务
rescue:定义当block句子中出现失败任务后运行的任务
always:定义最终独立运行的任务

五.ansible roles

1、ansible 角色简介

  • Ansible roles 是为了层次化,结构化的组织Playbook
  • roles就是通过分别将变量、文件、任务、模块及处理器放置于单独的目录中,并可以便捷地include它们
  • roles一般用于基于主机构建服务的场景中,在企业复杂业务场景中应用的频率很高
  • 以特定的层级目录结构进行组织的tasks、variables、handlers、templates、files等;相当于函数的调用把
    各个功能切割成片段来执行。

2、roles目录结构
files ##存放copy或script等模块调用的函数
tasks ##定义各种task,要有main.yml,其他文件include包含调用
handlers ##定义各种handlers,要有main.yml,其他文件include包含调用
vars ##定义variables,要有main.yml,其他文件include包含调用
templates ##存储由template模块调用的模板文本
meta ##定义当前角色的特殊设定及其依赖关系,要有main.yml的文件
defaults ##要有main.yml的文件,用于设定默认变量
tests ##用于测试角色

3、role存放的路径在配置文件ansible.cfg中定义
roles_path = path/roles (默认目录:/etc/ansible/roles)

4、创建目录结构

[jia@ansible ansible]$ ansible-galaxy init vsftpd
- Role vsftpd was created successfully
[jia@ansible ansible]$ cd vsftpd/
[jia@ansible vsftpd]$ ls
defaults  files  handlers  meta  README.md  tasks  templates  tests  vars

5、playbook中使用roles

[jia@ansible vsftpd]$ cd  tasks/
[jia@ansible tasks]$ ls
main.yml
[jia@ansible tasks]$ vim main.yml 
[jia@ansible tasks]$ cat main.yml 
---
# tasks file for vsftpd
- name: install vsftpd
  dnf:
    name: vsftpd
    state: latest
  notify:
    - restart vsftpd
    - firewalld set

- name: set vsftpd
  lineinfile:
    path: /etc/vsftpd/vsftpd.conf
    regexp: "anonymous_enable"
    line: "anonymous_enable={{ STATE }}"
  notify:
    - restart vsftpd
[jia@ansible vsftpd]$ cd handlers/
[jia@ansible handlers]$ ls
main.yml
[jia@ansible handlers]$ vim main.yml 
[jia@ansible handlers]$ cat main.yml 
---
# handlers file for vsftpd
- name: restart vsftpd
  service:
    name: vsftpd
    state: restarted
    enabled: yes

- name: firewalld set
  firewalld:
    service: ftp
    state: enabled
    permanent: yes
    immediate: yes
[jia@ansible vsftpd]$ cd vars/
[jia@ansible vars]$ ls
main.yml
[jia@ansible vars]$ vim main.yml 
[jia@ansible vars]$ cat main.yml 
---
# vars file for vsftpd
STATE: YES
[jia@ansible ansible]$ vim vsftpd.yaml
[jia@ansible ansible]$ cat vsftpd.yaml 
---
- name: install vsftpd server
  hosts: all
  roles:
    - role: vsftpd

在这里插入图片描述
6、控制任务执行顺序

[jia@ansible ansible]$ cat vsftpd.yaml 
---
- name: install vsftpd server
  hosts: all
  roles:
    - role: vsftpd  #角色任务
  pre_tasks:       #角色执行前执行的play
    - name: show pre
      debug:
        msg: start
  post_tasks:      #在角色和普通任务执行完毕后执行的play
    - name: show post
      debug:
        msg: end

在这里插入图片描述
7、ansible—galaxy命令工具

  • Ansible Galaxy 是一个免费共享和下载 Ansible 角色的网站,可以帮助我们更好的定义和学习roles。
  • ansible-galaxy命令默认与https://galaxy.ansible.com网站API通信,可以查找、下载各种社区开发的
    Ansible 角色传送门

安装选择的角色

install https://galaxy.ansible.com roles
ansible-galaxy install geerlingguy.nginx

install local roles

---
- src: file:///mnt/apache.tar.gz
  name: apache
nsible-galaxy install -r install_apache_role.yml

8、系统决策

[root@ansible ~]# dnf search role
[root@ansible ~]# dnf install -y rhel-system-roles.noarch
[root@ansible ~]# cd /usr/share/ansible/roles/
[root@ansible roles]# ls
linux-system-roles.kdump    linux-system-roles.storage   rhel-system-roles.postfix
linux-system-roles.network  linux-system-roles.timesync  rhel-system-roles.selinux
linux-system-roles.postfix  rhel-system-roles.kdump      rhel-system-roles.storage
linux-system-roles.selinux  rhel-system-roles.network    rhel-system-roles.timesync
[jia@ansible ansible]$ cat chronyc.yaml 
---
- name: timesync server
  vars:
    timesync_ntp_servers:
      - hostname: 172.25.51.250
        iburst: yes
  hosts: all
  roles:
    - rhel-system-roles.timesync
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贾几人要努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值