- debug: msg="{{ lookup('template', './some_template.j2') }}"
debug: msg="{{ lookup('vars', var1) }}"
date_utc: "{{ lookup('pipe','date +%Y-%m-%dT%H:%M:%S -d "00:00:00 JST 1 days ago" -u') }}"
date_utc: >- {{ lookup('pipe','date +%Y-%m-%dT%H:%M:%S -d "00:00:00 JST 1 days ago" -u') }}
name: {{ name1 }}
name: bar
- hosts: localhost connection: local vars: name1: foo list1: - "{{ lookup('template', 'var1.yml') | from_yaml }}" - "{{ lookup('template', 'var2.yml') | from_yaml }}" tasks: - debug: var: list1
TASK [debug] ***************************************************************************************************************************************** ok: [localhost] => { "list1": [ { "name": "foo" }, { "name": "bar" } ] }
--- - hosts: localhost connection: local gather_facts: false become: false vars: - debug: 1 - users: [] - name: "emails" prompt: "Enter emails (user1@example.com,user2@example.com,...)" confirm: no private: no default: "" tasks: - block: - set_fact: users: > {{ users + [{ 'email': item , 'password': lookup('password', '/tmp/user.' + item + '.password.txt length=16 chars=ascii_letters,digits') }] }} with_items: "{{ emails.split(',') | trim }}" - debug: var: users when: debug tags: - user
ansible-playbook /tmp/playbook.yml -e "emails=user1@example.com,user2@example.com" ... TASK [debug] ******************************************************************************************************************************************************* ok: [localhost] => { "users": [ { "email": "user1@example.com", "password": "hHy05pYvpfNBn1S9" }, { "email": "user2@example.com", "password": "dcqzCC0Zcw9LKc0a" } ] } ... # user毎のパスワードが入ったファイルが出来る ls /tmp/user.*.txt /tmp/user.user1@example.com.password.txt /tmp/user.user2@example.com.password.txt
- include_vars: "{{ lookup('first_found', possible_files) }}" vars: possible_files: - "{{ ansible_distribution }}.yaml" - "{{ ansible_os_family }}.yaml" - default.yaml
- include_vars: "{{ lookup('first_found', files=_files, skip=yes ) }}"
- include_vars: "{{ files }}" when: files | length vars: params: files: - "dir1/var1.yml" skip: yes files: "{{ lookup('first_found', params) }}"
- debug: msg="user.name is {{ lookup('ini', 'user.name type=properties file=user.properties') }}"
- debug: msg={{ lookup('ini', 'aws_access_key_id section=default file=~/.aws/config') }}
- debug: msg="{{ lookup('ini', 'aws_access_key_id section=%s file=~/.aws/config'|format(section)) }}"
lookup 関数で出来た
- hosts: 127.0.0.1 gather_facts: False vars: ec2_user_data: "{{ lookup('file', 'roles/cloudinit/files/centos6.yml') }}" tasks: - name: ec2 instance local_action: module: ec2 keypair: example-key group: example-sg instance_type: t2.micro image: ami-xxxxxxxx region: ap-northeast-1 vpc_subnet_id: subnet-xxxxxxxx assign_public_ip: yes count: 1 volumes: - device_name: /dev/sda1 volume_size: 10 # - device_name: /dev/sdb # t2インスタンスはephemeral diskは無い # ephemeral: ephemeral0 instance_tags: '{"Name":"test.example.com","key2":"val2"}' user_data: "{{ ec2_user_data }}" wait: yes wait_timeout: 600