ansible-playbook

YAML语法和playbook写法

ansible的playbook采用了yaml语法,它简单地实现了json格式的事件描述。在学习ansible playbook之前,很有必要把yaml的语法格式、引用方式做个梳理。

一、初步说明

以一个简单的playbook为例,说明yaml的基本语法

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-z47ovMSe-1666872340226)(file:///C:/Users/lenovo/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg)]

\1. yaml⽂件以 — 开头,以表明这是⼀个yaml⽂件,就像xml⽂件在开头使⽤ <?xml version="1.0" encoding="utf-8"?> 宣称它是xml⽂件⼀样。但即使没有使⽤ — 开头,也不会有什么影响。

\2. yaml中使⽤"#“作为注释符,可以注释整⾏,也可以注释⾏内从”#"开始的内容。

\3. yaml中的字符串通常不⽤加任何引号,即使它包含了某些特殊字符。但有些情况下,必须加引号,最常见的是在引⽤变量的时候。

\4. 关于布尔值的书写格式,即true/false的表达⽅式。其实playbook中的布尔值类型⾮常灵活,可分为两种情况:

模块的参数: 这时布尔值作为字符串被ansible解析。接受yes/on/1/true/no/of f /0/false,这时被ansible解析。例如上⾯⽰例中的 update_cache=yes 。

⾮模块的参数: 这时布尔值被yaml解释器解析,完全遵循yaml语法。接受不区分⼤⼩写的 true/yes/on/y/f alse/no/off /n。例如上⾯的 gpgcheck=no 和 enabled=True 。建议遵循ansible的官⽅规范,模块的布尔参数采⽤yes/no,⾮模块的布尔参数采⽤True/False

playbook的内容

每个play都包含⼀个hosts和⼀个tasks,hosts定义的是inventory中待控制的主机,tasks下定义的是⼀系列task任务列表,⽐如调⽤各个模块。这些task按顺序⼀次执⾏⼀个,直到所有被筛选出来的主机都执⾏了这个task之后才会移动到下⼀个task上进⾏同样的操作。

需要注意的是,虽然只有被筛选出来的主机会执⾏对应的task,但是所有主机(此处的所有主机表⽰的是,hosts选项所指定的那些主机)都会收到相同的task指令,所有主机收到指令后,ansible主控端会筛选某些主机,并通过ssh在远程执⾏任务。

YAML字典

YAML中使用的key/value对也称为字典、散列或关联数组

在key/value对中,键与值通过由冒号和空格组成的分隔符隔开

name: svcrole

svcservice: http

svcport: 80

字典也可以使用内嵌块格式表示,其中多个key/value对用花括号括起,并由逗号和空格隔开

- {name: svcrole, svcservice: http, svcport: 80}

YAML列表

在YAML中,列表类似于其他编程语言中的数组

为表示一组列表项,使用一个短划线加一个空格作为每个列表项的前缀

hosts:

- server1

- server2

列表也可使用内嵌块表示,其中多个列表项用方括号括起来并由逗号和空格隔开

hosts: [server1, server2]

playbook的使用

ansible-playbook用于运行剧本,-C 测试运行结果,并不是真的执行任务。

一、部署web服务器

1、部署yum仓库
2、安装httpd
3、新建/www目录
4、在/www中新建index.html,内容为my name is chenyu(chenyu为你们自己名字的全拼)
5、该web服务器的DocumentRoot为/www
5、实现在ansible中能够使用http://node2,3访问到该网页内容

[student@ansible ansible]$ cat a.yml 
---
- name: web station
  hosts: node2,node3
  tasks:
   - name: mount cdrom
     mount: 
       src: /dev/cdrom
       path: /mnt
       fstype: iso9660
       state: mounted

   - name: yum_repo
     yum_repository: 
       file: server
       name: BaseOS
       description: CtenOS8
       baseurl: file:///mnt/BaseOS
       enabled: yes
       gpgcheck: no

   - name: yum_repo2
     yum_repository: 
       file: server
       name: AppStream
       description: CtenOS8
       baseurl: file:///mnt/AppStream
       enabled: yes
       gpgcheck: no
     
   - name: install httpd
     dnf: 
       name: httpd
       state: present
       
   - name: create /www
     file: 
       src: /var/www/html
       dest: /www
       state: link
       mode: 0755

   - name: get html
     get_url: 
       url: http://192.168.244.139/index.html
       dest: /www
       mode: 0644

   - name: echo
     shell: echo my name is dly > /www/index.html


   - name: firewalld http
     firewalld: 
       rich_rule: rule family=ipv4 source address=192.168.244.0/24 service name=http accept
       permanent: yes
       immediate: yes
       state: enabled

   - name: context
     file: 
       path: /www/index.html
       setype: httpd_sys_content_t
       state: present

   - name: restorecon 
     shell: 
       cmd: restorecon -Rv /www/index.html
       
   - name: modify apache config
     replace: 
       path: /etc/httpd/conf/httpd.conf
       regexp: 'DocumentRoot "/var/www/html"'
       replace: 'DocumentRoot "/www"'

   - name: modify apache config2
     replace: 
       path: /etc/httpd/conf/httpd.conf
       regexp: <DocumentRoot "/var/www">
       replace: <DocumentRoot "/www">

   - name: restart httpd
     service: 
       name: httpd
       state: restarted
       enabled: yes
       
[student@ansible ansible]$ curl http://node2
my name is dly

[student@ansible ansible]$ curl http://node3
my name is dly

二、使用notify…handlers

1、写一个剧本runtime.yml,只对node1操作
2、创建用户aa,该用户不能用于登录,家目录/www
3、在/www创建一个文件html
4、每次执行该剧本时,将系统的当前时间输入到html文件中。
5、如果html中的时间发生变化,那么创建/tmp/kk的文件

[student@ansible ansible]$ cat runtime.yml 
---
- name: test
  hosts: node1
  tasks:
    - name: create useraa
      user:
        name: aa
        shell: /sbin/nologin
        create_home: yes
        home: /www

    - name: create html
      file:
        path: /www/html
        state: touch

    - name: date
      shell: date > /www/html

      notify:
        - kk

  handlers:
    - name: kk
      file:
        path: /tmp/kk
        state: touch

[student@ansible ansible]$ ansible-playbook runtime.yml 

PLAY [test] ********************************************************************

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

TASK [create useraa] ***********************************************************
changed: [node1]

TASK [create html] *************************************************************
changed: [node1]

TASK [date] ********************************************************************
changed: [node1]

RUNNING HANDLER [kk] ***********************************************************
changed: [node1]

PLAY RECAP *********************************************************************
node1                      : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值