当前位置:网站首页>Roles templates static file reference variables, batch recursive copy

Roles templates static file reference variables, batch recursive copy

2020-12-07 10:28:13 n0n1

background

ansible Of yml Such as script files can directly reference variables , But what if static file classes also want to reference variables ? Like a configuration file config.json. Most of the online tutorials are in tasks Call inside templates modular , And the source file emphasizes that the suffix is .j2, And is a single file to call . Like the following :

tasks: 
    - name: generation the hello_world.txt file
         template:
              src: hello_world.txt.j2    //src There are too many file suffixes for .j2
              dest: /tmp/hello_world.txt 

If you want to batch call templates Well ?roles Provides a way of thinking . The following is suitable for a certain amount of roles Basic people

Actual needs

roles/test Character's files All copies are required ( Directories and files ).templates The configuration files in the following multi-level directories should be copied , And the reference vars Variable

[root@centos7 chawn]# tree roles
roles
├── test
│   ├── files
│   │   ├── AAA  // Catalog 
│   │   └── base.retry   // file 
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   │   ├── api
│   │   │   ├── config1
│   │   │   │   └── config
│   │   │   │       ├── api1
│   │   │   │       │   └── redis.config.js
│   │   │   │       ├── api2
│   │   │   │       │   └── redis.config.js
│   │   │   │       └── api3
│   │   │   │           └── redis.config.js
│   │   │   └── config2
│   │   │       └── config
│   │   │           ├── api1
│   │   │           │   └── redis.config.js
│   │   │           ├── api2
│   │   │           │   └── redis.config.js
│   │   │           └── api3
│   │   │               └── redis.config.txt
│   │   └── config.ini   // No catalog , Change any suffix of configuration file , For reference 
│   └── vars
│       └── main.yml
└── test.yml

vars Of main.yml

cat /home/chawn/roles/test/vars/main.yml
dir: roles_test
api_ip: 192.168.44.100
api_port: 8090

tasks Of main.yml

[root@centos7 test]# cat tasks/main.yml 
---
  - name: mkdir  //copy Create the corresponding directory before use 
    file: dest=/tmp/{{ dir }} state=directory 
  - name: copy files
    copy: src={{ item.src }} dest={{ item.dest }} mode=755
    with_items: 
      - { src: './',dest: '/tmp/{{ dir }}/' }   //src With ./  Express files All the contents and files below 
  - name: Approve certs server directories //templates Quote the current src Environmental Science , stay dest Create the same directory 
    file:
      state: directory
      dest: '/tmp/{{ dir }}/{{ item.path }}'  // here /tmp/{{ dir }}/ For customization 
    with_filetree: '../templates'                // A key part 
    when: item.state == 'directory'
  - name: Approve certs server files
    template:
      src: '{{ item.src }}'
      dest: '/tmp/{{ dir }}/{{ item.path }}'    // here /tmp/{{ dir }}/ For customization 
      mode: '{{ item.mode }}'    //templates Of dest Calling a src Of , Out-of-service 755 Fixed values like that 
      owner: '{{ item.owner }}'  // ditto 
      group: '{{ item.group }}' // ditto 
    with_filetree: '../templates'   // A key part 
    when: item.state == 'file'

test Main entrance of

[root@centos7 roles]# cat test.yml 
- hosts: test
  gather_facts: no
  tasks:
  - name: hello
    shell: echo hello
- hosts: test
  gather_facts: no
  become: yes
  become_method: su
  roles: 
  - test
- hosts: test
  gather_facts: no
  tasks:
  - name: bye
    shell: echo bye

part templates Content

[root@centos7 api3]# cat redis.config.txt 
exports.config = {
  host:'{{ api_ip }}',
  port: {{ api_port }},
  db: 0,
  prefix: 'allinone:'
}
[root@centos7 api3]# pwd
/home/chawn/roles/test/templates/api/config2/config/api3
[root@centos7 templates]# cat config.ini 
path=/tmp/{{ dir }}
[root@centos7 templates]# pwd
/home/chawn/roles/test/templates

Execution results

  1. files All the following directories and files have been copied successfully
ll /tmp/roles_test/
total 8
drwxr-xr-x. 2 root root  6 Dec  7 10:02 AAA   //files The following table of contents 
drwxr-xr-x. 4 root root 36 Dec  7 10:02 api   //templates The following table of contents 
-rwxr-xr-x. 1 root root 15 Dec  7 10:01 base.retry //files The following file 
-rw-r--r--. 1 root root 21 Dec  7 10:02 config.ini //templates The following file 
  1. templates All the following configurations were copied successfully , And the value of the variable conforms to vars Definition , The template suffix in this condition doesn't matter .
[root@centos7-2 roles_test]# cat config.ini 
path=/tmp/roles_test
[root@centos7-2 roles_test]# cat api/config2/config/api3/redis.config.txt 
exports.config = {
  host:'192.168.44.100',
  port: 8090,
  db: 0,
  prefix: 'allinone:'
}

版权声明
本文为[n0n1]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201207102618406h.html