2021年11月17日 星期三

由command line 傳遞變數給ansible 與docker-compose

1.由command line傳送變數給ansible

  command:

    ansible-playbook -i hosts.yml --extra-vars "a=2 b=3" tasks/xxx.yml

    ansible-playbook -i hosts.yml -e "a=2 b=3" tasks/xxx.yml

  ansible:

    tasks:

      - name: add folder

        file:

          state: directory

          path: '{{a}}'

2.由ansible command line傳送陣列變數給ansible

  以json傳遞

  command:

    ansible-playbook -i hosts.yml --extra-vars "{'myitems':[1,2,3]}" tasks/xxx.yml

  ansible:

    loop: {{myitems}}

    tasks:

      - name: add loop folder

        file:

          state: directory

          path: '{{item}}'

3.由command line傳送變數給docker-compose

  ex:

  command:

    port=12345 docker-compose up -d

  docker-compose:

    ports:

      - ${api_port}:12345

4.由command line傳送變數給ansible再傳給docker-compose

  ex:

  command:

    ansible-playbook -i hosts.yml --extra-vars "api_port=12345" tasks/ansible.yml

  ansible.yml:

    tasks:

    - name: Shell Start node-api-server

      shell:

        cmd: 'port={{api_port}} docker-compose --compatibility up -d'

  docker-compose.yml:

    ports:

      - ${api_port}:12345

沒有留言: