Variables and Flow Control Flashcards
Variables to Retrieve the Results of Running Commands:
Register
- name: 1
hosts: server1
tasks:- name: create file on server1
copy:
dest: /tmp/hello
content: “Hello world!”
mode: 0644
register: output - name: testing results from creation file hello
debug:
msg: The below results are available {{ output }}
- name: create file on server1
- name: 2
hosts: server2
become: yes
tasks:- name: install apache on server2
yum:
name: httpd
state: latest - name: run and enable apache
service:
name: httpd
state: started
enabled: yes
register: apache_state - name: check output of apache_state
debug:
msg: “{{ apache_state }}”
…
- name: install apache on server2
Handlers
- name: testing handlers
hosts: server1
become: yes
handlers:- name: copy index.html
copy:
dest: /var/www/html/index.html
content: “Hello World!!!”
listen: “copy index” - name: restart apache
service:
name: httpd
state: restarted
listen: “restart apache”
tasks: - name: install apache
yum:
name: httpd
state: latest - name: enable apache
service:
name: httpd
state: started
notify:- “copy index”
- “restart apache”
…
- name: copy index.html
Using loops:
- with_items
- loop
- name: creating users with loops
hosts: servers
become: yes
tasks:- name: create users
user:
name: “{{ item }}”
state: present
loop:- lisa
- anna
- bob
- name: set password for new users
shell:
cmd: echo 123 | passwd –stdin {{ item }}
with_items:- lisa
- anna
- bob
- name: create users
- name: creating files with loops
hosts: localhost
tasks:- name: create files
copy:
dest: /tmp/{{ item }}
content: ‘adding data to {{ item }}’
with_items:- file1
- file2
- file3
…
- name: create files
Testing when conditional and loops using dictionary key-value
- name: testing loops
hosts: servers
become: yes
tasks:- name: create new group
group:
name: test - name: create users
user:
name: “{{ item.name }}”
append: yes
groups: “{{ item.groups }}”
with_items:- { name: ‘lisa’, groups: ‘wheel’ }
- { name: ‘ann’, groups: ‘test’ }
- name: install httpd
yum:
name: httpd
state: latest
when:- ansible_host == “10.0.1.232”
…
- ansible_host == “10.0.1.232”
- name: create new group
Ignore errors on certain hosts:
key:
ingore_errors: yes
- name: ingoring errors while copying files from remote hosts
hosts: localhost
tasks:- name: copy content of index file to tmp dir
get_url:
url: http://{{ item.host }}/index.html
dest: /tmp/{{ item.name }}
ignore_errors: yes
register: output
with_items:- { host: ‘10.0.1.164’, name: ‘server1’ }
- { host: ‘10.0.1.155’, name: ‘server2’ }
- name: checking results
debug:
msg: “This is output from copying: {{ output }}”
- name: copy content of index file to tmp dir
Block-rescue-always
Note: always is not mandatory
similar to try-except
Note: that module names after block keyword should have -, which is not common
–
- name: install httpd
hosts: servers
become: yes
tasks:
- name: install httpd
yum:
name: httpd
state: latest
- name: create index file
copy:
dest: /var/www/html/index.html
content: “Hello World”
- name: block-rescue-always
hosts: localhost
tasks:
- name: check if index file exists
block:
- get_url:
url: “http://{{ item.server }}/index.html”
dest: /tmp/{{ item.file }}
with_items:
- { server: “10.0.1.42”, file: ‘server1’ }
- { server: “10.0.1.176”, file: ‘server2’ }
rescue:
- debug
msg: “Service is unreacheable”
always:
- debug:
msg: “ Testing block-rescue module”
…
tags:
should be set on the level of module
ansible-playbook -i inventory tags.yaml –skip-tags “install,start” is the same as ansible-playbook -i inventory tags.yaml –tags “users,password”
- name: testing creation of users
become: yes
hosts: servers
tasks:- name: create users
user:
name: “{{ item }}”
with_items:- lisa
- anna
tags: - users
- name: set password
shell:
cmd: echo 123 | passwd –stdin {{ item }}
with_items:- lisa
- anna
tags: passwords
- name: create users
- name: testing set up of httpd
become: yes
hosts: server1
tasks:- name: install httpd
yum:
name: httpd
state: latest
tags:- install
- name: run httpd service
service:
name: httpd
state: started
enabled: yes
tags:- start
…
- start
- name: install httpd
Force_handlers
Forces previously notifies handler to run
If multiple tasks are notifying the same handler and one of those tasks fail, then the handler will not run and this can leave a host in an unexpected state.
For example, a configuration file being updated, which will then notify the handler that a service needs to be restarted but then another task in the same play has a failure, which causes the handler not to run. So to force that to always run, set force handlers equal to true
When
- hosts: all
become: true
tasks:- name: check file exists
shell:
cmd: “ls -la /tmp/archive1.txt “
register: variable
ignore_errors: true - name: create archive.txt
file:
path: /tmp/archive1.txt
state: touch
when: variable is failed
…
- name: check file exists
Failed_when
Defines failure conditions