Ansible - How to combine items

Viewed 112

I would like to make certificates by certbot command

var:
  domain_name: game.com
  server_name: 
         - -d api 
         - -d im

tasks:
  name: Generate new certificate
  shell: "certbot --nginx certonly --noninteractive --agree-tos --email xxx@mail.com {{ server_name }}.{{ domain_name }}"

I'd like to this:

certbot --nginx certonly --noninteractive --agree-tos --email xxx@mail.com -d api.game.com -d im.game.com

but it is:

certbot --nginx certonly --noninteractive --agree-tos --email xxx@mail.com -d api -d im.game.com

How to combine the item ?

1 Answers

Test the concatenation of the string, e.g.

- hosts: localhost
  vars:
    domain_name: game.com
    server_name:
      - "-d api"
      - "-d im"

  tasks:
    - name: Display command to generate new certificate
      debug:
        msg: "{{ _part1 ~ _part2 }}"
      vars:
        _part1: "certbot --nginx certonly --noninteractive --agree-tos --email xxx@mail.com"
        _part2: " {{ server_name|product([domain_name])|map('join', '.')|join(' ') }}"

gives

  msg: certbot --nginx certonly --noninteractive --agree-tos --email xxx@mail.com -d api.game.com -d im.game.com

If this is what you want try

    - name: Generate new certificate
      command: "{{ _part1 ~ _part2 }}"
      vars:
        _part1: "certbot --nginx certonly --noninteractive --agree-tos --email xxx@mail.com"
        _part2: " {{ server_name|product([domain_name])|map('join', '.')|join(' ') }}"
Related