目录
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