Part 2 - Section 3 and further Flashcards
Create a playbook to ping your localhost
vi first.yml
”—”
- name
hosts: localhost
tasks:
- name: “test connectivity
ping:
Check the syntax of your playbook
Run your playbook without affecting anything
ansible-playbook –syntax-check first.yml
dry run - runs like it would normally but won’t affect anything
ansible-playbook –check first.yml
Create playbook that outputs “Hello World” on localhost
vi heloworld.yml
- name: My Second Playbook
hosts: localhost
tasks:- name: Print Hello World
debug: msg=”Hello World”
- name: Print Hello World
ansible-playbook helloworld.yml
Create a playbook that pings the local host and also prints “Hello World”
vi mtask.yml
“—”
- name: Running Two Tasks
hosts: localhost
tasks:
- name: Test Connectivity
ping:
- name: Print Hello World debug: msg="Hello World" "..." ansible-playbook mtask.yml
Install a package and start it via playbook
- name Install HTTPD
hosts: localhost
tasks:- name: Install Apache
yum:
name: httpd
state: present - name: Start Httpd
service:
name: httpd
state: started
- name: Install Apache
Create a webservers group of clients in your hosts file for 192.168.10.12
192.168.10.15-22
How would you specify a different hosts file in a different location using ansible-playbook
[webservers]
192.168.10.12
192.168.10.[15:22]
ansible-playbook -i /home/me/hosts
If you have hosts in another directory, in your playbook just list it as the name of the inventory. When referencing with “-i” put the complete path.
If you don’t have a fqdn for your clients that you want in a hostfile, how can you setup an Alias for them?
[servers]
server1 ansible_ssh_host=192.168.10.142
What’s the difference between static and dynamic hosts in a config file
dynamic - dhcp clients
Run a command to see what’s in your hostfile
ansible-inventory –list
Establish a connection to remote clients
ping all clients with ansible
Show all their uptime
in hosts
[webservers]
192.168.10.142
ansible-inventory –list
ssh-keygen
ssh-copy-id 192.168.10.142
ansible all -m ping
ansible -a “uptime” all
a = arguments
Check remote client connectivity for all inventory via a playbook
Basically just ping everything in your hosts file
vi clientstatus.yml
-name: “Connectivity check”
hosts: all
tasks:
- name: Test Connectivity
ping:
ansible-playbook clientstatus.yml
Copy a file over to multiple clients via a playbook
The file should be owned by the delsinm user and group. Permissions should be 0644
echo “data” > some.cfg
vi copy.yml
- name Copy
hosts: all
tasks:- name: copy
become: True
copy:
src: /home/delsinm/some.cfg
dest: /tmp
owner: delsinm
group: delsinm
mode: 0644
- name: copy
become: True - run this as any user
owner: who will the owner be of the file
Change the file permissions of a file via playbook
From any client
touch /home/delsinm/linux2
vi filepermissions.yml
- name: Change file permissions
hosts: all
tasks:
- name: change file permissions
file:
path: /home/delsinm/linux2
mode: a+w (do this or octal)
Check file/directory status
vi checkFS.yml
- name FS checks
hosts: localhost
tasks:
- name: Check file status and attributes
stat:
path: /etc/hosts
register: fs
- name: Show result debug: msg: File attributes {{ fs }}
register - container to put output in, you can call is whatever you want
msg - This is just what will display on your output
Create and remove a directory and add a file to it. Give it permissions and an owning user and group.
Stat the file - check if the file was created
vi crfile.yml
- name: Create and remove file
hosts: all
tasks:
- name: Create a directory
file:
path: /tmp/seinfeld
owner: delsinm
group: delsinm
mode: 0770
state: directory
- name: create a file file: /tmp/seinfeld/jerry state: touch - name: stat new file stat: path: /tmp /seinfeld/jerry registry: jf - name: Show file status debug: msg: File status and attributes {{ jf }} - name: Remove file file: path: /tmp/seinfeld/jerry state: absent - name: Remove directory file: path: /tmp/seinfeld state: absent become: true
The msg here will display on the command line