13
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ansibleのfactを使ってシステム変数を取得する

Last updated at Posted at 2019-01-05

Ansibleのfactsを使って、システム変数を取得する

- hosts: role_bastion
  user: ubuntu
  become_method: sudo
  gather_facts: yes

  roles:
    - verifi
tasks/main.yml
---
- name: echo environment
  debug:
    var: "{{ item }}"
  with_items:
    - "{{ env }}"

- name: print ansible variables
  debug:
    var: '{{ item }}'
  with_items:
    - inventory_hostname
    - ansible_os_family
    - ansible_distribution
    - ansible_distribution_version
    - ansible_kernel
    - ansible_architecture
    - ansible_default_ipv4.address
    - ansible_default_ipv4.interface
    - ansible_default_ipv4.netmask
    - ansible_default_ipv4.network
    - ansible_dns.nameservers
    - ansible_effective_user_id
    - ansible_effective_group_id
    - ansible_env.HOME
    - ansible_env.SHELL
    - ansible_eth0.ipv4.address
    - ansible_processor_cores
    - ansible_processor_vcpus

- name: output env parameter as file
  template:
    src: env_params.j2
    dest: /home/ubuntu/output_params_ansible_facs
    owner: ubuntu
    group: ubuntu
    mode: 0644
templates/main.yml
{{ inventory_hostname }}
{{ ansible_os_family }}
{{ ansible_distribution }}
{{ ansible_distribution_version }}
{{ ansible_kernel }}
{{ ansible_architecture }}
{{ ansible_default_ipv4.address }}
{{ ansible_default_ipv4.interface }}
{{ ansible_default_ipv4.netmask }}
{{ ansible_default_ipv4.network }}
{{ ansible_dns.nameservers }}
{{ ansible_effective_user_id }}
{{ ansible_effective_group_id }}
{{ ansible_env.HOME }}
{{ ansible_env.SHELL }}
{{ ansible_eth0.ipv4.address }}
{{ ansible_processor_cores }}
{{ ansible_processor_vcpus }}
  • 実行結果
PLAY [role_bastion] ********************************************************************************************

TASK [Gathering Facts] *****************************************************************************************
ok: [localhost]

TASK [verifi : echo environment] *******************************************************************************
ok: [localhost] => (item=dev) => {
    "dev": "VARIABLE IS NOT DEFINED!",
    "item": "dev"
}

TASK [verifi : print ansible variables] ************************************************************************
ok: [localhost] => (item=inventory_hostname) => {
    "inventory_hostname": "localhost",
    "item": "inventory_hostname"
}
ok: [localhost] => (item=ansible_os_family) => {
    "ansible_os_family": "Debian",
    "item": "ansible_os_family"
}
ok: [localhost] => (item=ansible_distribution) => {
    "ansible_distribution": "Ubuntu",
    "item": "ansible_distribution"
}
ok: [localhost] => (item=ansible_distribution_version) => {
    "ansible_distribution_version": "16.04",
    "item": "ansible_distribution_version"
}
ok: [localhost] => (item=ansible_kernel) => {
    "ansible_kernel": "4.4.0-1072-aws",
    "item": "ansible_kernel"
}
ok: [localhost] => (item=ansible_architecture) => {
    "ansible_architecture": "x86_64",
    "item": "ansible_architecture"
}
ok: [localhost] => (item=ansible_default_ipv4.address) => {
    "ansible_default_ipv4.address": "10.0.1.113",
    "item": "ansible_default_ipv4.address"
}
ok: [localhost] => (item=ansible_default_ipv4.interface) => {
    "ansible_default_ipv4.interface": "eth0",
    "item": "ansible_default_ipv4.interface"
}
ok: [localhost] => (item=ansible_default_ipv4.netmask) => {
    "ansible_default_ipv4.netmask": "255.255.255.0",
    "item": "ansible_default_ipv4.netmask"
}
ok: [localhost] => (item=ansible_default_ipv4.network) => {
    "ansible_default_ipv4.network": "10.0.1.0",
    "item": "ansible_default_ipv4.network"
}
ok: [localhost] => (item=ansible_dns.nameservers) => {
    "ansible_dns.nameservers": [
        "10.0.0.2"
    ],
    "item": "ansible_dns.nameservers"
}
ok: [localhost] => (item=ansible_effective_user_id) => {
    "ansible_effective_user_id": 1000,
    "item": "ansible_effective_user_id"
}
ok: [localhost] => (item=ansible_effective_group_id) => {
    "ansible_effective_group_id": 1000,
    "item": "ansible_effective_group_id"
}
ok: [localhost] => (item=ansible_env.HOME) => {
    "ansible_env.HOME": "/home/ubuntu",
    "item": "ansible_env.HOME"
}
ok: [localhost] => (item=ansible_env.SHELL) => {
    "ansible_env.SHELL": "/bin/bash",
    "item": "ansible_env.SHELL"
}
ok: [localhost] => (item=ansible_eth0.ipv4.address) => {
    "ansible_eth0.ipv4.address": "10.0.1.113",
    "item": "ansible_eth0.ipv4.address"
}
ok: [localhost] => (item=ansible_processor_cores) => {
    "ansible_processor_cores": 1,
    "item": "ansible_processor_cores"
}
ok: [localhost] => (item=ansible_processor_vcpus) => {
    "ansible_processor_vcpus": 2,
    "item": "ansible_processor_vcpus"
}

TASK [verifi : output env parameter as file] *******************************************************************
--- before
+++ after: /home/ubuntu/.ansible/tmp/ansible-local-3830A34Lzo/tmpJscd8s/env_params.j2
@@ -0,0 +1,18 @@
+localhost
+Debian
+Ubuntu
+16.04
+4.4.0-1072-aws
+x86_64
+10.0.1.113
+eth0
+255.255.255.0
+10.0.1.0
+[u'10.0.0.2']
+1000
+1000
+/home/ubuntu
+/bin/bash
+10.0.1.113
+1
+2

changed: [localhost]

PLAY RECAP *****************************************************************************************************
localhost                  : ok=4    changed=1    unreachable=0    failed=0
  • $ cat output_params_ansible_facs
localhost
Debian
Ubuntu
16.04
4.4.0-1072-aws
x86_64
10.0.1.113
eth0
255.255.255.0
10.0.1.0
[u'10.0.0.2']
1000
1000
/home/ubuntu
/bin/bash
10.0.1.113
1
2

まとめ

ansible_factを利用して様々なシステム情報を取得することができた。
これで、システム変数を利用したplaybookやtasksを条件に応じた定義をすることができる。

13
14
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
13
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?