[ansible] playbook运用

本文详细介绍了AnsiblePlaybook中的剧本编写,包括设置变量、引用事实信息、when条件判断、变量循环、Templates模块的应用以及Tags的使用,以安装和配置Nginx和Apache为例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、复习playbook剧本

---
- name: first play for install nginx   #设置play的名称
  gather_facts: false                  #设置不收集facts信息
  hosts: webservers:dbservers          #指定执行此play的远程主机组
  remote_user: root                    #指定执行此play的用户
  tasks:                               #指定此play的任务列表
  - name: disabled firewalld
    service: name=firewalld  state=stopped  enabled=no
  - name: disable selinux
    command: '/sbin/setenforce 0'
    ignore_errors: yes
  - name: disabled selinux forever
    replace: path=/etc/selinux/config  regexp=enforcing  replace=disabled  after=loaded
  - name: mount cdrom
    mount: src=/dev/sr0 path=/mnt fstype=iso9660 state=mounted
  - name: install pkgs
    yum: name=pcre-devel,zlib-devel,openssl-devel,gcc,gcc-c++,make state=latest
  - name: create nginx user
    user: name=nginx create_home=no shell=/sbin/nologin
  - name: unarchive nginx package
    unarchive: copy=yes src=/etc/ansible/nginx/nginx-1.24.0.tar.gz dest=/opt/
  - name: install nginx by source
    shell: chdir=/opt/nginx-1.24.0/ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
  - name: create link file for nginx
    file: state=link src=/usr/local/nginx/sbin/nginx path=/usr/local/sbin/nginx
  - name: create nginx service file
    copy: src=nginx.service dest=/lib/systemd/system/nginx.service
  - name: start nginx
    service: name=nginx state=started enabled=yes

 

 

二、playbook的定义、引用变量

2.1 基础变量的定义与引用

在yaml文件中,我们可以在初始配置的模块中用var去定义变量的存在,变量的格式为key:value,以此来确定该变量在剧本中的存在

vim test1.yaml
---
- name: this is a play for testing variables
  hosts: dbservers
  remote_user: root
  vars:
   filename: abc.txt
  tasks:
   - name: touch a test file
     file: path=/opt/{
  {filename}} state=touch
 
 
ansible-playbook test1.yaml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值