LoginSignup
3
2

More than 3 years have passed since last update.

/etc/hosts形式のファイルをAnsible用インベントリファイルに変換するワンライナー

Posted at

これなに

  • 以下の様に/etc/hostsがあったとする。
    • 往々にして雑なコメントアウトがあったりする。
/etc/hosts
192.168.1.1       R1
192.168.1.2       R2
192.168.1.3       R3
# 192.168.1.4     R4 (現在設置中)
  • これを以下のように変換したい
inventory_for_ansible
R1       ansible_host=192.168.1.1
R2       ansible_host=192.168.1.2
R3       ansible_host=192.168.1.3

ワンライナー

  • あっさり。
~/
cat /etc/hosts |  grep -v "#"  | awk  '{printf "%s \t ansible_host=%s\n",$2,$1}' > inventory
3
2
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
3
2