首页 > 其他分享 >ansible include_tasks示例

ansible include_tasks示例

时间:2024-08-20 16:06:04浏览次数:13  
标签:git name 示例 TASK yaml tasks include

目录

include_tasks 导入任务

任务文件只包含任务,作用主机范围由playbook.yaml决定

include_tasks.yaml

---
- name: restart memcached
  service:
    name: memcached
    state: restarted

playbook.yaml

---
  - hosts: ubuntu
    tasks:
      - name: get date time
        command: "date +'%F %T'"
        register: date
      - name: get date time
        include_tasks: include_tasks.yaml
ansible-playbook playbook.yaml 

PLAY [ubuntu] *****

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

TASK [get date time] *****
changed: [192.168.255.110]

TASK [get date time] *****
included: /root/include/include_tasks.yaml for 192.168.255.110

TASK [restart memcached] *****
changed: [192.168.255.110]

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

include_tasks 拉取ansible代码示例

目录结构如下

tree ./gitclone
./gitclone
├── create_dir.yaml
├── git_checkout.yaml
├── playbook.yaml
├── static_git_pull.yaml
├── user-config.yaml
└── vars
    └── main.yaml
---
  - hosts: ubuntu
    vars:
      username: test 
      ssh_private_keys:
        - { src: /opt/id_rsa, dest: id_rsa }
    vars_files:
      vars/main.yaml
    tasks:
     - name: debug vars
       debug:
         msg: "{{ service_name }}"

     - include_tasks: user-config.yaml
       tags: user-config
     - include_tasks: ./create_dir.yaml
       tags: create_dir
     - include_tasks: ./static_git_pull.yaml
       tags: git_pull
     - include_tasks: ./git_checkout.yaml
       tags: git_check

vars/main.yaml

---
repository_static: "https://github.com/ansible/ansible.git"
package_dir: "/opt/"
project: "ansible"
git_commit: "devel"
flag: "2.17.3"
username: "test"
service_name: "memcached"

user-config.yaml

---
    - name: Add profile info for user.
      copy:
        src: "/home/{{ username }}/.profile" # copy功能模块
        dest: "/home/{{ username }}/.profile" # copy功能模块
        owner: "{{ username }}" # copy模块owner属性
        group: "{{ username }}" # copy模块group属性
        mode: 0744 # copy模块mode属性

    - name: Add private keys for user.
      copy:
        src: "{{ item.src }}"
        dest: "/home/{{ username }}/.ssh/{{ item.dest }}"
        owner: "{{ username }}"
        group: "{{ username }}"
        mode: 0600
      with_items: "{{ ssh_private_keys }}"

    - name: Restart  service_name  service.
      service: 
        name:  "{{ service_name }}"
        state: restarted # service模块,重启名为example的服务这些任务负责检出git变量

create_dir.yaml

---
- name: create dir
  # file模块,用于设置文件属性
  file: 
    path: "{{ package_dir }}{{ project }}-release-{{ git_commit }}/{{ flag }}-{{ project }}"
    owner: "{{ username }}"
    group: "{{ username }}"
    mode: 0755 
    recurse: yes 
    state: directory

static_git_pull.yaml

---
    - name: Git pull
      git: 
        repo: "{{ repository_static }}"
        dest: "{{ package_dir }}{{ project }}-release-{{ git_commit }}/{{ flag }}-{{ project }}"
        version: "{{ git_commit }}" 
        force: yes # git模块,结合Ansible后用于自动化版本管理

git_checkout.yaml

---
# 这些任务负责检出git变量
- name: Git init before git pull
  command: /usr/bin/git fetch
  args:
       chdir: "{{ package_dir }}{{ project }}-release-{{ git_commit }}/{{ flag }}-{{ project }}"
- name: Git reset
  command: /usr/bin/git reset --hard
  args:
       chdir: "{{ package_dir }}{{ project }}-release-{{ git_commit }}/{{ flag }}-{{ project }}"
- name: Git checkout
  command: /usr/bin/git checkout {{ git_commit }}
  args:
       chdir: "{{ package_dir }}{{ project }}-release-{{ git_commit }}/{{ flag }}-{{ project }}"
ansible-playbook playbook.yaml 

PLAY [ubuntu] *****

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

TASK [debug vars] *****
ok: [107.151.199.209] => {
    "msg": "memcached"
}

TASK [include_tasks] *****
included: /root/gitclone/user-config.yaml for 107.151.199.209

TASK [Add profile info for user.] *****
ok: [107.151.199.209]

TASK [Add private keys for user.] *****
ok: [107.151.199.209] => (item={'src': '/opt/id_rsa', 'dest': 'id_rsa'})

TASK [Restart  service_name  service.] *****
changed: [107.151.199.209]

TASK [include_tasks] *****
included: /root/gitclone/create_dir.yaml for 107.151.199.209

