Talk:Ansible: Difference between revisions

 
(12 intermediate revisions by the same user not shown)
Line 138:
* Verify YAML syntax:
ansible-playbook --syntax-check mycode.yaml
 
* Dry Run:
ansible-playbook -C mycode.yaml
* Step-by-Step:
ansible-playbook --step mycode.yaml
 
= Playbook =
 
* Tasks are performed by Modules.
* Ordering is important: Plays & Tasks are executed in the order they are presented.
* Playbooks do not change anything if the host is already in desired state.
* Avoid modules like command, shell & raw as they are not idempotent by nature.
 
== Task Attribute ==
 
* Most important Attribute is Task attribute:
 
<pre>
tasks:
- name: run service
service: name=vsftpd enabled=true
</pre>
 
* Here '-' marks beginning of a list of attributes.
* If multiple tasks are defined, each first attribute of task starts with '-'.
 
== Other Attributes ==
 
name: specify a specific label to play
hosts: uses patterns to define on which hosts to run a play
remote_user: overwrites the remote_user setting in ansible.cfg
become: overwrites the become setting in ansible.cfg
become_method: overwrites the become_method setting in ansible.cfg
become_user: overwrites the become_user setting in ansible.cfg
 
= Variables =
 
* Used for repeated tasks like:
Creating users
Removing files
Installing packages
* Names must start with letters
* Can be defined at a lot of levels
* Can be defined with different scope:
Global Scope: Set from CLI or ansible configuration file
Play Scope: Defined in playbook
Host Scope: Set on groups or Individual hosts through inventory file.
* Variables defined at highest level wins: Global scope wins from host scope
 
== Defining Variables ==
 
* Defining in Playbook:
<pre>
- hosts: all
vars:
user: test
home: /home/test
</pre>
* Using variable files:
<pre>
- hosts: all
vars_files:
- vars/users.yml
 
$ cat vars/users.yml
user: test
home: /home/test
user: test2
home: /home/test2
</pre>
 
== Using Variables ==
 
<pre>
tasks:
- name: Creates the user {{user}}
user:
name: "{{ user }}"
</pre>
 
== Host and Group Variables ==
 
Host Variable: Applies to one host defined in Inventory file.
Group Variable: Applies to multiple hosts defined in Inventory file.
 
* Depricated method: Define them in Inventory file:
[webservers]
webserver.example.com
[webservers:vars]
user=test
 
* Recommended method: Use group_vars & host_vars directories in project directory which contains inventory file.
nano group_vars/webservers
nano host_vars/server01
 
* Variables can be overwritten from CLI using
ansible-playbook -e "key=value"
 
== Arrays ==
 
* Define multiple values
* Refer to these using: users.test.first_name
users:
test:
first_name: test
last_name: done
home_dir: /home/test
test2:
first_name: test2
last_name: done
home_dir: /home/test2
 
= Facts =
 
* Contain discovered information about a host
* Can be used in conditional statements to make sure certain tasks run only if they are necessary.
* Setup module is used to gather fact information
ansible -i inventory servers -m setup
 
* Facts provide a lot of information, Filter them:
ansible -i inventory servers -m setup -a 'filter=ansible_kernel'
 
* Custom facts can be created to display information about a host.
* Create a file on the Managed Host:
sudo nano /etc/ansible/facts.d/servers.fact
[server_info]
profile = web_server
 
Check the facts:
ansible -i inventory servers -m setup -a 'filter=ansible_local'
 
= Inclusions =
 
* These make it easy to create a modular Ansible Setup.
* Main variables can be set in the master Ansible file, generic tasks can be defined in included files.
* Tasks can be included in a playbook from external YAML file using '''include''' directive.
* Variables can be included from YAML or JSON file using '''include_vars''' directive.