Home > Software engineering >  Want to build multiple Dockerfile via Ansible
Want to build multiple Dockerfile via Ansible

Time:05-10

I am trying to make Ansible-Playbook that builds multiple Dockerfile


│   ├── aa
│   │   ├── 11
│   │   │   └── ubuntu.18.04
│   │   │       └── Dockerfile
│   │   ├── 22
│   │   │   ├── ubuntu.16.04
│   │   │   │   └── Dockerfile
│   │   │   └── ubuntu.20.04
│   │   │       └── Dockerfile
│   │   ├── 33
│   │   │   └── ubuntu.20.04
│   │   │       └── Dockerfile
│   │   └── 44
│   │   │   └── ubuntu.18.04
│   │   │       └── Dockerfile

I attached my tree structure.

there are multiple directories such as aa bb and etc.

will there be more efficient way to get list and make build command?

I am now making every single playbook. I think this is not a good way, but still this is better at least pressing every build command in hand.

  - name: Build aa 11 ubuntu 18.04 images 
    command: docker build -t "{{basic_directory}}/aa/11/ubuntu.18.04:{{version}}" .

  - name: Build aa 22 ubuntu 16.04 images 
    command: docker build -t "{{basic_directory}}/aa/22/ubuntu.16.04:{{version}}" .

  - name: Build aa 22 ubuntu 18.04 images 
    command: docker build -t "{{basic_directory}}/aa/22/ubuntu.18.04:{{version}}" .

adding codes

  - find:
      path: "{{basic_directory}}"
      recurse: true
      patterns: 'Dockerfile'
    register: result

CodePudding user response:

Given the tree

shell> tree conf/
conf/
└── aa
    ├── 11
    │   └── ubuntu.18.04
    │       └── Dockerfile
    ├── 22
    │   ├── ubuntu.16.04
    │   │   └── Dockerfile
    │   └── ubuntu.20.04
    │       └── Dockerfile
    ├── 33
    │   └── ubuntu.20.04
    │       └── Dockerfile
    └── 44
        └── ubuntu.18.04
            └── Dockerfile

and the variables

  basic_directory: conf
  version: '1.0'

Use the find module to get the list of the paths

    - find:
        path: "{{ basic_directory }}"
        recurse: true
        pattern: Dockerfile
      register: result
  result.files|map(attribute='path')|list:
  - conf/aa/11/ubuntu.18.04/Dockerfile
  - conf/aa/33/ubuntu.20.04/Dockerfile
  - conf/aa/44/ubuntu.18.04/Dockerfile
  - conf/aa/22/ubuntu.20.04/Dockerfile
  - conf/aa/22/ubuntu.16.04/Dockerfile

Iterate the list

    - debug:
        msg: >-
          command: docker build -t {{ item|dirname }}:{{ version }} .
      loop: "{{ result.files|map(attribute='path')|list }}"

gives (abridged)

  msg: 'command: docker build -t /tmp/aa/11/ubuntu.18.04:1.0 .'
  msg: 'command: docker build -t /tmp/aa/33/ubuntu.20.04:1.0 .'
  msg: 'command: docker build -t /tmp/aa/44/ubuntu.18.04:1.0 .'
  msg: 'command: docker build -t /tmp/aa/22/ubuntu.20.04:1.0 .'
  msg: 'command: docker build -t /tmp/aa/22/ubuntu.16.04:1.0 .'

Optionally, it is possible to find the directories only, e.g.

    - find:
        path: "{{ basic_directory }}"
        recurse: true
        file_type: directory
        patterns: ubuntu*
      register: result
  result.files|map(attribute='path')|list:
  - conf/aa/11/ubuntu.18.04
  - conf/aa/33/ubuntu.20.04
  - conf/aa/44/ubuntu.18.04
  - conf/aa/22/ubuntu.20.04
  - conf/aa/22/ubuntu.16.04

Iterate the list

    - debug:
        msg: >-
          command: docker build -t {{ item }}:{{ version }} .
      loop: "{{ result.files|map(attribute='path')|list }}"

gives the same result

  msg: 'command: docker build -t /tmp/aa/11/ubuntu.18.04:1.0 .'
  msg: 'command: docker build -t /tmp/aa/33/ubuntu.20.04:1.0 .'
  msg: 'command: docker build -t /tmp/aa/44/ubuntu.18.04:1.0 .'
  msg: 'command: docker build -t /tmp/aa/22/ubuntu.20.04:1.0 .'
  msg: 'command: docker build -t /tmp/aa/22/ubuntu.16.04:1.0 .'
  • Related