TASK [create dir] *****
changed: [107.151.199.209]

TASK [include_tasks] *****
included: /root/gitclone/static_git_pull.yaml for 107.151.199.209

TASK [Git pull] *****
changed: [107.151.199.209]

TASK [include_tasks] *****
included: /root/gitclone/git_checkout.yaml for 107.151.199.209

TASK [Git init before git pull] *****
changed: [107.151.199.209]

TASK [Git reset] *****
changed: [107.151.199.209]

TASK [Git checkout] *****
changed: [107.151.199.209]

PLAY RECAP *****
107.151.199.209            : ok=14   changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

标签:git,name,示例,TASK,yaml,tasks,include
From: https://www.cnblogs.com/anyux/p/18369612

相关文章

  • C10-02-HTML示例
    HTML:02-1.html基本功能实现:<a>和<img>标签联合使用及<imgsrc="#">图片资源绝对路径引用JS使用:行内式、内嵌式、引入外部JS<input>标签:输入标签文本框<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8">......
  • 数据结构之 红黑树入门教程、红黑树代码示例
    红黑树(Red-BlackTree)是一种自平衡的二叉查找树(BST),它在插入、删除和查找操作后通过一些特定的规则来维护树的平衡,从而确保这些操作的时间复杂度始终为O(logn)。红黑树主要应用在需要高效动态集合操作的场景中,如操作系统中的进程调度器、数据库中的索引等。红黑树的基本性......
  • TopoJSON格式详解,写入读取TopoJSON示例
    还是大剑师兰特:曾是美国某知名大学计算机专业研究生,现为航空航海领域高级前端工程师;CSDN知名博主,GIS领域优质创作者,深耕openlayers、leaflet、mapbox、cesium,canvas,webgl,echarts等技术开发,欢迎加底部微信(gis-dajianshi),一起交流。No.内容链接1Openlayers【入门教程】-......
  • 多重示例详细说明Eureka原理实践
    Eureka原理(EurekaPrinciple)是指在长时间的思考和积累之后,通过偶然的瞬间获得灵感或发现解决问题的方法的一种认知现象。这个过程通常包括三个主要阶段:准备阶段、潜伏期以及突然的灵感爆发。下面详细说明Eureka原理的实践步骤:1.准备阶段广泛阅读与研究:在这个阶段,研究者需......
  • AES常用的代码示例
    AESAES是对称加密。对称加密是指加密和解密使用相同的密钥的加密算法。非对称加密是指加密和解密使用不同的密钥的加密算法。AES加密解密加密模式,有ECB模式和CBC模式等等,ECB不需要iv偏移量,而CBC需要。密钥,可以自定义。填充方式,有PKCS5、PKCS7、NoPadding。......
  • RabbitMQ消息队列:概念、单节点和集群示例
    目录消息队列概念主流的消息队列消息队列名词(1)Broker(2)Topic(3)Producer(4)Consumer(5)Queue(6)Message消息队列中两种工作模式Point-to-Point(PTP、点到点)Pub/Sub消息队列的缺点系统可用性降低系统复杂性提高数据一致性无法保证RabbitMQ相关术语(1)生产者(2)消费者(3)队......
  • axios取消请求CancelToken的原理解析及用法示例
    文章目录一、axios的实例与请求流程二、CancelToken的作用三、CancelToken的实现原理四、取消请求的流程五、CancelToken用法六、利用拦截器取消请求1、axios请求拦截器2、axios响应拦截器3、利用路由导航守卫取消请求一、axios的实例与请求流程下图是axios实例......
  • Java中ArrayList集合—基础详解(知识点+代码示例)
    ArrayList(集合)ArrayList(集合)ArrayList(集合)10.1ArrayList成员方法10.2集合练习10.2.1添加字符串10.2.2添加数字10.2.3添加学生对象并遍历10.2.4集合概述:集合可以直接存储引用数据类型,不能直接存储基本数据类型,如果要存储基本数据类型,需要将基本数据类型变成对......
  • Sonarqube 自定义规则,部署SonarSource / sonar-java源码中示例规则:docs/java-custom-r
    自定义规则,可以参考sonar-java/docs/CUSTOM_RULES_101.mdat8.0.0.36314·SonarSource/sonar-java·GitHub1、下载一份sonarqube源码,配置好本地的环境,JDK17和mavendocs/java-custom-rules-example示例项目中会有写好的规则;我们可以先尝试将这些写好的规则添加到Sonarqube......
  • 基于SpringBoot的停车场管理系统+毕业设计示例参考
    郑重声明:项目经过本地测试,确保可以运行。项目仅供学习和毕业设计参考~1.项目介绍技术栈+工具:SpringBoot+BootStrap+MySQL5.7+IDEA2022+Maven系统功能:系统管理、角色管理、车辆管理、车位管理、停车记录、充值、停车出场等系统角色:管理员、普通用户(可自行设定角......