PerlでYAML読む
AnsibleのTipsではないけども。
- perl-YAML-Tiny
# Get values form yaml file
sub get_value{
my $para = shift;
my $data= YAML::Tiny->new;
$data = YAML::Tiny->read( "./text.yml" );
my $yaml = $data->[0];
return $yaml->{$para};
}
下記のようなファイルをパラメータファイルとして読ませるために使った。
hoge: "A"
fuga: 3
また、コンビネーションの
hoge: { name: "string" , url: "http://123" }
のようなファイルはダンプできなかったので、
# "string" is
# $data->[0]->{hoge}->{name}
hoge:
name: "string"
url: "http://123"
のように書いた。
ディレクトリ配下のデータを全部・・・という時
with_fileblob
のループを使う。
http://docs.ansible.com/ansible/playbooks_loops.html#id4
- fetch src="{{ item }}" dest="{{ savedir }}"
with_fileglob:
- /playbooks/files/fooapp/*
file モジュールでワイルドカードが使えないための対処方
v2.0 以降でfind
モジュールがありそこでレジストして、呼び出して使う
例、/tmp以下の .sh ファイルを全部 0755へパーミッション変更
- name: find *.sh
find: paths="/tmp" patterns="*.sh"
register: sh_list
- debug: msg="{{ item.path }}"
with_items: '{{ sh_list.files }}'
- debug: var=item.path
with_items: '{{ sh_list.files }}'
- name: change test *.sh files
file: path="{{ item.path }}" owner=root group=root mode=755
with_items:
- '{{ sh_list.files }}'
become: true
become_user: root
ファイルを読んでパラメータにする方法
以下のようなファイルがあるときに、1行毎に変数に当て込んで処理を実行できる。
filelist.txt
/etc/ssh/sshd_config
/var/log/syslog
/etc/nsswitch.conf
/etc/hosts
例えば、inputfile に filelist.txt が入っているとする。
- name: Fetch files
fetch: src="{{ item }}" dest=/tmp/destdir
with_lines:
- cat "{{ inputfile }}"
- with_lines ではUNIX/Linuxのコマンドを実行し、結果をループできる
- cat で得られた標準出力一行ずつを、item に入れて fetch 処理がループされる
- http://docs.ansible.com/ansible/playbooks_loops.html