6
7

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 Tips

Last updated at Posted at 2016-08-23

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 }}"
6
7
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
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?