Ansible: Difference between revisions

From DWIKI
No edit summary
mNo edit summary
(43 intermediate revisions by the same user not shown)
Line 6: Line 6:
*[http://cloudacademy.com/blog/going-deeper-into-ansible-playbooks/ Going Deeper into Ansible Playbooks]
*[http://cloudacademy.com/blog/going-deeper-into-ansible-playbooks/ Going Deeper into Ansible Playbooks]
*[https://serversforhackers.com/an-ansible-tutorial An Ansible Tutorial]
*[https://serversforhackers.com/an-ansible-tutorial An Ansible Tutorial]
*[https://www.cyberciti.biz/faq/how-to-install-and-configure-latest-version-of-ansible-on-ubuntu-linux/ How to Install and Configure latest version of Ansible on Ubuntu Linux]
*[https://networklore.com/ansible-getting-started/ Getting started with Ansible]
*[https://ansible-tips-and-tricks.readthedocs.io/en/latest/os-dependent-tasks/variables/ Tips and tricks]
*[https://www.ansible.com/products/awx-project AWX]
*[https://galaxy.ansible.com Ansible Galaxy]
*[https://linuxacademy.com/blog/red-hat/ansible-roles-explained/ Ansible roles explained]
*[https://docs.ansible.com/ansible/latest/user_guide/playbooks_async.html Timeouts]
*[https://manintheit.org/ansible/ansible-sample-scripts/ Ansible sample scripts]


=Tools=
*[https://molecule.readthedocs.io/en/latest/ Molecule] for testing roles
*ansible-lint
=Docs=
*[http://ansible-docs.readthedocs.io/zh/stable-2.0/rst/playbooks_conditionals.html Conditionals]
*[https://docs.ansible.com/ansible/2.5/user_guide/playbooks_async.html Parallel!]
=Some terms=
=Inventories=
*[http://docs.ansible.com/ansible/latest/intro_dynamic_inventory.html Dynamic Inventory]
*[https://www.jeffgeerling.com/blog/creating-custom-dynamic-inventories-ansible Creating custom dynamic inventories]
=Alternatives=
*[[Puppet]]
*[[Saltstack]]


=Quickstart=
=Quickstart=
Line 43: Line 69:
  - name: useraccounts
  - name: useraccounts
   user:
   user:
     name: "{{ item.key \}\}"
     name: "{{ item.key }}"
     comment: "{{ item.value.name }}"
     comment: "{{ item.value.name }}"
     uid: "{{ item.value.uid }}"
     uid: "{{ item.value.uid }}"
Line 55: Line 81:
  - name: userpasswords
  - name: userpasswords
   user:
   user:
     name: "{{ item.key \}\}"
     name: "{{ item.key }}"
     password: "\{\{ lookup('csvfile',item.key + ' file=/etc/shadow delimiter=: col=1' ) \}\}"
     password: "{{ lookup('csvfile',item.key + ' file=/etc/shadow delimiter=: col=1' ) }}"
   with_dict: accounts
   with_dict: accounts


Line 70: Line 96:
   tags:
   tags:
     - foobar
     - foobar
  </pre>                                        
  </pre>
 
 
 
 
= FAQ =
 
== roles for multiple distributions ==
 
  include_vars with "distro-{{ansible_distro_name}}.yml"
 
== Escape single quote ==
 
'foo ''bar'' '
 
==Syntax Error while loading YAML. did not find expected key==
Check indentation
 
 
&nbsp;
 
== Newlines in output ==
 
instead of all those '\n':
 
In ansible.cfg:
 
stdout_callback = yaml
 
== Ad-hoc commands ==
 
[http://docs.ansible.com/ansible/latest/intro_adhoc.html http://docs.ansible.com/ansible/latest/intro_adhoc.html]
 
== check python code ==
 
ansible-test sanity --test pep8 mycode.py
 
== Command/shell output on single line ==
 
ANSIBLE_STDOUT_CALLBACK=oneline ansible-playbook foo.yml
 
== Storing passwords ==
 
[http://docs.ansible.com/ansible/2.4/vault.html http://docs.ansible.com/ansible/2.4/vault.html]


== Show all host variables ==


=FAQ=
==Show all host variables==
  ansible -m setup <hostname>
  ansible -m setup <hostname>


==Syntax highlighting for ansible==
== Show all OS/versions ==
 
ansible all -m setup -a "filter=ansible_distribution*"
 
== Syntax highlighting for ansible ==


*https://github.com/chase/vim-ansible-yaml
*[https://github.com/chase/vim-ansible-yaml https://github.com/chase/vim-ansible-yaml]


Drop the files in ~/vim/bundle and in .vimrc:
Drop the files in ~/vim/bundle and in .vimrc:
  call pathogen#infect()
  call pathogen#infect()
  call pathogen#helptags()
  call pathogen#helptags()
Or maybe better:
*[https://github.com/pearofducks/ansible-vim https://github.com/pearofducks/ansible-vim]
== Retry ==
--limit @/home/ansible/ssh.retry
== ERROR! this task 'import_playbook' has extra params ==
meh
&nbsp;
== /usr/bin/chattr: Clearing extent flag not supported ==
Probably trying to make a backup of a symlink
== Escape curly braces ==
{{ '{' }}
== Check file for string ==
  tasks:
    - name: grep line
      shell: "grep -q swap /etc/fstab"
      failed_when: false
      register: grepped
    - name: show grep
      debug:
        msg: "exists"
      when: grepped.rc == 0
     
     
== Error messages ==
=== msg: The PyMySQL (Python 2.7 and Python 3.X) or MySQL-python (Python 2.X) module is required ===
Mans you nedd to install for example python2-PyMySQL on that host
=Tips & tricks=
==Includes only when on host group==
  - block:
    - include: foo.yml
    - include: bar.yml
  when: "'foobar' in group_names"
== Show info/facts of a host ==
ansible somehost -m setup
ansible somehost -m ansible.builtin.setup
==Show distribution and version==
  - name: show some host info
    debug:
      msg: Dist {{ ansible_distribution }} {{ ansible_distribution_version }}

Revision as of 19:34, 21 January 2022

Configuration management

Links

Tools

Docs

Some terms

Inventories

Alternatives

Quickstart

On server as root create key:

ssh-keygen

(go for defaults) and then get content of ~/.ssh/id_rsa.pub in your copypastebuffer

On 'clients' edit /etc/ssh/sshd_config to

PermitRootLogin without-password

and restart sshd

Edit/create ~root/.ssh/authorized_keys and add:

from="ip.of.ansible.server" <paste public key here>

Scripts/playbooks

Maintain useraccounts

 ---

 - name: remove users
  user: name=exuser state=absent force=yes groups=''
  with_dict: accounts
  tags:
    - delusers

 - name: sync group
  group:
    name: sync
    gid: 999
    state: "present"

 - name: fix homedir rights
  lineinfile: dest=/etc/login.defs regexp=^UMASK line="UMASK 007"

 - name: useraccounts
  user:
    name: "{{ item.key }}"
    comment: "{{ item.value.name }}"
    uid: "{{ item.value.uid }}"
    state: "present"
    shell: "/bin/bash"
    groups: sudo
  with_dict: accounts
  tags:
    - accounts

 - name: userpasswords
  user:
    name: "{{ item.key }}"
    password: "{{ lookup('csvfile',item.key + ' file=/etc/shadow delimiter=: col=1' ) }}"
  with_dict: accounts

#ssh keys
 - name: userkeys
  authorized_key: user={{ item.key }} key="{{ lookup('file','/home/' + item.key + '/.ssh/authorized_keys' ) }}" exclusive=yes
  with_dict: accounts
  tags:
    - keys

 - name: nofoobar
  user: name=foobar state=absent remove=yes
  tags:
    - foobar
 



FAQ

roles for multiple distributions

 include_vars with "distro-Template:Ansible distro name.yml"

Escape single quote

'foo bar '

Syntax Error while loading YAML. did not find expected key

Check indentation


 

Newlines in output

instead of all those '\n':

In ansible.cfg:

stdout_callback = yaml

Ad-hoc commands

http://docs.ansible.com/ansible/latest/intro_adhoc.html

check python code

ansible-test sanity --test pep8 mycode.py

Command/shell output on single line

ANSIBLE_STDOUT_CALLBACK=oneline ansible-playbook foo.yml

Storing passwords

http://docs.ansible.com/ansible/2.4/vault.html

Show all host variables

ansible -m setup <hostname>

Show all OS/versions

ansible all -m setup -a "filter=ansible_distribution*"

Syntax highlighting for ansible

Drop the files in ~/vim/bundle and in .vimrc:

call pathogen#infect()
call pathogen#helptags()

Or maybe better:

Retry

--limit @/home/ansible/ssh.retry

ERROR! this task 'import_playbook' has extra params

meh

 

/usr/bin/chattr: Clearing extent flag not supported

Probably trying to make a backup of a symlink

Escape curly braces

{{ '{' }}

Check file for string

 tasks:
   - name: grep line
     shell: "grep -q swap /etc/fstab"
     failed_when: false
     register: grepped
   - name: show grep
     debug:
       msg: "exists"
     when: grepped.rc == 0
      
     

Error messages

msg: The PyMySQL (Python 2.7 and Python 3.X) or MySQL-python (Python 2.X) module is required

Mans you nedd to install for example python2-PyMySQL on that host

Tips & tricks

Includes only when on host group

  - block:
    - include: foo.yml 
    - include: bar.yml
  when: "'foobar' in group_names"


Show info/facts of a host

ansible somehost -m setup
ansible somehost -m ansible.builtin.setup

Show distribution and version

 - name: show some host info
   debug:
     msg: Dist Template:Ansible distribution Template:Ansible distribution version