LoginSignup
0

More than 5 years have passed since last update.

AnsibleでDocker inspectのフォーマットを指定して値を取得する方法({{}}のエスケープ)

Last updated at Posted at 2018-09-12

AnsibleでDockerコンテナのIPアドレスを取得したいと思いshellモジュールでやってみたのですがうまくいかなかったので調べた結果の備忘録です。

Playbook

'{% raw %}{% endraw %}' で括ると文字列で扱われてフォーマットが指定できました。

---
- name: Get docker container ip address.
  hosts: localhost
  gather_facts: no
  vars:
    docker_container: test
  tasks:
    - shell: "docker inspect -f '{% raw %}{{ .NetworkSettings.IPAddress }}{% endraw %}' {{ docker_container }}"
      register: r

    - debug: msg="{{ r.stdout }}"

実行結果

[root@localhost ~]# ansible-playbook main.yml
 [WARNING]: Could not match supplied host pattern, ignoring: all

 [WARNING]: provided hosts list is empty, only localhost is available


PLAY [Get docker container ip address.] **************************************************************************************************************************

TASK [command] ***************************************************************************************************************************************************
changed: [localhost]

TASK [debug] *****************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "172.17.0.2"
}

PLAY RECAP *******************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0

参考

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0