Module example explanation in playbook

1. Examples of Templates module

1.1. Copy the template using remote replication

[root@ansible ~]# scp root@192.168.140.30:/etc/httpd/conf/httpd.conf ./
#用root身份去远程拷贝模板文件到当前目录下

Insert picture description here

[root@ansible ~]# mkdir demo   #创建demo的文件夹
[root@ansible ~]# mv httpd.conf demo   #把模板剪切到demo文件夹里,方便操作
[root@ansible ~]# cd demo
[root@ansible demo]# ls

Insert picture description here

[root@ansible demo]# vim httpd.conf         #修改配置文件

Insert picture description here
Insert picture description here

[root@ansible demo]# mv httpd.conf httpd.conf.j2  #把配置文件改变成模板文件  
注:j2是jinja2,基于python的模板引擎,提供模板功能
[root@ansible demo]# vim /etc/ansible/hosts     #进入配置文件,定义变量

Insert picture description here

[root@ansible demo]# vim apache.yml       #创建剧本
- hosts: mysql
  remote_user: root
  vars:
  - server: httpd
  tasks:
  - name: install apache
    yum: name={
    
    {
    
    server}} state=latest
  - name: config file
    template: src=/root/demo/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
    notify:
    - restart httpd
  - name: start httpd
    service: name={
    
    {
    
    server}}
  handlers:
  - name: restart httpd
    service: name={
    
    {
    
    server}} state=restart enabled=true

Insert picture description here

[root@ansible demo]# ansible-playbook apache.yml --syntax-check   #检查语法
[root@ansible demo]# ansible-playbook apache.yml    #执行文件

Insert picture description here

[root@mysql ~]# cd /etc/httpd/conf
[root@mysql conf]# ll

Insert picture description here

[root@mysql conf]# vim httpd.conf

Insert picture description here
Insert picture description here

2. Examples of tags module

Note: In a playbook, we generally define many tasks. If we only want to execute one or more tasks, we can use the tags tag function

[root@ansible ~]# vim hosts.yml
- hosts: webserver
  remote_user: root
  tasks:
  - name: copy hosts
    copy: src=/etc/hosts dest=/opt/hosts
    tags:
    - ms
  - name: touch hosts01
    file: path=/opt/hosts01 state=touch

Insert picture description here

[root@ansible ~]# ansible-playbook hosts.yml --tags="ms"

Insert picture description here

[root@webserver opt]# ll

Insert picture description here

2.1, the same label experiment

[root@ansible ~]# vim hosts.yml

Insert picture description here

[root@ansible ~]# ansible-playbook hosts.yml --tags="ms"

Insert picture description here

[root@webserver opt]# ll

Insert picture description here
Summary: If you need to perform several tasks, you only need to tag the tasks with the same name, and the ones that are not tagged will not be executed.

2.2, the label named always experiment

[root@ansible ~]# vim hosts.yml

Insert picture description here

[root@ansible ~]# ansible-playbook hosts.yml --tags="ms"

Insert picture description here

[root@webserver opt]# ll

Insert picture description here

3. Summary

Only if you are familiar with and master the modules in ansible and playbook, can you better use ansible to work

Guess you like

Origin blog.csdn.net/weixin_50344814/article/details/112646035