Skip to content

Instantly share code, notes, and snippets.

@itbakery
Created February 4, 2018 03:31
Show Gist options
  • Save itbakery/2979607dcb88e2103260ec1f090ace0e to your computer and use it in GitHub Desktop.
Save itbakery/2979607dcb88e2103260ec1f090ace0e to your computer and use it in GitHub Desktop.
prepare ansible

Install Ansible

$ sudo pip3.6 install git+https://github.com/ansible/ansible.git@devel
$ ansible
ansible             ansible-doc         ansible-pull
ansible-config      ansible-galaxy      ansible-vault
ansible-connection  ansible-inventory   
ansible-console     ansible-playbook

/usr/lib/python3.6/site-packages

folder structure

mkdir platform
cd Ansible
mkdir roles
touch hosts
wget https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
vi ansible.cfg
[defaults]
inventory = hosts
roles_path    = roles
vi hosts
server

ansible -m ping all

prepare host with lxd

sudo dnf install lxc lxc-templates lxc-extra debootstrap libvirt perl gpg

$ sudo systemctl start libvirtd.service
$ sudo systemctl start lxc.service
$ sudo systemctl enable lxc.service
$ sudo systemctl start libvirtd.service

sudo brctl show
virbr0		8000.525400a261e8	yes		virbr0-nic

Edit config to use virbr0

vi /etc/lxc/default.conf

lxc.network.type = veth
lxc.network.link = virbr0
lxc.network.flags = up
lxc.network.hwaddr = 00:16:3e:xx:xx:xx

Dhcp range from libvirt use by container

$ sudo systemctl status libvirtd.service | grep range
Feb 04 09:44:47 localhost.localdomain dnsmasq-dhcp[7275]: DHCP, IP range 192.168.122.2 -- 192.168.122.254, lease time 1h

kernel support

lxc-checkconfig   

Kernel configuration found at /boot/config-4.14.16-300.fc27.x86_64
--- Namespaces ---
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: enabled
Network namespace: enabled

Create ubuntu linux container

sudo lxc-create -t download -n ubuntu-c1 -- -d ubuntu -r xenial -a amd64

Setting up the GPG keyring
Downloading the image index
Downloading the rootfs
Downloading the metadata
The image cache is now ready
Unpacking the rootfs

---
You just created an Ubuntu container (release=xenial, arch=amd64, variant=default)

To enable sshd, run: apt-get install openssh-server

For security reason, container images ship without user accounts
and without a root password.

Use lxc-attach or chroot directly into the rootfs to set a root password
or create user accounts.

change root to set password of root user and ubuntu

$ sudo chroot /var/lib/lxc/ubuntu-c1/rootfs/ passwd ubuntu
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

$ sudo chroot /var/lib/lxc/ubuntu-c1/rootfs/ passwd 
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

login to

$ sudo lxc-start -n ubuntu-c1
$ sudo lxc-console -n ubuntu-c1
$ sudo lxc-stop -n ubuntu-c1

Note: Exit from lxc-console with Ctrl-a q alternate start with sudo lxc-start -d -n ubuntu-c1

$ sudo lxc-console  -n ubuntu-c1

Connected to tty 1
Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself

Ubuntu 16.04.3 LTS ubuntu-c1 pts/0

ubuntu-c1 login: 

check ip address

9: eth0@if10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:16:3e:bb:13:4d brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.122.141/24 brd 192.168.122.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::216:3eff:febb:134d/64 scope link 
       valid_lft forever preferred_lft forever

install ssh

sudo apt install openssh-server
sudo systemctl start ssh
sudo systemctl enable ssh

sudo apt install python

Edit host

vi hosts
web ansible_ssh_host=192.168.122.141

$ ansible -m ping all 

web | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Warning: Permanently added '192.168.122.141' (ECDSA) to the list of known hosts.\r\nroot@192.168.122.141: Permission denied (publickey,password).\r\n", 
    "unreachable": true
}

$ sudo ansible -m ping all -u ubuntu
web | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ubuntu@192.168.122.141: Permission denied (publickey,password).\r\n",
    "unreachable": true
}


Copy key to target

$ ssh-copy-id  ubuntu@192.168.122.141

$ ansible -m ping all -u ubuntu
web | SUCCESS => {
    "changed": false,
    "failed": false,
    "ping": "pong"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment