Variables and Flow Control Flashcards

1
Q

Variables to Retrieve the Results of Running Commands:

Register

A
  • 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: 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 }}”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Handlers

A
  • 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”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Using loops:

  • with_items
  • loop
A
  • 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: creating files with loops
    hosts: localhost
    tasks:
    • name: create files
      copy:
      dest: /tmp/{{ item }}
      content: ‘adding data to {{ item }}’
      with_items:
      • file1
      • file2
      • file3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Testing when conditional and loops using dictionary key-value

A
  • 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”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Ignore errors on certain hosts:
key:
ingore_errors: yes

A
  • 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 }}”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A


- 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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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”

A
  • 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: 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Force_handlers

Forces previously notifies handler to run

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Failed_when

A

Defines failure conditions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly